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 <matt@mattki.me>
This commit is contained in:
Lukas Olson 2025-03-18 10:17:53 -07:00 committed by GitHub
parent a7cc00c4fe
commit 2de4b331d3
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,