mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
Fix sorting of scripted string fields (#72681)
This commit is contained in:
parent
8d5a5d0860
commit
b0ef3e9580
2 changed files with 61 additions and 3 deletions
|
@ -23,13 +23,23 @@ import { IIndexPattern } from '../..';
|
|||
|
||||
describe('SearchSource#normalizeSortRequest', function () {
|
||||
const scriptedField = {
|
||||
name: 'script string',
|
||||
name: 'script number',
|
||||
type: 'number',
|
||||
scripted: true,
|
||||
sortable: true,
|
||||
script: 'foo',
|
||||
lang: 'painless',
|
||||
};
|
||||
const stringScriptedField = {
|
||||
...scriptedField,
|
||||
name: 'script string',
|
||||
type: 'string',
|
||||
};
|
||||
const booleanScriptedField = {
|
||||
...scriptedField,
|
||||
name: 'script boolean',
|
||||
type: 'boolean',
|
||||
};
|
||||
const murmurScriptedField = {
|
||||
...scriptedField,
|
||||
sortable: false,
|
||||
|
@ -37,7 +47,7 @@ describe('SearchSource#normalizeSortRequest', function () {
|
|||
type: 'murmur3',
|
||||
};
|
||||
const indexPattern = {
|
||||
fields: [scriptedField, murmurScriptedField],
|
||||
fields: [scriptedField, stringScriptedField, booleanScriptedField, murmurScriptedField],
|
||||
} as IIndexPattern;
|
||||
|
||||
it('should return an array', function () {
|
||||
|
@ -106,6 +116,54 @@ describe('SearchSource#normalizeSortRequest', function () {
|
|||
]);
|
||||
});
|
||||
|
||||
it('should use script based sorting with string type', function () {
|
||||
const result = normalizeSortRequest(
|
||||
[
|
||||
{
|
||||
[stringScriptedField.name]: SortDirection.asc,
|
||||
},
|
||||
],
|
||||
indexPattern
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
_script: {
|
||||
script: {
|
||||
source: stringScriptedField.script,
|
||||
lang: stringScriptedField.lang,
|
||||
},
|
||||
type: 'string',
|
||||
order: SortDirection.asc,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should use script based sorting with boolean type as string type', function () {
|
||||
const result = normalizeSortRequest(
|
||||
[
|
||||
{
|
||||
[booleanScriptedField.name]: SortDirection.asc,
|
||||
},
|
||||
],
|
||||
indexPattern
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
_script: {
|
||||
script: {
|
||||
source: booleanScriptedField.script,
|
||||
lang: booleanScriptedField.lang,
|
||||
},
|
||||
type: 'string',
|
||||
order: SortDirection.asc,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should use script based sorting only on sortable types', function () {
|
||||
const result = normalizeSortRequest(
|
||||
[
|
||||
|
|
|
@ -69,7 +69,7 @@ function normalize(
|
|||
|
||||
// The ES API only supports sort scripts of type 'number' and 'string'
|
||||
function castSortType(type: string) {
|
||||
if (['number', 'string'].includes(type)) {
|
||||
if (['number'].includes(type)) {
|
||||
return 'number';
|
||||
} else if (['string', 'boolean'].includes(type)) {
|
||||
return 'string';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue