![]() |
VOOZH | about |
Elasticsearch is a powerful search engine that good at full-text search among other types of queries. One of its key features is the ability to rank search results based on relevance. Relevancescoring determines how well a document matches a given search query and ensures that the most relevant results appear at the top.
In this article, we will understand relevance scoring in Elasticsearch with detailed examples and outputs to make the concepts simple and easy to learn.
To understand about the Relevance Scoring and Search Relevance in Elasticsearch we will consider below products collection as shown as below:
[
{
"title": "Wireless Headphones",
"description": "High-quality wireless headphones with noise-canceling technology.",
"price": 99.99,
"popularity": 100
},
{
"title": "Smartphone",
"description": "A powerful smartphone with a high-resolution display.",
"price": 499.99,
"popularity": 200
},
{
"title": "Laptop",
"description": "Thin and light laptop with long battery life.",
"price": 899.99,
"popularity": 150
},
{
"title": "Smart Watch",
"description": "Fitness tracker with heart rate monitor and GPS.",
"price": 199.99,
"popularity": 75
},
{
"title": "Tablet",
"description": "10-inch tablet with quad-core processor.",
"price": 299.99,
"popularity": 120
}
]
Let's start with a simple example using a match query to see how relevance scoring works.
Let's Retrieve all products with a description containing the term "smartphone."
GET /products/_search
{
"query": {
"match": {
"description": "smartphone"
}
}
}
Output:
"hits" : [
{
"_id" : "2",
"_source" : {
"title" : "Smartphone",
"description" : "A powerful smartphone with a high-resolution display.",
"price" : 499.99,
"popularity" : 200
}
}
]
Explanation: This query searches for documents in the "products" index where the "description" field contains the term "smartphone." It retrieves all documents that match this criteria
Let's Search for products with either "smartphone" or "tablet" in the title or description, giving more weight to matches in the title
GET /products/_search
{
"query": {
"multi_match": {
"query": "smartphone tablet",
"fields": ["title^2", "description"]
}
}
}
Output:
"hits" : [
{
"_id" : "2",
"_source" : {
"title" : "Smartphone",
"description" : "A powerful smartphone with a high-resolution display.",
"price" : 499.99,
"popularity" : 200
}
},
{
"_id" : "5",
"_source" : {
"title" : "Tablet",
"description" : "10-inch tablet with quad-core processor.",
"price" : 299.99,
"popularity" : 120
}
}
]
Explanation:This query searches for documents in the "products" index where either the "title" or "description" field contains the terms "smartphone" or "tablet." It gives more weight to matches in the "title" field (by using the ^2 notation) compared to matches in the "description" field
Let's Retrieve all products, boosting their relevance based on the popularity of each product. The popularity is used as a factor in the relevance score calculation, with a square root modifier to moderate the boost effect.
GET /products/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"boost_mode": "sqrt",
"functions": [
{
"field_value_factor": {
"field": "popularity",
"factor": 1.2,
"modifier": "sqrt"
}
}
]
}
}
}
Output:
"hits" : [
{
"_id" : "2",
"_source" : {
"title" : "Smartphone",
"description" : "A powerful smartphone with a high-resolution display.",
"price" : 499.99,
"popularity" : 200
},
"_score" : 14.142136
},
{
"_id" : "3",
"_source" : {
"title" : "Laptop",
"description" : "Thin and light laptop with long battery life.",
"price" : 899.99,
"popularity" : 150
},
"_score" : 12.247448
},
{
"_id" : "5",
"_source" : {
"title" : "Tablet",
"description" : "10-inch tablet with quad-core processor.",
"price" : 299.99,
"popularity" : 120
},
"_score" : 10.954451
},
{
"_id" : "1",
"_source" : {
"title" : "Wireless Headphones",
"description" : "High-quality wireless headphones with noise-canceling technology.",
"price" : 99.99,
"popularity" : 100
},
"_score" : 10
},
{
"_id" : "4",
"_source" : {
"title" : "Smart Watch",
"description" : "Fitness tracker with heart rate monitor and GPS.",
"price" : 199.99,
"popularity" : 75
},
"_score" : 8.6602545
}
]
Explanation:This query retrieves all documents in the "products" index, boosting their relevance based on the "popularity" field of each document. The "popularity" field is used as a factor in the relevance score calculation, with a square root modifier to moderate the boost effect
Understanding relevance scoring and search relevance in Elasticsearch is crucial for building effective search applications. By understanding the concepts and techniques discussed in this article you can improve the quality and relevance of your search results and ensuring that users find the most relevant information quickly and easily.
Remember, relevance scoring is an iterative process. Continuously monitor, analyze, and adjust your search configurations to adapt to changing data and user behavior.