Skip to the page

Search and Elasticsearch · senior

Writing product search and tuning relevance

A good product search combines a scored full-text query with exact filters and business signals. When ranking looks wrong, ask Elasticsearch why with explain, then change boosts and fields.

The notes

What you must know

  • Mapping for products: name as text with a custom analyzer plus subfields (name.keyword, a search-as-you-type field), brand as keyword with a lowercase normalizer, category as keyword, price as long, in_stock as boolean, dates as date. Use dynamic: strict so stray fields do not create mappings.
  • Query shape: a bool query. Put the scored text query (multi_match with boosts such as name^3) in must, and the exact conditions (category, in stock, price range) in filter. Filters are not scored and can be cached.
  • Synonyms with a synonym_graph filter in the search-time analyzer, loaded from a synonyms set so they can change without a reindex.
  • BM25 rewards terms that appear often (with saturation), appear in short fields (length normalisation), and are rare across the index (IDF). So a cover with a short title and the phone name repeated can beat the phone.
  • Diagnose with explain: true or the _explain API, which show the field and term behind a score.
  • Fixes: boost the title over the description, reward phrase matches with match_phrase in should, choose best_fields or cross_fields on purpose, detect category intent, and add business signals with function_score (sales, stock, rating).
  • Measure with a fixed set of test queries and expected results, so a change that helps one query does not break ten others.

The red pen

Common traps: mistake → fix

  • Mistake: More matches in the description means a better result

    Fix: BM25 also rewards short fields, so a cover can outrank the phone.

  • Mistake: I put every condition in must

    Fix: Exact conditions belong in filter: no scoring and cacheable.

  • Mistake: Boosting fixes every ranking problem

    Fix: Without test queries, a boost for one search breaks others.

Easy to hard

Questions interviewers ask, easy to hard

  1. Which Elasticsearch query types would you use for a product search box?
  2. What is the difference between must and filter in a bool query?
  3. How does BM25 score a document in simple words?
  4. A phone cover ranks above the phone. How do you find out why and fix it?
  5. Write the mapping and query for product search with synonyms, filters and a popularity boost.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview