Advanced Search Relevancy Techniques with Elasticsearch
Improve Elasticsearch relevance by boosting fields and signals with function_score, adding search-time synonyms, combining BM25 with vector search, and measuring every change.
To improve search relevance in Elasticsearch, tune scoring before you add complexity: weight the right fields, boost business signals with function_score, add search-time synonyms, then layer in vector search and Learning to Rank, measuring each change against real queries. This guide walks through each technique with working examples.
Search relevancy is the cornerstone of a great search experience, and most gains come from getting the basics right before reaching for machine learning.
How does Elasticsearch score results?
Elasticsearch uses the BM25 algorithm by default, but you can customize scoring to match your specific needs.
Custom Scoring with Function Score
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "elasticsearch tutorial",
"fields": ["title^2", "content"]
}
},
"functions": [
{
"filter": { "term": { "featured": true } },
"weight": 1.5
},
{
"field_value_factor": {
"field": "popularity_score",
"factor": 1.2,
"modifier": "log1p"
}
}
],
"score_mode": "multiply",
"boost_mode": "multiply"
}
}
}
How do you add synonyms?
Synonyms help capture user intent even when they use different terminology. Elastic recommends applying them at search time with the synonym_graph filter: it handles multi-word synonyms correctly, and you can change the rules without reindexing.
Creating a synonym set
Store the rules with the synonyms API, so you can update them later without touching the index:
PUT _synonyms/my-synonyms
{
"synonyms_set": [
{ "id": "es", "synonyms": "elasticsearch, elastic search" },
{ "id": "db", "synonyms": "database, db, datastore" },
{ "id": "find", "synonyms": "search, find, lookup" }
]
}
Using it in a search analyzer
{
"settings": {
"analysis": {
"filter": {
"my_synonyms": {
"type": "synonym_graph",
"synonyms_set": "my-synonyms",
"updateable": true
}
},
"analyzer": {
"my_search_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "my_synonyms"]
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "my_search_analyzer"
}
}
}
}
Updating the synonym set through the API reloads the analyzers that use it automatically.
How do you expand and weight queries?
1. Multi-Match Queries
Use different match types for various scenarios:
{
"query": {
"multi_match": {
"query": "machine learning",
"fields": ["title^3", "tags^2", "content"],
"type": "best_fields",
"tie_breaker": 0.3
}
}
}
2. Boosting Recent Content
Give preference to newer content:
{
"query": {
"function_score": {
"query": { "match_all": {} },
"functions": [
{
"gauss": {
"date": {
"origin": "now",
"scale": "30d",
"decay": 0.5
}
}
}
]
}
}
}
How do you personalize search results?
User Behavior Tracking
Track user interactions to improve relevancy:
- Click-through rates
- Dwell time
- Conversion rates
- User preferences
Implementing Personalized Search
The {{...}} placeholders below are filled in by a search template (or your application) with the user's query and profile:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{ "match": { "content": "{{query}}" } }
],
"should": [
{ "terms": { "category": ["{{user_interests}}"] } }
]
}
},
"functions": [
{
"filter": { "terms": { "author": ["{{followed_authors}}"] } },
"weight": 1.3
}
]
}
}
}
How do you test relevancy changes?
Implement A/B testing to measure relevancy improvements:
- Define metrics: Click-through rate, conversion rate, user satisfaction
- Create variants: Different scoring algorithms or query structures
- Measure results: Statistical significance and business impact
- Iterate: Continuously improve based on results
What are the advanced techniques?
1. Learning to Rank
Learning to Rank (available since Elasticsearch 8.12, on certain subscription levels) uses a trained model to re-rank the top results of a normal query:
- Collect judgment data from user interactions or human ratings
- Define features as templated queries (for example, BM25 scores on different fields) plus custom signals
- Train a gradient boosted tree model such as XGBoost's
XGBRankeroutside Elasticsearch, and upload it with Eland - Apply it as a rescorer on the top N hits of each shard
{
"query": {
"multi_match": {
"fields": ["title", "content"],
"query": "the quick brown fox"
}
},
"rescore": {
"learning_to_rank": {
"model_id": "ltr-model",
"params": { "query_text": "the quick brown fox" }
},
"window_size": 100
}
}
2. Semantic Search
Leverage vector search for semantic understanding. For production workloads, use approximate kNN on a dense_vector field rather than a script_score query, which scores every matching document and does not scale to large indices (it is still useful for exact scoring over a small, pre-filtered subset):
{
"knn": {
"field": "content_vector",
"k": 10,
"num_candidates": 100,
"query_vector_builder": {
"text_embedding": {
"model_id": "my-text-embedding-model",
"model_text": "elasticsearch tutorial"
}
}
}
}
Combining this with a BM25 query gives you hybrid search. How the two scores get combined matters more than most teams expect; we measured it in Hybrid search scoring, explained.
How do you monitor relevancy?
Track key metrics:
- Query performance: Response times and throughput
- User engagement: Click-through rates and session duration
- Business metrics: Conversion rates and revenue impact
- Search quality: Manual relevancy assessments
Best Practices
- Start simple: Begin with basic relevancy tuning before implementing complex features
- Use analytics: Leverage search analytics to understand user behavior
- Test continuously: Regular A/B testing ensures ongoing improvement
- Monitor performance: Balance relevancy improvements with query performance
- Gather feedback: Collect user feedback to validate relevancy improvements
Conclusion
Advanced search relevancy requires a combination of technical expertise and user understanding. By implementing these techniques and continuously measuring their impact, you can create a search experience that truly serves your users' needs.
Sources
- Elastic documentation, Search with synonyms
- Elastic documentation, Synonym graph token filter
- Elastic documentation, Learning To Rank
- Elastic documentation, Search using Learning To Rank
- Elastic documentation, kNN search
Abhilash B is an advisor at Query Quotient and an information retrieval and AI expert who holds patents in the field. Abhilash brings that retrieval depth to the search, RAG and agent systems Query Quotient builds for clients.
