From f0fbefa144a07033a75b59fbc58e544f105a5586 Mon Sep 17 00:00:00 2001 From: Ania Kowalska <63072419+akowalska622@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:43:07 +0100 Subject: [PATCH] [Discover] Highlight matching field values when performing a KQL search on a keyword field (#201952) ## Summary Closes [Highlighting isn't visible in Discover on keywords if the field also has text type while searching #118590](https://github.com/elastic/kibana/issues/118590 ) Added `text` field highlight when querying on matching `keyword` Before: ![image](https://github.com/user-attachments/assets/8b296df6-5647-4c4f-bfb1-4cd9f575e3c9) After: Screenshot 2024-11-27 at 13 10 51 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --- .../src/utils/format_hit.test.ts | 30 +++++++++++++++++++ .../src/utils/format_hit.ts | 9 ++++-- .../src/components/source_document.test.tsx | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/kbn-discover-utils/src/utils/format_hit.test.ts b/packages/kbn-discover-utils/src/utils/format_hit.test.ts index ba38812b0e142..6e2acbd43846b 100644 --- a/packages/kbn-discover-utils/src/utils/format_hit.test.ts +++ b/packages/kbn-discover-utils/src/utils/format_hit.test.ts @@ -106,6 +106,36 @@ describe('formatHit', () => { ]); }); + it('should highlight a subfield even shouldShowFieldHandler determines it should not be shown ', () => { + const highlightHit = buildDataTableRecord( + { + _id: '2', + _index: 'logs', + fields: { + object: ['object'], + 'object.value': [42, 13], + }, + highlight: { 'object.value': ['%%'] }, + }, + dataViewMock + ); + + const formatted = formatHit( + highlightHit, + dataViewMock, + (fieldName) => ['object'].includes(fieldName), + 220, + fieldFormatsMock + ); + + expect(formatted).toEqual([ + ['object.value', 'formatted:42,13', 'object.value'], + ['object', ['object'], 'object'], + ['_index', 'formatted:logs', '_index'], + ['_score', undefined, '_score'], + ]); + }); + it('should filter fields based on their real name not displayName', () => { const formatted = formatHit( row, diff --git a/packages/kbn-discover-utils/src/utils/format_hit.ts b/packages/kbn-discover-utils/src/utils/format_hit.ts index 99913e32cb78c..b29353253df51 100644 --- a/packages/kbn-discover-utils/src/utils/format_hit.ts +++ b/packages/kbn-discover-utils/src/utils/format_hit.ts @@ -70,9 +70,14 @@ export function formatHit( const pairs = highlights[key] ? renderedPairs : otherPairs; // If the field is a mapped field, we first check if it should be shown, - // if not we always include it into the result. + // or if it's highlighted, but the parent is not. + // If not we always include it into the result. if (displayKey) { - if (shouldShowFieldHandler(key)) { + const multiParent = field.getSubtypeMulti?.()?.multi.parent; + const isHighlighted = Boolean(highlights[key]); + const isParentHighlighted = Boolean(multiParent && highlights[multiParent]); + + if ((isHighlighted && !isParentHighlighted) || shouldShowFieldHandler(key)) { pairs.push([displayKey, undefined, key]); } } else { diff --git a/packages/kbn-unified-data-table/src/components/source_document.test.tsx b/packages/kbn-unified-data-table/src/components/source_document.test.tsx index f48e2b58c8424..25d61312dc242 100644 --- a/packages/kbn-unified-data-table/src/components/source_document.test.tsx +++ b/packages/kbn-unified-data-table/src/components/source_document.test.tsx @@ -52,7 +52,7 @@ describe('Unified data table source document cell rendering', function () { /> ); expect(component.html()).toMatchInlineSnapshot( - `"
_index
test
_score
1
"` + `"
extension
.gz
_index
test
_score
1
"` ); }); });