Skip to content

fix(search): handle synonyms in V3 matched fields and add whole-query template synonyms - #21669

Open
alexjst wants to merge 8 commits into
masterfrom
fix/v3-matched-fields-synonyms
Open

alexjst wants to merge 8 commits into
masterfrom
fix/v3-matched-fields-synonyms

Conversation

@alexjst

@alexjst alexjst commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Two synonym fixes for Search V3.

Matched fields for synonyms. On V3, matched fields are found after the query from each hit's stored values (MatchedFields), because the shared copy_to fields the query reads cannot be highlighted. That scan did not apply synonyms, while the analyzers do expand them. A hit retrieved only through a synonym, such as a name holding one spelling when the query used the other, came back with no matched fields, so match tiers saw no name match and the two spellings of one query ranked their results differently. MatchedFields now also matches the one-word synonyms of the query and of each of its words, from the equivalence lines of the synonym files (SearchTemplateSynonyms). As in the analyzers, the lookup reads the folded words of the query before short words are dropped, so a quoted query, words joined by punctuation and a word shorter than the minimum length still find the synonyms of each word (a synonym-file phrase that itself contains punctuation, such as single sign-on, is not matched as a whole query). A field reports a value that holds a typed word ahead of one that holds only a synonym. Multi-word synonyms, which the engine matches only as phrases, and explicit => rules are left out, and prefix matching still applies only to the last word as typed. In explain output a synonym match reads as a word match on the stemmed subfield.

Whole-query synonyms for templates. The template synonyms list holds the synonyms of the whole query and of each of its words, which suits recall clauses. An exact-name clause built on it would treat a word's synonym as a name for the whole query. Templates can now read exactSynonyms, the synonyms of the whole query only, capped like synonyms. The design doc and the operator context table list it, and the design doc corrects the synonyms row, which described the whole query only. The shipped fulltext template's exact-name term still reads synonyms; switching it changes default ranking and needs its own seed revision, so it is left for a follow-up.

Tests: MatchedFieldsTest.testSynonymsOfTheQueryMatch uses an equivalence line from the shipped synonym file where neither word is a prefix or stem of the other, and fails without the change. testMultiWordSynonymWordsDoNotMatchAlone and testTypedWordsWinOverSynonyms cover the phrase and preference rules. SearchTemplateSynonymTest.testWholeQuerySynonymsLeaveOutWordSynonyms checks that a multi-word query's word synonyms stay out of exactSynonyms, that a phrase synonym is in it, and that a body may branch on it at write time. MatchedFieldsTest.testTypedWordsWinAcrossThePathsOfOneName and testAPathListedUnderTwoNamesReportsBoth, and SearchTemplateRequestFlowTest.testALabelKeyKeepsItsNameWhenAnotherLabelReadsIt, cover how a label's paths are grouped; testATypedStoredPathWinsOverASynonymOnTheLabelKey and testAHighlightedPathCountsWithTheQueriedPathsOfALabel cover the same rule through the template scan, and testSynonymsOfShortAndQuotedWordsMatch covers the folded lookup and the explain output of a synonym match. testExactSynonymsLeaveOutWordSynonyms also checks that exactSynonyms follows the synonym cap. The existing matched-field, request-handler, render, golden and contract tests pass unchanged.

A label read from its own key and from stored paths reports a typed match from any of them ahead of a synonym-only one (MatchedFields.firstPerName, used by the template and stage scans). Each name's paths are listed explicitly, so a label key that another label also reads keeps its own name, and the queried and highlighted label sets give their paths to one scan per label. Within one scan, matches that hold a typed word are reported ahead of synonym-only ones, each group in label order, so a caller that reads the first of several matched labels gets a typed one when there is one (structured-property paths are scanned after the labels); a label matched only through a stored path no longer follows the labels matched on their own key. The synonym catalog now merges a term that appears on several lines, as the analyzers' synonym map does; the shipped synonym file repeats no term, so its expansions do not change. A query whose only words are shorter than the minimum length, such as s3, now fetches the scanned paths when it has a synonym, on the template and non-template V3 paths. The per-path scan behind the non-template V3 search drops a synonym-only value when another path of the same name holds a typed word.

In OSS the visible change is the matched-fields report and explain output. A deployment with a match-tier post-process ranks by these fields too: on such an evaluation catalog with spelling-variant synonyms, the two spellings of a two-word query now rank the same top five, and no other ranking assertion changed.

V3 finds matched fields after the query, from each hit's stored values, and
did not apply synonyms. The analyzers do expand them, so a hit retrieved only
through a synonym, such as a name holding one spelling when the query used the
other, came back with no matched fields. Match tiers then saw no name match at
all, and the two spellings of one query ranked differently.

MatchedFields now adds the stems of the query's synonyms, and of each of its
words' synonyms, from the synonym files the analyzers load, so such a hit
reports the field it matched through.
@github-actions github-actions Bot added the product PR or Issue related to the DataHub UI/UX label Nov 27, 2026
@cursor

