Skip to content

Commit c53d432

Browse files
akowalska622CAWilson94
authored andcommitted
[Discover] Highlight matching field values when performing a KQL search on a keyword field (elastic#201952)
## Summary Closes [Highlighting isn't visible in Discover on keywords if the field also has text type while searching elastic#118590](elastic#118590 ) Added `text` field highlight when querying on matching `keyword` Before: ![image](https://github.com/user-attachments/assets/8b296df6-5647-4c4f-bfb1-4cd9f575e3c9) After: <img width="722" alt="Screenshot 2024-11-27 at 13 10 51" src="https://github.com/user-attachments/assets/c4c0ae23-e6bc-4840-84fb-908c50da5491"> ### 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) - [ ] ...
1 parent 09f4184 commit c53d432

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

packages/kbn-discover-utils/src/utils/format_hit.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ describe('formatHit', () => {
106106
]);
107107
});
108108

109+
it('should highlight a subfield even shouldShowFieldHandler determines it should not be shown ', () => {
110+
const highlightHit = buildDataTableRecord(
111+
{
112+
_id: '2',
113+
_index: 'logs',
114+
fields: {
115+
object: ['object'],
116+
'object.value': [42, 13],
117+
},
118+
highlight: { 'object.value': ['%%'] },
119+
},
120+
dataViewMock
121+
);
122+
123+
const formatted = formatHit(
124+
highlightHit,
125+
dataViewMock,
126+
(fieldName) => ['object'].includes(fieldName),
127+
220,
128+
fieldFormatsMock
129+
);
130+
131+
expect(formatted).toEqual([
132+
['object.value', 'formatted:42,13', 'object.value'],
133+
['object', ['object'], 'object'],
134+
['_index', 'formatted:logs', '_index'],
135+
['_score', undefined, '_score'],
136+
]);
137+
});
138+
109139
it('should filter fields based on their real name not displayName', () => {
110140
const formatted = formatHit(
111141
row,

packages/kbn-discover-utils/src/utils/format_hit.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ export function formatHit(
7070
const pairs = highlights[key] ? renderedPairs : otherPairs;
7171

7272
// If the field is a mapped field, we first check if it should be shown,
73-
// if not we always include it into the result.
73+
// or if it's highlighted, but the parent is not.
74+
// If not we always include it into the result.
7475
if (displayKey) {
75-
if (shouldShowFieldHandler(key)) {
76+
const multiParent = field.getSubtypeMulti?.()?.multi.parent;
77+
const isHighlighted = Boolean(highlights[key]);
78+
const isParentHighlighted = Boolean(multiParent && highlights[multiParent]);
79+
80+
if ((isHighlighted && !isParentHighlighted) || shouldShowFieldHandler(key)) {
7681
pairs.push([displayKey, undefined, key]);
7782
}
7883
} else {

packages/kbn-unified-data-table/src/components/source_document.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('Unified data table source document cell rendering', function () {
5252
/>
5353
);
5454
expect(component.html()).toMatchInlineSnapshot(
55-
`"<dl class=\\"euiDescriptionList unifiedDataTable__descriptionList unifiedDataTable__cellValue css-id58dh-euiDescriptionList-inline-left\\" data-test-subj=\\"discoverCellDescriptionList\\" data-type=\\"inline\\"><dt class=\\"euiDescriptionList__title unifiedDataTable__descriptionListTitle css-1fizlic-euiDescriptionList__title-inline-compressed\\">_index</dt><dd class=\\"euiDescriptionList__description unifiedDataTable__descriptionListDescription css-11rdew2-euiDescriptionList__description-inline-compressed\\">test</dd><dt class=\\"euiDescriptionList__title unifiedDataTable__descriptionListTitle css-1fizlic-euiDescriptionList__title-inline-compressed\\">_score</dt><dd class=\\"euiDescriptionList__description unifiedDataTable__descriptionListDescription css-11rdew2-euiDescriptionList__description-inline-compressed\\">1</dd></dl>"`
55+
`"<dl class=\\"euiDescriptionList unifiedDataTable__descriptionList unifiedDataTable__cellValue css-id58dh-euiDescriptionList-inline-left\\" data-test-subj=\\"discoverCellDescriptionList\\" data-type=\\"inline\\"><dt class=\\"euiDescriptionList__title unifiedDataTable__descriptionListTitle css-1fizlic-euiDescriptionList__title-inline-compressed\\">extension</dt><dd class=\\"euiDescriptionList__description unifiedDataTable__descriptionListDescription css-11rdew2-euiDescriptionList__description-inline-compressed\\">.gz</dd><dt class=\\"euiDescriptionList__title unifiedDataTable__descriptionListTitle css-1fizlic-euiDescriptionList__title-inline-compressed\\">_index</dt><dd class=\\"euiDescriptionList__description unifiedDataTable__descriptionListDescription css-11rdew2-euiDescriptionList__description-inline-compressed\\">test</dd><dt class=\\"euiDescriptionList__title unifiedDataTable__descriptionListTitle css-1fizlic-euiDescriptionList__title-inline-compressed\\">_score</dt><dd class=\\"euiDescriptionList__description unifiedDataTable__descriptionListDescription css-11rdew2-euiDescriptionList__description-inline-compressed\\">1</dd></dl>"`
5656
);
5757
});
5858
});

0 commit comments

Comments
 (0)