Search and Elasticsearch learning path Level 3 This level · 7 of 15
Hard search elasticsearch

How do you add business signals to ranking without paying for them on every match?

The 30 second answer

function_score runs its functions on every matching document, so I keep them cheap: field_value_factor on a precomputed popularity field, filter-plus-weight boosts, no scripts. Anything heavier goes in a rescore block, which re-ranks only the top window_size hits per shard, default 10. Filters that shouldn't score go in filter or constant_score, and track_total_hits stays at its default instead of exact counts.

Could you say this in an interview?

The problem. Pure text relevance (BM25) doesn't know that a product sells well, has stock in all sizes, or is new. Shops blend those signals in. The question is where in the pipeline you pay for them.

Cost model. The query matches maybe a million documents. Anything that runs per match runs a million times on each search. Anything that runs only on the top results runs a few hundred times.

Tools, cheapest first.

  1. **Filter context and constant_score.** No score computed. The docs: constant_score "wraps a filter query and returns every matching document with a relevance score equal to the boost parameter value".
  2. **function_score with simple functions.** field_value_factor reads a numeric field from doc values; a function with a filter and a weight boosts matching documents. score_mode (default multiply) combines functions, boost_mode combines them with the query score. This runs for every match, so keep it to doc-values reads.
  3. **rescore.** The docs: "All rescores have the window_size parameter that controls how many top documents will be considered for rescoring. The default is 10." The rescore runs on each shard before results go to the coordinating node. An explicit sort other than _score descending throws an error.
  4. A separate re-ranking service on the top few hundred candidates, for ML models. Zalando's 2024 ranking-platform paper describes a candidate-generation stage followed by ranking.

Badly written. A script computing a blended score for every match:

{ "query": { "script_score": {
    "query": { "match": { "name": "dress" } },
    "script": { "source": "_score * Math.log(2 + doc['popularity'].value) * (doc['in_stock'].value ? 1.2 : 0.5)" } } } }

Better. Cheap functions in function_score, the expensive phrase check only on the top window:

GET products/_search
{ "size": 48,
  "query": { "function_score": {
    "query": { "bool": {
      "must":   [ { "match": { "name": "summer dress" } } ],
      "filter": [ { "term": { "market": "de" } } ] } },
    "functions": [
      { "field_value_factor": { "field": "popularity", "modifier": "log1p", "missing": 0 } },
      { "filter": { "term": { "in_stock": true } }, "weight": 1.2 } ],
    "score_mode": "multiply", "boost_mode": "multiply" } },
  "rescore": { "window_size": 200,
    "query": { "rescore_query": { "match_phrase": { "name": { "query": "summer dress", "slop": 2 } } },
               "query_weight": 1.0, "rescore_query_weight": 1.5 } } }

**track_total_hits.** The search API default is 10,000: Elasticsearch counts exactly up to that number and then reports "at least". true asks for an exact count "at the cost of some performance". false skips the count and lets Lucene skip non-competitive documents more freely. A shop rarely needs "1,234,567 results"; "10,000+" is enough.

Trade-offs.

  • Rescoring only the top window means a product just outside the window can never be promoted. Pick the window from data, and keep it the same across pages; the docs warn that changing it while paging can shift the top hits.
  • Multiplying signals can let popularity overwhelm relevance. Use log1p or similar dampening and evaluate offline and online.
  • Signals change often (stock, sales). Keep them as separate numeric fields updated on a fast lane.

Alternate interview scenarios

"Latency rose after adding personalization boosts to the query."

If the boosts depend on the user, they're computed per match and they also defeat result caching. Move them to rescore or to a re-ranking service on the top candidates, and keep the shared part of the query cacheable.

"Product wants exact result counts on every page."

Explain the cost, offer "10,000+" or an approximate count, and give exact counts only where a business process needs them (exports, merchandising tools).

Sources (5)
  1. Elasticsearch docs: Function score query (score_mode, boost_mode, field_value_factor)elastic.co
  2. Elasticsearch docs: Rescore search results (window_size)elastic.co
  3. Elasticsearch docs: Constant score queryelastic.co
  4. Elasticsearch docs: Search API (track_total_hits)elastic.co
  5. arXiv 2409.02856: Building a Scalable, Effective, and Steerable Search and Ranking Platform (Zalando, 2024)arxiv.org
Previous
Next