# BM25 score of two indexed document in elasticsearch

**URL:** <https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289>\
**Category:** Elasticsearch\
**Created:** [January 27, 2018, 8:30am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289 "2018-01-27T08:30:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [January 27, 2018, 8:30am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/1 "2018-01-27T08:30:53Z")

</div>

I have an index with two type t1 and t2. a few documents has been index in each of them with following structure:

```
{
  "mappings": {
    "t1": {
      "properties": {
        "body": {
          "type": "string"
        }
      }
    },
    "t2": {
      "properties": {
        "body": {
          "type": "string"
        }
      }
    }
  }
}

```

how can I obtain BM25 score between two arbitrary documents D1 and D2. please note that I don't want to find Documents similar to D1 or D2. so I think more\_like\_this query is not proper for my case.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 27, 2018, 8:42am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/2 "2018-01-27T08:42:47Z")

</div>

Note that multiple types is not allowed anymore in 6.x.

---

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [January 27, 2018, 8:54am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/3 "2018-01-27T08:54:41Z")

</div>

I don't knew that, thank you! but it is not important right now, because as you see structure of documents are same. I can store my documents in one type.  
are you have any solution for main problem?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 27, 2018, 9:10am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/4 "2018-01-27T09:10:17Z")

</div>

You can just obtain individual scores and compare on client side I think.

What do you want to do with that score exactly?

---

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [January 27, 2018, 9:14am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/5 "2018-01-27T09:14:40Z")

</div>

how can I do that?!  
BM25 has been defined as a score between a Query and a document, such that the query can be a document itself. am I right?

I want use this score as a feature for a learning to rank application.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 27, 2018, 10:47am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/6 "2018-01-27T10:47:35Z")

</div>

You mean getting a score when there is no query?

I don't know.

May be others have ideas.

If you extract all the values you have in document A then build a bool query with should clauses for each field, may be you can call [https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-explain.html) and get that?

But I'm unsure here if it would help.

---

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [January 27, 2018, 10:58am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/7 "2018-01-27T10:58:28Z")

</div>

"You mean getting a score when there is no query?"

exactly, The original BM25 formula can be used to calculate the score between each arbitrary documents. (query also can be consider as a document)

"If you extract all the values you have in document A then build a bool query with should clauses for each field, may be you can call [https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-explain.html) and get that?"

I don't think this is works. because the formula is a sum of aggregated weight on common words between two documents.

I can write a code that calculate this score. it's not hard, but I thought may be it's implemented in elasticsearch in optimal way.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 27, 2018, 10:59am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/8 "2018-01-27T10:59:58Z")

</div>

I dunno. @jimczi or @jpountz might have ideas?

---

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [January 28, 2018, 5:53am UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/9 "2018-01-28T05:53:07Z")

</div>

for calculating BM25 by myself, I need length of document and average document length on whole documents in collection. according to formula, length of a document is number of it's word. I search a lot, but no success to find a way to elastic return this values to me.  
could you please give me a solution?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [February 1, 2018, 2:25pm UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/10 "2018-02-01T14:25:12Z")

</div>

There is no built-in way to do this. One way would be to build a giant disjunction out of D1 and run it against D2.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 1, 2018, 2:25pm UTC](https://discuss.elastic.co/t/bm25-score-of-two-indexed-document-in-elasticsearch/117289/11 "2018-03-01T14:25:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
