From d08bc2fc2c62274c16732ca0621964747cc961b8 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 16 Nov 2022 13:55:47 -0500 Subject: [PATCH] [8.5] [Discover] Improve performance of getFieldsToShow (#144672) (#145424) # Backport This will backport the following commits from `main` to `8.5`: - [[Discover] Improve performance of getFieldsToShow (#144672)](https://github.com/elastic/kibana/pull/144672) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Matthias Wilhelm --- .../public/utils/get_fields_to_show.ts | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/plugins/discover/public/utils/get_fields_to_show.ts b/src/plugins/discover/public/utils/get_fields_to_show.ts index fd9fdd70a4279..be6bcc2ea5ce9 100644 --- a/src/plugins/discover/public/utils/get_fields_to_show.ts +++ b/src/plugins/discover/public/utils/get_fields_to_show.ts @@ -8,24 +8,38 @@ import { getFieldSubtypeMulti } from '@kbn/data-views-plugin/public'; import type { DataView } from '@kbn/data-views-plugin/public'; +/** + * 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: DataView, showMultiFields: boolean) => { - const childParentFieldsMap = {} as Record; - const mapping = (name: string) => dataView.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); }); };