2

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.

Nikolay Vasiliev
  • 5,656
  • 22
  • 31
mhn_namak
  • 453
  • 1
  • 6
  • 18

1 Answers1

3

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.

Amit
  • 30,756
  • 6
  • 57
  • 88
  • 1
    Using _explain, can I count on explanation.value as a score value for that query+doc? Given a doc that would rank lower than another in actual search (querying time), would I get a lower value in explain? Then, that would work. Thanks :). – mhn_namak Apr 20 '20 at 07:07
  • 1
    @mhn_namak, yes given a doc if it comes in your query result, then it would get lower value of `_score` in explain as well and you can try this API in both old and new version of ES to understand the diff b/w tf/idf and bm25, also if you give `DFS Query Then Fetch` and you have multiple shards then also same doc with same query will have different score and rank, also it mention the boosting part as well – Amit Apr 20 '20 at 07:13
  • @mhn_namak, so overall looks like it would work and reveal a lot of info you need :) – Amit Apr 20 '20 at 07:13
  • 1
    @OpsterElasticsearchNinja Explain API [does exactly what the person was asking for](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html#sample-api-desc) + the explanation how this value was computed. The answer makes an impression that this is not possible and is a hack, although it is not a hack. – Nikolay Vasiliev Apr 20 '20 at 17:14
  • _explain is the best path to answering questions like this, afaik. – Jonesome Reinstate Monica Apr 20 '20 at 17:17
  • Sure guys would update my answer tomorrow. It's late here and commenting from my mobile – Amit Apr 20 '20 at 17:28
  • @JonesomeReinstateMonica updated my answer, please go through it and let me know if further medication required – Amit Apr 21 '20 at 09:42
  • @NikolayVasiliev updated my answer, please go through it and let me know if further medication required – Amit Apr 21 '20 at 09:43
  • @NikolayVasiliev , thank you for suggesting and correcting me :), have a great day ahead – Amit Apr 21 '20 at 11:23