Skip to content
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

[7.9] Fix sorting of scripted string fields (#72681) #72737

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@ 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,
name: 'murmur script',
type: 'murmur3',
};
const indexPattern = {
fields: [scriptedField, murmurScriptedField],
fields: [scriptedField, stringScriptedField, booleanScriptedField, murmurScriptedField],
} as IIndexPattern;

it('should return an array', function () {
Expand Down Expand Up @@ -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(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down