[7.17] [Discover] Improve performance of getFieldsToShow (#144672) (#178731)

# Backport

Backport of the following commits from `main` to `7.17`:
- [[Discover] Improve performance of getFieldsToShow (#144672)](https://github.com/elastic/kibana/pull/144672)

## Summary

Significantly improves the performance of `getFieldsToShow` for very large data views (>=100k). This is done by reducing the number of iterations necessary to detect multi fields shat should not be displayed.
This commit is contained in:
Matthias Wilhelm 2024-03-18 16:24:26 +01:00 committed by GitHub
parent 3b0097e0b3
commit aca26741b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,28 +7,42 @@
*/
import { IndexPattern, getFieldSubtypeMulti } from '../../../../data/common';
/**
* 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[],
indexPattern: IndexPattern,
dataView: IndexPattern,
showMultiFields: boolean
) => {
const childParentFieldsMap = {} as Record<string, string>;
const mapping = (name: string) => indexPattern.fields.getByName(name);
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);
});
};