Add popularity and freshness while preserving textual relevance.
course-products examples use the teaching mapping and product from chapter 5, “Mapping and field types”. Exercise walkthroughs below use the trainer’s separate public dataset.
- Candidates and preferences
- Functions and normalization
- Scripts and a second stage
Add popularity and freshness while preserving textual relevance.
The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.
Candidates and preferences
First establish mandatory constraints such as access, stock and category. Rank eligible candidates afterward. A popular product in the wrong category should not appear merely because of a large business signal. Every signal has a range and distribution: sales counts from zero to a million should not be blindly added to text scores.
Check your understandingFunctions and normalization
function_score combines the original score and functions through boost_mode and score_mode. field_value_factor uses numeric fields; logarithmic modifiers compress large ranges but need valid inputs. Decay functions decrease contributions with distance from dates, numbers or locations. rank_feature is designed for prepared positive signals and needs its own mapping.
Check your understandingScripts and a second stage
script_score is flexible but expensive across many candidates; scores must be valid non-negative numbers. Rescore applies expensive ranking to a top window on each shard, so a small window limits possible improvements. Check not only the first product but also rare queries and missing feature values.
Multiplication combines text score and rating; validate rating ranges and missing values.
{
"query": {
"function_score": {
"query": {
"match": {
"name": "headphones"
}
},
"field_value_factor": {
"field": "rating",
"modifier": "sqrt",
"missing": 1
},
"boost_mode": "multiply"
}
}
}How to read this result
Multiplication combines text score and rating; validate rating ranges and missing values.
Catalog reasoning example
With text scores 8 and 6 and sales 10 and 100000, naive multiplication overwhelmingly favors the second product. Logarithmic or saturation transformations compress the range, and bounded influence preserves strong text evidence. Do not tune from two numbers: inspect the sales distribution, missing history, new products and exact-model queries.
Check your understandingRelevance under constraints
Separate three responsibilities: must describes text relevance, filter describes eligibility, and must_not describes exclusions. Our example searches travel using name^3 and description, requires stock and price below 800, then excludes preorder. Keep multi_match in the scored must path rather than inside a local or outer filter.
Do not add a field sort when the goal is a relevance ranking. A boost placed on an unrelated clause or inside an aggregation does not give the actual text query the intended weighting. Inspect the complete matching set and the relative order of documents rather than hardcoding the numeric score of one hit.
Try a different scenario
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "travel",
"fields": [
"name^3",
"description"
]
}
}
],
"filter": [
{
"term": {
"in_stock": true
}
},
{
"range": {
"price": {
"lt": 800
}
}
}
],
"must_not": [
{
"term": {
"status": "preorder"
}
}
]
}
},
"size": 2,
"_source": [
"name",
"price",
"status",
"in_stock"
]
}Elasticsearch response
{
"hits": {
"total": {
"value": 34,
"relation": "eq"
},
"hits": [
{
"_id": "23",
"_score": 4.8997154,
"_source": {
"name": "Dell Travel Speaker 23",
"price": 100,
"in_stock": true,
"status": "discontinued"
}
},
{
"_id": "53",
"_score": 4.8997154,
"_source": {
"name": "Dell Travel Speaker 53",
"price": 100,
"in_stock": true,
"status": "discontinued"
}
}
]
}
}How to read this result
The text query determines relative ranking among eligible, non-excluded products. Stock, price and exclusions determine membership, not a relevance bonus.
Check your understanding
Answer before expanding each explanation.
1. Where does access control belong?
In mandatory server-side eligibility constraints.
2. Why log-transform sales?
To prevent very large values from dominating other signals.
3. What does boost_mode control?
How function output combines with the original score.
4. Does rescore see every document?
No, only a selected candidate window per shard.
5. How do you validate a new weight?
Compare metrics and regressions on judged queries.
Key takeaways
- In mandatory server-side eligibility constraints.
- To prevent very large values from dominating other signals.
- How function output combines with the original score.
- No, only a selected candidate window per shard.
- Compare metrics and regressions on judged queries.
Official reference
Ready to put this into practice?
Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.