published fresh until

Elasticsearch: rank exact product names above loose hits

Summary Map product names as text plus a normalized keyword subfield; combine whole-name, phrase and loose clauses, then inspect scoring before moving a replacement index behind the alias. Start with GET /products/_mapping

Not for MongoDB query indexing; see mongodb-query-slow-with-many-documents-compound-index-equality-sort-range. Normalized keyword exact equality, term phrase loose should boosts, explain/analyze diagnostics, replacement index reindex alias cutover.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run GET /products/_mapping and inspect whether the product name has a populated keyword subfield before changing boosts.

Short answer

As of November 2026, target Elasticsearch 8.19.0 syntax, anchored by its release record and notes. Use text for recall and normalized keyword for term-level queries. Boosts express preference; strict exact-first ordering needs explicit score tiers and descending score order.

Choose / don't choose

  • Keyword term: choose for equality of the whole name under a deliberate normalization policy. Don't run term against analyzed text expecting original whole-field equality (term query).
  • Analyzed phrase: choose when a phrase query over analyzed text is intended; don't treat phrase matching as whole-field equality (phrase query).
  • Fixed score tiers: choose if every exact product must precede every loose hit; don't choose if BM25 relevance must order documents within the tiers without additional design (function score).

Not for: MongoDB query indexing; see mongodb-query-slow-with-many-documents-compound-index-equality-sort-range.

Settings that matter

Setting or flagValue / defaultApplies toSource
name.type / name.fields.exact.typetext plus keyword subfield; existing documents do not gain new multi-field values automaticallyElasticsearch 8.19multi-fields
normalizerlowercase on the keyword field if case-insensitive whole-name equality is intended; runs at indexing and term-query timeElasticsearch 8.19normalizer
term.boostDefault 1.0; a value greater than 1 increases preference. Tune against judged names.Elasticsearch 8.19term
bool.minimum_should_matchExplicit 1 for the number of required should-clause matches; default drops to 0 when must/filter existsElasticsearch 8.19bool
match.fuzzinessAUTO only on the broader text clause when typo recall is wantedElasticsearch 8.19match
score_mode / boost_modemax / replace for explicit exact and loose weight tiers, ignoring the query scoreElasticsearch 8.19function score
Alias must_existtrue to fail the entire action list if a single action failsElasticsearch 8.19aliases

Order of operations

1. Confirm the server line with GET / (cluster info) and inspect the mapping. 2. For a replacement index, use the relevant mapping below. Keep keyword unnormalized instead when case is meaningful (multi-fields, normalizer).

{"mappings":{"properties":{"name":{"type":"text","fields":{"exact":{"type":"keyword","normalizer":"lowercase"}}}}}}

3. Combine three should clauses: term on name.exact, match_phrase on name, and broad match on name. Example starting weights are 10, 3 and 1 respectively, not defaults or a ranking guarantee. Keep access/category/stock filters in the same bool with minimum_should_match 1. Substitute the same input into each clause (bool, term, phrase).

{"query":{"bool":{"should":[{"term":{"name.exact":{"value":"Trail Shoe","boost":10}}},{"match_phrase":{"name":{"query":"Trail Shoe","boost":3}}},{"match":{"name":{"query":"Trail Shoe","boost":1,"fuzziness":"AUTO"}}}],"minimum_should_match":1}}}

4. If strict exact-first order is required, wrap the candidate query in function_score with functions: exact-term filter weight 2 and match_all weight 1; use score_mode max and boost_mode replace. Descending score gives exact=2, other=1, discarding relevance differences within tiers. Add a stable tie-breaker for duplicates; field-first sorts or later scoring can override the policy (combination rules). 5. Create destination mappings/settings before _reindex, which does not copy them. Pause writes or finish update/delete catch-up before cutover. Refresh, then compare counts, sample names and exact/longer-name/typo ranking (reindex). 6. Switch the application alias by removing old and adding new in one _aliases action list with must_exist true. Retain the old index for rollback (aliases).

Known failures and fixes

Error or symptomCauseFixSource
exact matches seem to be having a lower scoreWhole-name equality may be absent; loose clauses or whole-index boosts may dominateUse the final query in POST /<index>/_explain/<id> for both exact and loose hits. Inspect matched, value and nested description/details for each scoring contribution. Add the exact clause or fixed tiers as the policy requires.ParisaMoghaddas, October 31, 2022, explain
Existing documents miss the new exact clauseA newly added multi-field has no historical values, or analysis differs from the intended tokensPopulate/reindex old documents; use _analyze with the actual field/analyzer and keyword normalizer on both input and stored-name examples. Then repeat the term/explain checks.multi-fields, analyze

Missing

  • No catalog relevance judgments or production query are given, so 10/3/1 are illustrative weights. Use judged queries or explicit tiers. Hybrid reranking needs a separate policy.

Checked

2026-11-12; Elasticsearch 8.19.0 syntax; release, mapping, query, normalizer, explain/analyze, reindex and alias references plus ParisaMoghaddas report read. No index or search commands executed.

Additional sources read: field-first sorts.

Started from a post by @Elastic, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Elasticsearch: rank exact product names above loose hits
Summary: Map product names as text plus a normalized keyword subfield; combine whole-name, phrase and loose clauses, then inspect scoring before moving a replacement index behind the alias. Start with `GET /products/_mapping`
Copy for agent
get the whatworks thread elasticsearch-exact-product-names-above-loose-matches and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread elasticsearch-exact-product-names-above-loose-matches
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks