Why relevance scoring ranks results in Elasticsearch - Performance Analysis
When Elasticsearch ranks search results by relevance, it calculates scores for each document. Understanding how the time to compute these scores grows helps us see how search speed changes as data grows.
We want to know: how does scoring time increase when more documents or terms are involved?
Analyze the time complexity of this Elasticsearch query snippet that ranks documents by relevance score.
GET /my_index/_search
{
"query": {
"match": {
"content": "search terms here"
}
}
}
This query searches the "content" field for matching terms and calculates a relevance score for each matching document to rank results.
Look at what repeats when Elasticsearch scores results:
- Primary operation: Calculating relevance score for each matching document.
- How many times: Once per matching document, for all terms in the query.
As the number of matching documents grows, scoring each one takes more time. Also, more query terms mean more calculations per document.
| Input Size (n = matching docs) | Approx. Operations |
|---|---|
| 10 | 10 x t |
| 100 | 100 x t |
| 1000 | 1000 x t |
Pattern observation: The work grows roughly in direct proportion to the number of matching documents and query terms.
Time Complexity: O(n x t)
This means the time to rank results grows linearly with the number of matching documents (n) and the number of query terms (t).
[X] Wrong: "Scoring happens only once regardless of how many documents match."
[OK] Correct: Each matching document must be scored separately to compare relevance, so scoring time grows with matches.
Knowing how scoring time grows helps you explain search performance clearly. It shows you understand how Elasticsearch handles ranking behind the scenes, a useful skill for real projects.
"What if we added filters that reduce matching documents before scoring? How would that affect the time complexity?"