0
0
ElasticsearchConceptBeginner · 3 min read

What is Relevance Score in Elasticsearch: Explanation and Example

In Elasticsearch, the relevance score is a number that shows how well a document matches a search query. It helps rank search results so the most relevant documents appear first.
⚙️

How It Works

Imagine you are searching for a book in a library. Some books match your search better than others. Elasticsearch uses a relevance score to measure this match quality. The higher the score, the better the document fits your search.

Elasticsearch calculates this score using factors like how often the search words appear in the document, how rare those words are across all documents, and how long the document is. This is similar to how a librarian might judge which books are most useful based on your keywords.

The score helps Elasticsearch sort results so you see the most useful documents first, making your search faster and more accurate.

💻

Example

This example shows a simple Elasticsearch query that searches for the word "apple" in a field called "title" and returns the relevance score for each matching document.

json
{
  "query": {
    "match": {
      "title": "apple"
    }
  }
}
Output
[ { "_id": "1", "_score": 1.234, "_source": {"title": "Apple fruit benefits"} }, { "_id": "2", "_score": 0.789, "_source": {"title": "Apple pie recipe"} } ]
🎯

When to Use

Use the relevance score whenever you want to rank search results by how well they match the query. This is essential for full-text search applications like product search, document search, or any system where users expect the best matches first.

For example, an online store uses relevance scores to show the most relevant products when a customer searches for "running shoes." A news website uses it to rank articles by how closely they match the search terms.

Key Points

  • The relevance score is a numeric value showing how well a document matches a query.
  • It is calculated using factors like term frequency and inverse document frequency.
  • Higher scores mean better matches and appear higher in search results.
  • It helps users find the most useful documents quickly.

Key Takeaways

Relevance score ranks documents by how well they match the search query.
It is calculated based on term frequency, rarity, and document length.
Higher scores mean more relevant results shown first.
Use relevance scores to improve search result quality in applications.