cursor Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

PR Summary

Overview
V3 matched fields now treat one-word equivalence synonyms (from the same synonym files the analyzers use) when deciding which stored value to show, but only when no typed word match exists on that field. Multi-word and explicit => synonyms stay excluded; typed matches are ordered ahead of synonym-only ones. Label scanning merges queried and highlight paths via firstPerName, so a label can keep its own name when another label shares a stored path, and stored-path matches compete correctly with direct keys.

Search templates gain exactSynonyms: synonyms of the whole query only (not each word’s), capped like synonyms, so recall-oriented synonyms and precision clauses (e.g. exact-name) can diverge. synonyms is clarified/expanded to include per-word expansions. The synonym catalog loader merges terms that appear on multiple equivalence lines, matching analyzer behavior.

Docs for Search V3 highlight behavior and Mustache context are updated accordingly.

Reviewed by Cursor Bugbot for commit 8c1ba11. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Smart test selection

pytest smoke tests

Mode: active. Only the selection below runs for the pytest smoke tests on this PR (applied: smart).

  • Selection: smart: tests for the tagged features, plus always-run modules, this PR's changed tests, and the plumbing sample if the PR changes the pytest harness
  • Size: 146 of 846 tests in 1 batch, predicted longest batch ~2 min
  • Tags: change-type: docs; feature-dev: search-indexing; feature-user: search; technical-layer: elasticsearch
  • Confidence: HIGH (14 changed files)
  • Classifier reasoning: Changes rework Search V3 matched-field reporting, synonym handling and template context in the Elasticsearch query path, with tests and docs for those behaviors.
Selected scopes (30 files or classes, 146 tests)
  • tests/e2e/authorization/test_self_entity_auth.py
  • tests/e2e/browse/browse_test.py
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchBasic
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchDiagnose
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchDryRun
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchErrorHandling
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchFilterCombinations
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchFiltersByEntityType
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchFiltersByPlatform
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchPagination
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchProjectionLive
  • tests/e2e/cli/search_cmd/test_search_cmd.py::TestSearchWhereFilter
  • tests/e2e/frontend/test_static_asset_encodings.py
  • tests/e2e/mcp/mcp_test.py
  • tests/e2e/query_projection/test_query_projection_smoke.py
  • tests/e2e/read_only/test_search.py
  • tests/e2e/read_only/test_services_up.py
  • tests/e2e/search/test_lineage_search_index_fields.py
  • tests/e2e/search/test_search_projection.py::TestSearchProjection
  • tests/e2e/semantic/test_classical_embedding_provider.py::TestClassicalEmbeddingProvider
  • tests/e2e/semantic/test_config_fingerprint.py::TestConfigFingerprint
  • tests/e2e/semantic/test_current_offset_api.py
  • tests/e2e/semantic/test_event_driven_docs.py::TestEventDrivenDocumentIngestion
  • tests/e2e/semantic/test_event_mode_fallback.py::TestEventModeFallback
  • tests/e2e/semantic/test_local_embedding_provider.py::TestLocalEmbeddingProvider
  • tests/e2e/semantic/test_semantic_search.py::TestSemanticSearchWithIngestion
  • tests/e2e/semantic/test_source_type_filtering.py::TestSourceTypeFiltering
  • tests/e2e/semantic/test_stateful_ingestion.py::TestStatefulIngestion
  • tests/e2e/siblings/siblings_search_batching_test.py
  • tests/e2e/test_e2e.py

Add the run-all-tests:pytest label to run the full pytest suite on this PR.

Playwright E2E tests

This run: smart

  • Selection: smart: specs tagged search, search-indexing plus the tests the change affects
  • Size: 52 tests in 8 spec files, ~9 min predicted on 2 shard(s)
Specs selected because of this PR (8)
Spec Tests Why
autocomplete-v2/autocomplete.spec.ts 2/2 tagged search
browse-v2/browse-v2.spec.ts 10/10 tagged search
logical-models/physical-children-view.spec.ts 2/2 tagged search
search/query-and-filter-search.spec.ts 10/10 tagged search
search/search-filters.spec.ts 5/5 tagged search
search/search-within-operator.spec.ts 1/1 tagged search
search/search.spec.ts 7/7 tagged search
search/searchv2.spec.ts 15/15 tagged search

Add the run-all-tests:playwright label to run the full Playwright suite on this PR.

commit 8c1ba11 · workflow run

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 6 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...csearch/query/template/SearchTemplateSynonyms.java 87.50% 1 Missing and 3 partials ⚠️
...ticsearch/query/request/v3/StageMatchedFields.java 87.50% 0 Missing and 1 partial ⚠️
...h/elasticsearch/query/v3/SearchV3QueryBuilder.java 83.33% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

