Skip to content

Commit

Permalink
[Search Source] Do not pick scripted fields if * provided (#85133)
Browse files Browse the repository at this point in the history
* [Search Source] Do not pick scripted fields if * provided

* Update src/plugins/data/common/search/search_source/search_source.ts

Co-authored-by: Tim Roes <[email protected]>

* Add unit test

* Add documentation

Co-authored-by: Tim Roes <[email protected]>
  • Loading branch information
Maja Grubic and timroes authored Dec 9, 2020
1 parent 0a04835 commit 74bb155
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/plugins/data/common/search/search_source/search_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ describe('SearchSource', () => {
const request = await searchSource.getSearchRequestBody();
expect(request.script_fields).toEqual({ hello: {} });
});

test('returns all scripted fields when one fields entry is *', async () => {
searchSource.setField('index', ({
...indexPattern,
getComputedFields: () => ({
storedFields: [],
scriptFields: { hello: {}, world: {} },
docvalueFields: [],
}),
} as unknown) as IndexPattern);
searchSource.setField('fields', ['timestamp', '*']);

const request = await searchSource.getSearchRequestBody();
expect(request.script_fields).toEqual({ hello: {}, world: {} });
});
});

describe('handling for when specific fields are provided', () => {
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,12 @@ export class SearchSource {
case 'query':
return addToRoot(key, (data[key] || []).concat(val));
case 'fields':
// uses new Fields API
// This will pass the passed in parameters to the new fields API.
// Also if will only return scripted fields that are part of the specified
// array of fields. If you specify the wildcard `*` as an array element
// the fields API will return all fields, and all scripted fields will be returned.
// NOTE: While the fields API supports wildcards within names, e.g. `user.*`
// scripted fields won't be considered for this.
return addToBody('fields', val);
case 'fieldsFromSource':
// preserves legacy behavior
Expand Down Expand Up @@ -518,11 +523,13 @@ export class SearchSource {
);
const uniqFieldNames = [...new Set([...bodyFieldNames, ...fieldsFromSource])];

// filter down script_fields to only include items specified
body.script_fields = pick(
body.script_fields,
Object.keys(body.script_fields).filter((f) => uniqFieldNames.includes(f))
);
if (!uniqFieldNames.includes('*')) {
// filter down script_fields to only include items specified
body.script_fields = pick(
body.script_fields,
Object.keys(body.script_fields).filter((f) => uniqFieldNames.includes(f))
);
}

// request the remaining fields from stored_fields just in case, since the
// fields API does not handle stored fields
Expand Down

0 comments on commit 74bb155

Please sign in to comment.