Fix sorting of scripted string fields (#72681)

This commit is contained in:
Nick Partridge 2020-07-21 15:31:49 -05:00 committed by GitHub
parent 8d5a5d0860
commit b0ef3e9580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 3 deletions

View file

@ -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(
[

View file

@ -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';