The template `synonyms` list holds the synonyms of the whole query and of each
of its words. That suits recall clauses, but an exact-name clause built on it
treats a word's synonym as a name for the whole query: for a two-word query,
an entity named like one word's synonym would count as an exact match.

Templates can now read `exactSynonyms`, the synonyms of the whole query only,
capped like `synonyms`. The design doc lists it and corrects the `synonyms`
row, which described the whole query only.
@alexjst alexjst changed the title fix(search): report V3 matched fields for synonyms of the query fix(search): handle synonyms in V3 matched fields and template exact names Nov 27, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Review follow-up for the synonym-aware V3 matched fields:

- A multi-word synonym matches the engine only as a phrase, but the word scan
  matched each of its words alone, so a hit holding just one of them reported
  a field. Multi-word synonyms are now left out.
- A synonym-only match could take a field's single reported value, or the
  fragment anchor, from a value that also held a typed word. A field now
  reports a value with a typed word first and falls back to a synonym-only
  value.
…ynonyms docs

Review follow-up:

- A name such as a dataset's can be read from its own key and from fallback
  paths, and the template scan took the first match per name across separate
  scans, so a synonym-only value on the key could hide a typed match on a
  fallback path. MatchedFields.firstPerName scans one name's paths together,
  typed words first, and the template and stage scans use it.
- The shipped fulltext template's exact-name term still expands every word's
  synonym; the docs and Javadoc now say exactSynonyms is for such a clause
  rather than that one reads it. The operator context table lists it, and
  the design doc says one-word synonyms now count in matched fields.
@alexjst alexjst changed the title fix(search): handle synonyms in V3 matched fields and template exact names fix(search): handle synonyms in V3 matched fields and add whole-query template synonyms Nov 27, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c5486cf. Configure here.

The scan grouped a label's key with its stored paths by renaming each
path through the fallback labels. A label key that is also a stored path
of another label was then renamed to that label and never reported under
its own name.

firstPerName now takes the paths of each name explicitly, so one path can
be listed under two names. The per-path scan behind the non-template V3
search also drops a synonym-only value when another path of the same name
holds a typed word.
…abel once

The synonym lookup read the raw query split on whitespace, while the
analyzers fold the query's words and expand their synonyms before they
drop short words. A quoted or punctuated query, or one whose only word is
shorter than the minimum length, reported no synonym match, although the
engine found the hit through that synonym. The lookup now reads the
folded words, and a synonym counts as a query word.

The queried and highlighted label sets were scanned one after the other,
so a label could report a synonym-only value from the first scan and hide
a typed value from a stored path that only the second set gives it. Both
sets now give their paths to one scan per label.

Tests also cover the exactSynonyms cap, a typed stored path against a
synonym on the label key, and a synonym match in explain output.
…eated synonym terms

A caller that reads the first of several matched names, such as a match
tier that takes a display name, could get a synonym-only value ahead of a
typed one on a later name. Typed matches are now reported first.

The synonym catalog kept only the last line of a term that appears on
several lines, while the analyzers' synonym map takes the members of
every line. It now merges them. The shipped synonym file repeats no term,
so its expansions do not change.

@alexsku alexsku left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found two cases where the engine can match a stored name but the V3 response omits it from matchedFields. The inline examples are small enough to add to MatchedFieldsTest. I also found a whitespace inconsistency in the new exactSynonyms variable.

final Matcher matcher = WORD.matcher(text);
while (matcher.find()) {
for (String form : forms(matcher.group())) {
if (synonymStems.contains(stem(form))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searching staging reports name=stg_orders, but name=stgOrders produces no matched field. I checked the index-side word delimiter with the V3 settings and it emits stg for stgOrders. This scan folds the entire camel-case token before comparing stems and loses that boundary. Could we preserve the index-side word forms when scanning stored values and test both spellings against staging?

// multi-word synonym matches only as a phrase, which this word scan cannot check, so it is left
// out rather than matching each of its words alone.
final Set<String> synonymStems = new HashSet<>();
for (String synonym : SearchTemplateSynonyms.expansions(String.join(" ", foldedWords))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For data platform team, the shipped data platform, dataplatform equivalence rule produces dataplatform in Lucene’s synonym graph. The lookup here tries the entire query and each individual word, so a stored name=dataplatform yields no matched field. Could the scan recognize a multi-token input phrase within a longer query when the rule emits a single token? That would also preserve the existing behavior that an individual word of a multi-word output is insufficient.

*/
@Nonnull
public static List<String> wholeQuery(@Nonnull String query) {
String text = query.toLowerCase(Locale.ROOT).trim();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wholeQuery("data platform") returns dataplatform, while wholeQuery("data platform") returns an empty list. The matched-field scan recognizes the phrase with either spacing. Could we normalize internal whitespace before looking up a whole phrase and add a test for repeated spaces?

This branch was successfully deployed

1 active deployment
Preview — 8c1ba113 Deployed Nov 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product PR or Issue related to the DataHub UI/UX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants