Quick revision
The short answer
I would start from BM25 text relevance and combine it with business signals through function_score or script_score, using dampened functions such as log1p of popularity so no signal can overwhelm relevance. Hard rules such as 'never show unavailable products' belong in filters, not scores. Boosts stay few, documented and tested with judged query sets and A/B experiments; learning to rank can combine many features later if we have training data and monitoring; and an explain view lets support teams see why a product ranks where it does, with limits that keep margin boosts from hurting user trust.
What to remember
In this answer, we designed a ranking that starts from BM25 and adds business signals through function_score or script_score with dampened functions like log1p and a max_boost cap. Availability is a filter, not a score, margin is a small tie-breaker guarded by conversion metrics, every boost is documented and tested with judged query sets and A/B experiments, learning to rank comes in when signals multiply, and an explain view keeps the ranking understandable for support teams.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would start from BM25 text relevance and combine it with business signals through function_score or script_score, using dampened functions such as log1p of popularity so no signal can overwhelm relevance. Hard rules such as 'never show unavailable products' belong in filters, not scores. Boosts stay few, documented and tested with judged query sets and A/B experiments; learning to rank can combine many features later if we have training data and monitoring; and an explain view lets support teams see why a product ranks where it does, with limits that keep margin boosts from hurting user trust.
Why this question is asked
Every commercial search engine faces this request. Text relevance alone puts a perfect match that nobody buys above a bestseller, and the business is right that popularity and stock matter. But if every department adds its own boost, the ranking becomes a tangle nobody can explain, and users notice when results feel bought. Interviewers ask this scenario to see whether we can combine signals in a principled, measurable and explainable way. In this answer, we will separate hard rules from soft signals, combine relevance with popularity using function_score, handle margin carefully, set up testing, consider learning to rank, and keep the ranking explainable.
1Hard Rules Are Filters, Not Scores
The first design decision is to separate rules from preferences. 'Do not show products that cannot be bought' is a rule, so it belongs in a filter clause of a bool query. A filter removes documents before scoring, is cached, and cannot be outweighed by a strong text match. If we instead subtracted score for being out of stock, a very relevant unavailable product could still rank above available ones, and users would click into dead ends.
Softer versions are also filters plus ordering: if out-of-stock items should appear only at the end, we can sort by an availability flag first and score second, or run a second query when the first returns too few results. Using scores for rules makes every other boost harder to reason about.
2Combining Relevance With Popularity
BM25 gives a text relevance score, roughly between 0 and 20 for typical queries. Popularity, such as sales in the last 30 days, might range from 0 to 50,000. Multiplying the two raw values would make popularity dominate everything. So we dampen the signal with a logarithm, which grows quickly at first and then flattens, and combine it with the text score.
The log1p modifier in field_value_factor is log10(1 + value), so 10 times more sales adds one step, not ten times the score. The first function, a plain weight of 1, is a base so that a product with no sales keeps its text score instead of being multiplied by zero, and score_mode sum adds the functions together before boost_mode multiplies the result with the text score. max_boost caps the combined function value, so no product can get more than five times its text score from business signals. script_score offers the same with a custom formula, for example a saturation function such as sales / (sales + 100), which approaches 1 and never runs away, much like BM25's own term frequency.
3Treating Margin With Care
Margin is the most dangerous signal because it serves the company, not the user. A strong margin boost pushes expensive, less relevant items up, users learn that the first results are not the best ones, and trust, conversion and repeat visits fall. That loss is real but slower to see than the margin gain.
So I would keep the margin boost small, a tie-breaker between similarly relevant products like the 0.1 bonus above, apply it only within relevant results, and require an A/B test that measures conversion and revenue per search, not margin alone. If the test shows conversion dropping, the boost goes. Writing this policy down in advance prevents the boost from growing quietly over time.
4Keeping Boosts Few, Documented and Tested
Each boost should have an owner, a reason, a documented formula and a test result. A short ranking document listing them, such as 'popularity: log1p of 30-day sales; margin: +0.1 above 30 percent; owner: search team', makes reviews possible. Every change runs first against a judged query set, real queries with graded results, and is measured with a metric such as NDCG@10, and then goes into an online A/B test that compares click-through, conversion and revenue per search.
- Filtersin stock, sellable in the region, policy rules
- Text relevanceBM25 on title, description, brand
- Business signalsfunction_score: popularity log1p, small margin factor
- Optional rerankinglearning to rank on the top 100 results
- Explain viewper result: text score, each boost, final score
5 layers, from the top down. Step through them one at a time.
5When to Move to Learning to Rank
With three or four signals, hand-tuned functions are manageable. With twenty, such as freshness, ratings, return rates, personalisation and price fit, hand tuning becomes guesswork. Learning to rank trains a model on judged queries and click data to combine features, and runs it as a rescorer over the top results. Plugins exist for Elasticsearch and OpenSearch, and recent Elasticsearch versions have built-in support (check your version). The cost is a training data pipeline, model versioning and monitoring for drift and bias. I would adopt it once the signal count and the traffic justify that investment, with the same A/B discipline.
6Keeping the Ranking Explainable
Support teams and merchants will ask 'why is this product third?'. An explain view answers it. Elasticsearch's explain: true returns the full score calculation, but it is too detailed for most readers, so I would build a small internal page that shows, for each result, the text score, each boost factor and the final score. Named queries help label which clauses matched. That view also catches mistakes early, such as a boost applied to the wrong field, and gives the business confidence that ranking follows the documented rules.
7Follow-up questions
Why not just sort by popularity after filtering by relevance?
A sort ignores how relevant each match is: a barely matching bestseller would beat a perfect match. Combining scores keeps relevance in charge and lets popularity decide between similarly relevant products.
What about new products with no sales?
log10(1 + 0) is 0, so on its own the popularity function would give them nothing; the base weight of 1 in the sum keeps their factor at 1, so text relevance alone decides. Many teams add a small, time-limited freshness boost so new products get a chance to collect clicks.
How do we stop popularity from creating a rich-get-richer loop?
Dampen it, cap it with max_boost, measure it over a window rather than all time, and give new items some exposure. Monitoring how concentrated clicks become over time shows whether the loop is getting worse.
8Conclusion
In this answer, we designed a ranking that starts from BM25 and adds business signals through function_score or script_score with dampened functions like log1p and a max_boost cap. Availability is a filter, not a score, margin is a small tie-breaker guarded by conversion metrics, every boost is documented and tested with judged query sets and A/B experiments, learning to rank comes in when signals multiply, and an explain view keeps the ranking understandable for support teams.
Open with
I would keep text relevance as the base, BM25 on the main fields, and add business signals on top in a controlled way. First, anything that is a rule, like products that are unavailable, goes into a filter, not the score, so no strong text match can bring them back.
Then, if they want more
Then I would use function_score, or script_score, to combine relevance with popularity, but dampened: field_value_factor on 30-day sales with log1p, so ten times the sales adds one step rather than ten times the score, plus a max_boost cap so business signals can at most multiply the text score by a fixed amount.
Margin I would treat carefully: a small bonus, like 0.1 on top of a base of 1, that acts as a tie-breaker between similar matches, because a heavy margin boost pushes less relevant products up and damages user trust and conversion.
Each boost would be few, documented with an owner and formula, tested offline on a judged query set with NDCG, and then in an A/B test on conversion and revenue per search.
If the number of signals grows a lot, learning to rank becomes worth it, as a rescorer over the top results, but it needs training data, versioning and monitoring. Finally, I would build an explain view for support and merchants that shows each result's text score, each boost and the final score, using the explain API underneath.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Elasticsearch documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Business wants search results ranked by relevance but also boosted by popularity, stock...": https://scrivio-interview-library.netlify.app/databases/search-engines/7f17de281f/