Skip to content

Commit

Permalink
[8.5] [Discover] Improve performance of getFieldsToShow (#144672) (#1…
Browse files Browse the repository at this point in the history
…45424)

# Backport

This will backport the following commits from `main` to `8.5`:
- [[Discover] Improve performance of getFieldsToShow
(#144672)](#144672)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthias
Wilhelm","email":"matthias.wilhelm@elastic.co"},"sourceCommit":{"committedDate":"2022-11-16T17:53:18Z","message":"[Discover]
Improve performance of getFieldsToShow (#144672)\n\n##
Summary\r\n\r\nSignificantly 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.","sha":"1ffb93d687e899ec93b4e1735458e9e2863d6b39","branchLabelMapping":{"^v8.6.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:fix","Team:DataDiscovery","v8.6.0","v7.17.8","v8.7.0","v8.5.2"],"number":144672,"url":"#144672
Improve performance of getFieldsToShow (#144672)\n\n##
Summary\r\n\r\nSignificantly 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.","sha":"1ffb93d687e899ec93b4e1735458e9e2863d6b39"}},"sourceBranch":"main","suggestedTargetBranches":["7.17","8.7","8.5"],"targetPullRequestStates":[{"branch":"main","label":"v8.6.0","labelRegex":"^v8.6.0$","isSourceBranch":true,"state":"MERGED","url":"#144672
Improve performance of getFieldsToShow (#144672)\n\n##
Summary\r\n\r\nSignificantly 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.","sha":"1ffb93d687e899ec93b4e1735458e9e2863d6b39"}},{"branch":"7.17","label":"v7.17.8","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.5","label":"v8.5.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Matthias Wilhelm <matthias.wilhelm@elastic.co>
  • Loading branch information
kibanamachine and kertal committed Nov 16, 2022
1 parent f74a1f0 commit d08bc2f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/plugins/discover/public/utils/get_fields_to_show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
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);
});
};

0 comments on commit d08bc2f

Please sign in to comment.