Skip to content

Commit b0ef3e9

Browse files
authored
Fix sorting of scripted string fields (#72681)
1 parent 8d5a5d0 commit b0ef3e9

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/plugins/data/public/search/search_source/normalize_sort_request.test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,31 @@ import { IIndexPattern } from '../..';
2323

2424
describe('SearchSource#normalizeSortRequest', function () {
2525
const scriptedField = {
26-
name: 'script string',
26+
name: 'script number',
2727
type: 'number',
2828
scripted: true,
2929
sortable: true,
3030
script: 'foo',
3131
lang: 'painless',
3232
};
33+
const stringScriptedField = {
34+
...scriptedField,
35+
name: 'script string',
36+
type: 'string',
37+
};
38+
const booleanScriptedField = {
39+
...scriptedField,
40+
name: 'script boolean',
41+
type: 'boolean',
42+
};
3343
const murmurScriptedField = {
3444
...scriptedField,
3545
sortable: false,
3646
name: 'murmur script',
3747
type: 'murmur3',
3848
};
3949
const indexPattern = {
40-
fields: [scriptedField, murmurScriptedField],
50+
fields: [scriptedField, stringScriptedField, booleanScriptedField, murmurScriptedField],
4151
} as IIndexPattern;
4252

4353
it('should return an array', function () {
@@ -106,6 +116,54 @@ describe('SearchSource#normalizeSortRequest', function () {
106116
]);
107117
});
108118

119+
it('should use script based sorting with string type', function () {
120+
const result = normalizeSortRequest(
121+
[
122+
{
123+
[stringScriptedField.name]: SortDirection.asc,
124+
},
125+
],
126+
indexPattern
127+
);
128+
129+
expect(result).toEqual([
130+
{
131+
_script: {
132+
script: {
133+
source: stringScriptedField.script,
134+
lang: stringScriptedField.lang,
135+
},
136+
type: 'string',
137+
order: SortDirection.asc,
138+
},
139+
},
140+
]);
141+
});
142+
143+
it('should use script based sorting with boolean type as string type', function () {
144+
const result = normalizeSortRequest(
145+
[
146+
{
147+
[booleanScriptedField.name]: SortDirection.asc,
148+
},
149+
],
150+
indexPattern
151+
);
152+
153+
expect(result).toEqual([
154+
{
155+
_script: {
156+
script: {
157+
source: booleanScriptedField.script,
158+
lang: booleanScriptedField.lang,
159+
},
160+
type: 'string',
161+
order: SortDirection.asc,
162+
},
163+
},
164+
]);
165+
});
166+
109167
it('should use script based sorting only on sortable types', function () {
110168
const result = normalizeSortRequest(
111169
[

src/plugins/data/public/search/search_source/normalize_sort_request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function normalize(
6969

7070
// The ES API only supports sort scripts of type 'number' and 'string'
7171
function castSortType(type: string) {
72-
if (['number', 'string'].includes(type)) {
72+
if (['number'].includes(type)) {
7373
return 'number';
7474
} else if (['string', 'boolean'].includes(type)) {
7575
return 'string';

0 commit comments

Comments
 (0)