elasticsearchscoringrelevance

ElasticSearch: given a document and a query, what is the relevance score?


Once a query is executed on ElasticSearch, a relevance _score is calculated for each retrieved document. Given a specific document (e.g. by doc ID) and a specific query, I would like to see what is its _score?

One way is perhaps to query ES, retrieve all the hit documents, and look up the desired document out of all the retrieved documents to see its score.

I assume there should be a more efficient way to do this. Given a query and a document ID, what is its _score?

I'm using ElasticSearch 7.x

PS: I need this for a learning-to-rank scenario (to create my judgment list). I have in fact a complex query that was created from various should and must over different fields. My major requirement was to get the score value for each individual sub-query, which seems there is no solution for it. I want to understand which part of this complex query is more useful and which one is less. The only way I've come up with is to execute each sub-query separately to get the score but I do not want to actually execute that query just asking for what is the score of a specific document for that sub-query.


Solution

  • Scoring of the document is not only related to just the document and all other documents in the index, but it also depends on various factor like:

    1. _score is calculated per shard basis not on an index basis by default, although you can change this behavior by using DFS Query Then Fetch param in your query. More info on this official blog.
    2. Is there is any boost applied at index or query time(index time is deprecated from 5.X).
    3. Any custom scoring function is used in addition to the default ES scoring algorithm(tf/idf in old versions) and BM25 in the latest versions.

    Edit: Based on the comments from the other respected community members, rephrasing the below statement:

    To answer your question, Using the _explain API, you can understand how Elasticsearch computes a score explanation for a query and a specific document. This can give useful feedback on whether a document matches or didn’t match a specific query.