# Match\_phrase score calculation

**URL:** https://discuss.elastic.co/t/match-phrase-score-calculation/171243
**Category:** Elasticsearch
**Created:** [March 7, 2019, 7:27am UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243 "2019-03-07T07:27:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kkendzia](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@kkendzia](https://discuss.elastic.co/u/kkendzia)
#### Post date: [March 7, 2019, 7:27am UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/1 "2019-03-07T07:27:19Z")

</div>

Hi,  
we are currently working with elasticsearch 6.6 and we have troubles with match\_phrase and the calculated score.

**Whats the setting**  
similarity setting is at the moment this:  
"scripted\_wc": {  
"type": "scripted",  
"script": {  
"source": "return doc.freq;"  
}  
}

**What do we want to have**  
For example we have 3 Documents:  
"bbb aaa aaa bbb aaa",  
"aaa ccc aaa bbb",  
"ccc aaa aaa bbb aaa"

When we query for "aaa", the scores are 3, 2 and 3. This is correct. But if we search for "ccc aaa" we get the scores 0, 2 and 2. Our expectation is to get 0, 1, 1. When we enable explain, we see that for every added word it creates a subquery which returns 1 but sums up. So "ccc aaa aaa" return 0, 0, 3 instead of 0, 0, 1.  
"bbb aaa" should return 2, 0, 1 for each document (0 of course wouldnt be returned).

Is this a bug or is our understanding that wrong?

Thanks for the help

---

<div class="post-metadata">

### Author: ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)
#### Post date: [March 7, 2019, 12:31pm UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/2 "2019-03-07T12:31:27Z")

</div>

> Is this a bug or is our understanding that wrong?

That's the current design, for phrases the script is executed on each individual term and the `doc.freq` is the frequency of the phrase within the document. BM25 similarity is a bit special, it multiplies the idf of each term and then use it to score the phrase once. The other similarities are consistent with the scripted similarity, they just sum the score of each individual term to produce a score that is comparable with a boolean query on the same terms.

---

<div class="post-metadata">

### Author: ![kkendzia](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@kkendzia](https://discuss.elastic.co/u/kkendzia)
#### Post date: [March 7, 2019, 1:05pm UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/3 "2019-03-07T13:05:08Z")

</div>

Well, is there a possibility to say that "ccc aaa" for example is a single term and not 2

---

<div class="post-metadata">

### Author: ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)
#### Post date: [March 7, 2019, 1:24pm UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/4 "2019-03-07T13:24:10Z")

</div>

Not with the current API, no. If you know the number of terms in the phrase you could use a custom `boost` to change the score of the phrase query ? We could also change the API to have all terms available when scoring a phrase but I am not sure how the API would look like. If you're interested, can you open an issue in github that explains the current limitation ?

---

<div class="post-metadata">

### Author: ![kkendzia](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@kkendzia](https://discuss.elastic.co/u/kkendzia)
#### Post date: [March 11, 2019, 6:27am UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/5 "2019-03-11T06:27:08Z")

</div>

Thanks for your suggestion. We did it now like you said:

doc.freq \* query.boost - where query.boost is 1/count(words in query).

But I will create an issue at github too.

---

<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: [April 8, 2019, 6:27am UTC](https://discuss.elastic.co/t/match-phrase-score-calculation/171243/6 "2019-04-08T06:27:09Z")

</div>

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