Skip to content

Commit

Permalink
[Discover] Highlight matching field values when performing a KQL sear…
Browse files Browse the repository at this point in the history
…ch 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](#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)
- [ ] ...
  • Loading branch information
akowalska622 authored Dec 3, 2024
1 parent e5b1773 commit f0fbefa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/kbn-discover-utils/src/utils/format_hit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions packages/kbn-discover-utils/src/utils/format_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Unified data table source document cell rendering', function () {
/>
);
expect(component.html()).toMatchInlineSnapshot(
`"<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>"`
`"<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>"`
);
});
});

0 comments on commit f0fbefa

Please sign in to comment.