From 5caf5d82e21ea6c81cee1477f3d7a200de935aff Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 18 Mar 2025 10:17:53 -0700 Subject: [PATCH] Fix sort for rollup data views (#214656) ## Summary Resolves https://github.com/elastic/kibana/issues/213629. Since https://github.com/elastic/kibana/pull/163784 we have included a `format` parameter in the `sort` that we send to Elasticsearch. This worked for everything except rollup data views, which break when the `format` parameter is provided. This restores the behavior prior to that PR (we still send the `sort` but don't include the `format` parameter). Ideally we would probably not send the timestamp field at all for rollup data views since we treat them as if they are non-time-based, but this would require a bit of a refactor, and rollups are deprecated anyway. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed ### Release notes Fixes opening a rollup data view in Discover. Co-authored-by: Matthew Kime (cherry picked from commit 2de4b331d334454c4b3bb17b75dcdb83207ee9f9) --- .../data_views/common/data_view.stub.ts | 11 ++++++++ .../get_sort_for_search_source.test.ts | 26 ++++++++++++++++++- .../sorting/get_sort_for_search_source.ts | 4 +-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/platform/plugins/shared/data_views/common/data_view.stub.ts b/src/platform/plugins/shared/data_views/common/data_view.stub.ts index e11f251d0ffb35..0f6759181425f0 100644 --- a/src/platform/plugins/shared/data_views/common/data_view.stub.ts +++ b/src/platform/plugins/shared/data_views/common/data_view.stub.ts @@ -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({ diff --git a/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.test.ts b/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.test.ts index 8c1b8d5a90a38f..449ebab7a2a08b 100644 --- a/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.test.ts +++ b/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.test.ts @@ -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' }]); + }); }); diff --git a/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.ts b/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.ts index dfd444a918f153..e47f8b3e134629 100644 --- a/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.ts +++ b/src/platform/plugins/shared/discover/common/utils/sorting/get_sort_for_search_source.ts @@ -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) => { - if (timeFieldName && sortPair[timeFieldName]) { + if (dataView.type !== DataViewType.ROLLUP && timeFieldName && sortPair[timeFieldName]) { return getESQuerySortForTimeField({ sortDir: sortPair[timeFieldName] as SortDirection, timeFieldName,