[8.16] Fix sort for rollup data views (#214656) (#215033)

# Backport

This will backport the following commits from `main` to `8.16`:
- [Fix sort for rollup data views
(#214656)](https://github.com/elastic/kibana/pull/214656)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Lukas
Olson","email":"lukas@elastic.co"},"sourceCommit":{"committedDate":"2025-03-18T17:17:53Z","message":"Fix
sort for rollup data views (#214656)\n\n## Summary\n\nResolves
https://github.com/elastic/kibana/issues/213629.\n\nSince
https://github.com/elastic/kibana/pull/163784 we have included
a\n`format` parameter in the `sort` that we send to Elasticsearch.
This\nworked for everything except rollup data views, which break when
the\n`format` parameter is provided.\n\nThis restores the behavior prior
to that PR (we still send the `sort`\nbut don't include the `format`
parameter). Ideally we would probably not\nsend the timestamp field at
all for rollup data views since we treat\nthem as if they are
non-time-based, but this would require a bit of a\nrefactor, and rollups
are deprecated anyway.\n\n### Checklist\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n\n### Release notes\n\nFixes opening a
rollup data view in Discover.\n\nCo-authored-by: Matthew Kime
<matt@mattki.me>","sha":"2de4b331d334454c4b3bb17b75dcdb83207ee9f9","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Search","release_note:fix","Feature:Data
Views","Feature:Rollups","Team:DataDiscovery","backport:all-open","v9.1.0"],"title":"Fix
sort for rollup data
views","number":214656,"url":"https://github.com/elastic/kibana/pull/214656","mergeCommit":{"message":"Fix
sort for rollup data views (#214656)\n\n## Summary\n\nResolves
https://github.com/elastic/kibana/issues/213629.\n\nSince
https://github.com/elastic/kibana/pull/163784 we have included
a\n`format` parameter in the `sort` that we send to Elasticsearch.
This\nworked for everything except rollup data views, which break when
the\n`format` parameter is provided.\n\nThis restores the behavior prior
to that PR (we still send the `sort`\nbut don't include the `format`
parameter). Ideally we would probably not\nsend the timestamp field at
all for rollup data views since we treat\nthem as if they are
non-time-based, but this would require a bit of a\nrefactor, and rollups
are deprecated anyway.\n\n### Checklist\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n\n### Release notes\n\nFixes opening a
rollup data view in Discover.\n\nCo-authored-by: Matthew Kime
<matt@mattki.me>","sha":"2de4b331d334454c4b3bb17b75dcdb83207ee9f9"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/214656","number":214656,"mergeCommit":{"message":"Fix
sort for rollup data views (#214656)\n\n## Summary\n\nResolves
https://github.com/elastic/kibana/issues/213629.\n\nSince
https://github.com/elastic/kibana/pull/163784 we have included
a\n`format` parameter in the `sort` that we send to Elasticsearch.
This\nworked for everything except rollup data views, which break when
the\n`format` parameter is provided.\n\nThis restores the behavior prior
to that PR (we still send the `sort`\nbut don't include the `format`
parameter). Ideally we would probably not\nsend the timestamp field at
all for rollup data views since we treat\nthem as if they are
non-time-based, but this would require a bit of a\nrefactor, and rollups
are deprecated anyway.\n\n### Checklist\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n\n### Release notes\n\nFixes opening a
rollup data view in Discover.\n\nCo-authored-by: Matthew Kime
<matt@mattki.me>","sha":"2de4b331d334454c4b3bb17b75dcdb83207ee9f9"}}]}]
BACKPORT-->

Co-authored-by: Lukas Olson <lukas@elastic.co>
This commit is contained in:
Kibana Machine 2025-03-18 20:32:10 +01:00 committed by GitHub
parent 86b8706a96
commit 6c7c4d0b6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 3 deletions

View file

@ -7,6 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { DataViewType } from './types';
import { stubFieldSpecMap, stubLogstashFieldSpecMap } from './field.stub';
import { createStubDataView } from './data_views/data_view.stub';
export {
@ -23,6 +24,16 @@ export const stubDataView = createStubDataView({
},
});
export const stubRollupDataView = createStubDataView({
spec: {
id: 'logstash-*',
fields: stubFieldSpecMap,
title: 'logstash-*',
timeFieldName: '@timestamp',
type: DataViewType.ROLLUP,
},
});
export const stubIndexPattern = stubDataView;
export const stubDataViewWithoutTimeField = createStubDataView({

View file

@ -9,7 +9,11 @@
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { getSortForSearchSource } from './get_sort_for_search_source';
import { stubDataView, stubDataViewWithoutTimeField } from '@kbn/data-plugin/common/stubs';
import {
stubDataView,
stubDataViewWithoutTimeField,
stubRollupDataView,
} from '@kbn/data-plugin/common/stubs';
describe('getSortForSearchSource function', function () {
test('should be a function', function () {
@ -94,4 +98,24 @@ describe('getSortForSearchSource function', function () {
})
).toEqual([{ _score: 'asc' }]);
});
test('should return an object including format when data view is not a rollup', function () {
expect(
getSortForSearchSource({
sort: [['@timestamp', 'desc']],
dataView: stubDataView,
defaultSortDir: 'desc',
})
).toEqual([{ '@timestamp': { format: 'strict_date_optional_time', order: 'desc' } }]);
});
test('should not return an object excluding format when data view is a rollup', function () {
expect(
getSortForSearchSource({
sort: [['@timestamp', 'desc']],
dataView: stubRollupDataView,
defaultSortDir: 'desc',
})
).toEqual([{ '@timestamp': 'desc' }]);
});
});

View file

@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { DataView } from '@kbn/data-views-plugin/common';
import { type DataView, DataViewType } from '@kbn/data-views-plugin/common';
import type { EsQuerySortValue, SortDirection } from '@kbn/data-plugin/common';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { getSort } from './get_sort';
@ -50,7 +50,7 @@ export function getSortForSearchSource({
const sortPairs = getSort(sort, dataView, false); // ES|QL request is not using search source
const sortForSearchSource = sortPairs.map((sortPair: Record<string, string>) => {
if (timeFieldName && sortPair[timeFieldName]) {
if (dataView.type !== DataViewType.ROLLUP && timeFieldName && sortPair[timeFieldName]) {
return getESQuerySortForTimeField({
sortDir: sortPair[timeFieldName] as SortDirection,
timeFieldName,