Skip to content

Commit

Permalink
[Discover] Improve performance of getFieldsToShow (elastic#144672)
Browse files Browse the repository at this point in the history
## Summary

Significantly improves the performance of `getFieldsToShow` for very large data views (>=100k). This is done by reducing the number ofiterations necessary to detect multi fields shat should not be displayed.

(cherry picked from commit 1ffb93d)

# Conflicts:
#	src/plugins/discover/public/application/helpers/get_fields_to_show.ts
  • Loading branch information
kertal committed Mar 14, 2024
1 parent 2388662 commit 7d3208e
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,38 @@
*/
import { IndexPattern, getFieldSubtypeMulti } from '../../../../data/common';

export const getFieldsToShow = (
fields: string[],
indexPattern: IndexPattern,
showMultiFields: boolean
) => {
const childParentFieldsMap = {} as Record<string, string>;
const mapping = (name: string) => indexPattern.fields.getByName(name);
/**
* Returns am array of fields to display in the Documents column of the data table
* If showMultiFields is set to false, it filters out multifields that have a parent, to prevent entries for multifields
* like this: field, field.keyword, field.whatever
* @param fields
* @param dataView
* @param showMultiFields
*/
export const getFieldsToShow = (fields: string[], dataView: IndexPattern, showMultiFields: boolean) => {
if (showMultiFields) {
return fields;
}
const fieldSet = new Set();
const childParentFieldsMap = new Map();
const parentFieldSet = new Set();
fields.forEach((key) => {
const mapped = mapping(key);
const mapped = dataView.fields.getByName(key);
const subTypeMulti = mapped && getFieldSubtypeMulti(mapped.spec);
const isMultiField = Boolean(subTypeMulti?.multi);
if (mapped && subTypeMulti?.multi?.parent) {
childParentFieldsMap[mapped.name] = subTypeMulti.multi.parent;
childParentFieldsMap.set(key, subTypeMulti.multi.parent);
}
if (mapped && isMultiField) {
parentFieldSet.add(key);
}
fieldSet.add(key);
});
return fields.filter((key: string) => {
const fieldMapping = mapping(key);
const subTypeMulti = fieldMapping && getFieldSubtypeMulti(fieldMapping.spec);
const isMultiField = !!subTypeMulti?.multi;
if (!isMultiField) {
if (!parentFieldSet.has(key)) {
return true;
}
const parent = childParentFieldsMap[key];
return showMultiFields || (parent && !fields.includes(parent));
const parent = childParentFieldsMap.get(key);
return parent && !fieldSet.has(parent);
});
};

0 comments on commit 7d3208e

Please sign in to comment.