Skip to content

Commit

Permalink
Increase flatten performance of SearchSource (#150658)
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal authored Feb 16, 2023
1 parent 807625c commit 2406ada
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,7 @@
*/

import { setWith } from '@kbn/safer-lodash-set';
import {
difference,
isEqual,
isFunction,
isObject,
keyBy,
pick,
uniqueId,
uniqWith,
concat,
} from 'lodash';
import { difference, isEqual, isFunction, isObject, keyBy, pick, uniqueId, concat } from 'lodash';
import {
catchError,
finalize,
Expand Down Expand Up @@ -889,28 +879,28 @@ export class SearchSource {
// inject the format from the computed fields if one isn't given
const docvaluesIndex = keyBy(filteredDocvalueFields, 'field');
const bodyFields = this.getFieldsWithoutSourceFilters(index, body.fields);
body.fields = uniqWith(
bodyFields.concat(filteredDocvalueFields),
(fld1: SearchFieldValue, fld2: SearchFieldValue) => {
const field1Name = this.getFieldName(fld1);
const field2Name = this.getFieldName(fld2);
return field1Name === field2Name;

const uniqueFieldNames = new Set();
const uniqueFields = [];
for (const field of bodyFields.concat(filteredDocvalueFields)) {
const fieldName = this.getFieldName(field);
if (metaFields.includes(fieldName) || uniqueFieldNames.has(fieldName)) {
continue;
}
)
.filter((fld: SearchFieldValue) => {
return !metaFields.includes(this.getFieldName(fld));
})
.map((fld: SearchFieldValue) => {
const fieldName = this.getFieldName(fld);
if (Object.keys(docvaluesIndex).includes(fieldName)) {
// either provide the field object from computed docvalues,
// or merge the user-provided field with the one in docvalues
return typeof fld === 'string'
? docvaluesIndex[fld]
: this.getFieldFromDocValueFieldsOrIndexPattern(docvaluesIndex, fld, index);
}
return fld;
});
uniqueFieldNames.add(fieldName);
if (Object.keys(docvaluesIndex).includes(fieldName)) {
// either provide the field object from computed docvalues,
// or merge the user-provided field with the one in docvalues
uniqueFields.push(
typeof field === 'string'
? docvaluesIndex[field]
: this.getFieldFromDocValueFieldsOrIndexPattern(docvaluesIndex, field, index)
);
} else {
uniqueFields.push(field);
}
}
body.fields = uniqueFields;
}
} else {
body.fields = filteredDocvalueFields;
Expand Down

0 comments on commit 2406ada

Please sign in to comment.