Skip to content

Commit

Permalink
[Discover] Improve performance for unmapped fields (#159113)
Browse files Browse the repository at this point in the history
Enhanced Discover performance for scenarios with numerous unmapped fields. While investigating an edge case involving 4k unmapped fields, an issue was identified in our nested field detection code, resulting in significant processing time. This commit optimizes the code by addressing redundant RegEx execution, resulting in a performance boost for the specific scenario.
  • Loading branch information
kertal authored Aug 28, 2023
1 parent ba61ac0 commit 3c1e333
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
40 changes: 40 additions & 0 deletions packages/kbn-discover-utils/src/utils/nested_fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { DataView } from '@kbn/data-views-plugin/common';
import { isNestedFieldParent } from './nested_fields';
describe('isNestedFieldParent', () => {
it('correctly identifies nested parent fields', () => {
const nestedField = {
name: 'nested.field',
type: 'keyword',
subType: {
nested: {
path: 'nested.field.path',
},
},
};
const unnestedField = {
name: 'unnested.field',
type: 'keyword',
};
const list = [nestedField, unnestedField];

const dataView = {
fields: {
getByName: jest.fn((fieldName) => list.find((field) => field.name === fieldName)),
getAll: jest.fn(() => list),
},
} as unknown as DataView;

expect(isNestedFieldParent('nested', dataView)).toBe(true);
expect(isNestedFieldParent('nested.field', dataView)).toBe(false);
expect(isNestedFieldParent('unnested.field', dataView)).toBe(false);
expect(isNestedFieldParent('whateverField', dataView)).toBe(false);
});
});
5 changes: 4 additions & 1 deletion packages/kbn-discover-utils/src/utils/nested_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ import type { DataView } from '@kbn/data-views-plugin/public';
* issue: https://github.com/elastic/kibana/issues/54957
*/
export function isNestedFieldParent(fieldName: string, dataView: DataView): boolean {
const nestedRootRegex = new RegExp(escapeRegExp(fieldName) + '(\\.|$)');
return (
!dataView.fields.getByName(fieldName) &&
!!dataView.fields.getAll().find((patternField) => {
// We only want to match a full path segment
const nestedRootRegex = new RegExp(escapeRegExp(fieldName) + '(\\.|$)');
const subTypeNested = getDataViewFieldSubtypeNested(patternField);
if (!subTypeNested) {
return false;
}
return nestedRootRegex.test(subTypeNested?.nested.path ?? '');
})
);
Expand Down

0 comments on commit 3c1e333

Please sign in to comment.