-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discover] Change default sort handling #85561
Changes from all commits
22592f6
6ee0ebc
905d143
f837f33
7b2bfbd
51a8b93
b2a53f8
815c133
93e3d2c
720bb74
1c187ba
bd0d0e3
a73ee9b
484446c
5c957b8
8bc46e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { getDefaultSort } from './get_default_sort'; | ||
// @ts-ignore | ||
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; | ||
import { IndexPattern } from '../../../../kibana_services'; | ||
|
||
describe('getDefaultSort function', function () { | ||
let indexPattern: IndexPattern; | ||
beforeEach(() => { | ||
indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IndexPattern; | ||
}); | ||
test('should be a function', function () { | ||
expect(typeof getDefaultSort === 'function').toBeTruthy(); | ||
}); | ||
|
||
test('should return default sort for an index pattern with timeFieldName', function () { | ||
expect(getDefaultSort(indexPattern, 'desc')).toEqual([['time', 'desc']]); | ||
expect(getDefaultSort(indexPattern, 'asc')).toEqual([['time', 'asc']]); | ||
}); | ||
|
||
test('should return default sort for an index pattern without timeFieldName', function () { | ||
delete indexPattern.timeFieldName; | ||
expect(getDefaultSort(indexPattern, 'desc')).toEqual([]); | ||
expect(getDefaultSort(indexPattern, 'asc')).toEqual([]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
* under the License. | ||
*/ | ||
import { IndexPattern } from '../../../../kibana_services'; | ||
// @ts-ignore | ||
import { isSortable } from './get_sort'; | ||
import { SortOrder } from '../components/table_header/helpers'; | ||
|
||
|
@@ -26,12 +25,12 @@ import { SortOrder } from '../components/table_header/helpers'; | |
* the default sort is returned depending of the index pattern | ||
*/ | ||
export function getDefaultSort( | ||
indexPattern: IndexPattern, | ||
indexPattern: IndexPattern | undefined, | ||
defaultSortOrder: string = 'desc' | ||
): SortOrder[] { | ||
if (indexPattern.timeFieldName && isSortable(indexPattern.timeFieldName, indexPattern)) { | ||
if (indexPattern?.timeFieldName && isSortable(indexPattern.timeFieldName, indexPattern)) { | ||
return [[indexPattern.timeFieldName, defaultSortOrder]]; | ||
} else { | ||
return [['_score', defaultSortOrder]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quick question @timroes , I don't think it makes much sense to sort by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @timroes update: changed the default to |
||
return []; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { getSortForSearchSource } from './get_sort_for_search_source'; | ||
// @ts-ignore | ||
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; | ||
import { IndexPattern } from '../../../../kibana_services'; | ||
import { SortOrder } from '../components/table_header/helpers'; | ||
|
||
describe('getSortForSearchSource function', function () { | ||
let indexPattern: IndexPattern; | ||
beforeEach(() => { | ||
indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IndexPattern; | ||
}); | ||
test('should be a function', function () { | ||
expect(typeof getSortForSearchSource === 'function').toBeTruthy(); | ||
}); | ||
|
||
test('should return an object to use for searchSource when columns are given', function () { | ||
const cols = [['bytes', 'desc']] as SortOrder[]; | ||
expect(getSortForSearchSource(cols, indexPattern)).toEqual([{ bytes: 'desc' }]); | ||
expect(getSortForSearchSource(cols, indexPattern, 'asc')).toEqual([{ bytes: 'desc' }]); | ||
delete indexPattern.timeFieldName; | ||
expect(getSortForSearchSource(cols, indexPattern)).toEqual([{ bytes: 'desc' }]); | ||
expect(getSortForSearchSource(cols, indexPattern, 'asc')).toEqual([{ bytes: 'desc' }]); | ||
}); | ||
|
||
test('should return an object to use for searchSource when no columns are given', function () { | ||
const cols = [] as SortOrder[]; | ||
expect(getSortForSearchSource(cols, indexPattern)).toEqual([{ _doc: 'desc' }]); | ||
expect(getSortForSearchSource(cols, indexPattern, 'asc')).toEqual([{ _doc: 'asc' }]); | ||
delete indexPattern.timeFieldName; | ||
expect(getSortForSearchSource(cols, indexPattern)).toEqual([{ _score: 'desc' }]); | ||
expect(getSortForSearchSource(cols, indexPattern, 'asc')).toEqual([{ _score: 'asc' }]); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what scenario would index pattern be undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't. But when using this function in an embeddable, according the TypeScript it could be. So the decision was the edit in the function or in the embeddable. decided to modify here, since the function would work without an index pattern.