# Adjust score based on the number of matches in Boolean filter

**URL:** https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716
**Category:** Elasticsearch
**Created:** [February 15, 2012, 2:43pm UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716 "2012-02-15T14:43:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dragan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dragan/32/2943_2.png) [@dragan](https://discuss.elastic.co/u/dragan)
#### Post date: [February 15, 2012, 2:43pm UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/1 "2012-02-15T14:43:10Z")

</div>

Hi,

I have the following setup:  
Every document in ES has a field of type string, that contains a list  
of  
items

basket : ["yuca", "potato"]

and a field of type long called price

price: 626

I'm adjusting the score of my search results based on the difference  
between  
the price field and the price I search for.  
I use CustomScoreQuery and I pass this mvel script to it:

floor(doc.price.value/100)

I am using a Boolean filter to get a partial match on the basket  
field. I'm  
using pyes (0.16.0), so under the hood the filter looks like this:

'bool':{  
'minimum\_number\_should\_match':1,  
'should':[  
{  
'term':{'basket':'yuca'}  
},  
{  
'term':{'basket':'yam'}  
}  
]  
}

Here's what I'm trying to do:  
I want to be able to adjust the score when I get a partial match on  
the  
basket field.

In this example I asked for 'yuca' and 'yam' and the result contains  
'yuca'  
and 'potato' (so only 'yuca' is a match). I want to penalise the score  
by  
one point for each missed term.

In this case the price contribution to the score is 6 and the basket  
penalty  
should be 1, so the overall score for this should be 5.

I prefer to handle this logic in the script that I pass to  
CustomScoreQuery,  
but I'm open to other solutions as well.

Thank you

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [February 15, 2012, 7:43pm UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/2 "2012-02-15T19:43:00Z")

</div>

When you use should clauses in a bool query, the more matches you will have, the higher the score will be, so it should work for you.

On Wednesday, February 15, 2012 at 4:43 PM, Dragan wrote:

> Hi,
> 
> I have the following setup:  
> Every document in ES has a field of type string, that contains a list  
> of  
> items
> 
> basket : ["yuca", "potato"]
> 
> and a field of type long called price
> 
> price: 626
> 
> I'm adjusting the score of my search results based on the difference  
> between  
> the price field and the price I search for.  
> I use CustomScoreQuery and I pass this mvel script to it:
> 
> floor(doc.price.value/100)
> 
> I am using a Boolean filter to get a partial match on the basket  
> field. I'm  
> using pyes (0.16.0), so under the hood the filter looks like this:
> 
> 'bool':{  
> 'minimum\_number\_should\_match':1,  
> 'should':[  
> {  
> 'term':{'basket':'yuca'}  
> },  
> {  
> 'term':{'basket':'yam'}  
> }  
> ]  
> }
> 
> Here's what I'm trying to do:  
> I want to be able to adjust the score when I get a partial match on  
> the  
> basket field.
> 
> In this example I asked for 'yuca' and 'yam' and the result contains  
> 'yuca'  
> and 'potato' (so only 'yuca' is a match). I want to penalise the score  
> by  
> one point for each missed term.
> 
> In this case the price contribution to the score is 6 and the basket  
> penalty  
> should be 1, so the overall score for this should be 5.
> 
> I prefer to handle this logic in the script that I pass to  
> CustomScoreQuery,  
> but I'm open to other solutions as well.
> 
> Thank you

---

<div class="post-metadata">

### Author: ![dragan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dragan/32/2943_2.png) [@dragan](https://discuss.elastic.co/u/dragan)
#### Post date: [February 15, 2012, 10:21pm UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/3 "2012-02-15T22:21:51Z")

</div>

Thanks Shay, that works great when I want to see the results sorted by  
score.

I'm trying to use score as a percentage match metric. Is there a way to set  
the score based on the number of matches in the Boolean query? I'd like to  
be able to tune the penalty function for imperfect matches.

On Wed, Feb 15, 2012 at 2:43 PM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> When you use should clauses in a bool query, the more matches you will  
> have, the higher the score will be, so it should work for you.
> 
> On Wednesday, February 15, 2012 at 4:43 PM, Dragan wrote:
> 
> Hi,
> 
> I have the following setup:  
> Every document in ES has a field of type string, that contains a list  
> of  
> items
> 
> basket : ["yuca", "potato"]
> 
> and a field of type long called price
> 
> price: 626
> 
> I'm adjusting the score of my search results based on the difference  
> between  
> the price field and the price I search for.  
> I use CustomScoreQuery and I pass this mvel script to it:
> 
> floor(doc.price.value/100)
> 
> I am using a Boolean filter to get a partial match on the basket  
> field. I'm  
> using pyes (0.16.0), so under the hood the filter looks like this:
> 
> 'bool':{  
> 'minimum\_number\_should\_match':1,  
> 'should':[  
> {  
> 'term':{'basket':'yuca'}  
> },  
> {  
> 'term':{'basket':'yam'}  
> }  
> ]  
> }
> 
> Here's what I'm trying to do:  
> I want to be able to adjust the score when I get a partial match on  
> the  
> basket field.
> 
> In this example I asked for 'yuca' and 'yam' and the result contains  
> 'yuca'  
> and 'potato' (so only 'yuca' is a match). I want to penalise the score  
> by  
> one point for each missed term.
> 
> In this case the price contribution to the score is 6 and the basket  
> penalty  
> should be 1, so the overall score for this should be 5.
> 
> I prefer to handle this logic in the script that I pass to  
> CustomScoreQuery,  
> but I'm open to other solutions as well.
> 
> Thank you

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [February 16, 2012, 7:36pm UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/4 "2012-02-16T19:36:56Z")

</div>

No, there isn't such an option. the closest you can get to is the custom\_filters\_score query ([http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)).

On Thursday, February 16, 2012 at 12:21 AM, Dragan Chupacabrovic wrote:

> Thanks Shay, that works great when I want to see the results sorted by score.
> 
> I'm trying to use score as a percentage match metric. Is there a way to set the score based on the number of matches in the Boolean query? I'd like to be able to tune the penalty function for imperfect matches.
> 
> On Wed, Feb 15, 2012 at 2:43 PM, Shay Banon \<[kimchy@gmail.com](mailto:kimchy@gmail.com) ([mailto:kimchy@gmail.com](mailto:kimchy@gmail.com))\> wrote:
> 
> > When you use should clauses in a bool query, the more matches you will have, the higher the score will be, so it should work for you.
> > 
> > On Wednesday, February 15, 2012 at 4:43 PM, Dragan wrote:
> > 
> > > Hi,
> > > 
> > > I have the following setup:  
> > > Every document in ES has a field of type string, that contains a list  
> > > of  
> > > items
> > > 
> > > basket : ["yuca", "potato"]
> > > 
> > > and a field of type long called price
> > > 
> > > price: 626
> > > 
> > > I'm adjusting the score of my search results based on the difference  
> > > between  
> > > the price field and the price I search for.  
> > > I use CustomScoreQuery and I pass this mvel script to it:
> > > 
> > > floor(doc.price.value/100)
> > > 
> > > I am using a Boolean filter to get a partial match on the basket  
> > > field. I'm  
> > > using pyes (0.16.0), so under the hood the filter looks like this:
> > > 
> > > 'bool':{  
> > > 'minimum\_number\_should\_match':1,  
> > > 'should':[  
> > > {  
> > > 'term':{'basket':'yuca'}  
> > > },  
> > > {  
> > > 'term':{'basket':'yam'}  
> > > }  
> > > ]  
> > > }
> > > 
> > > Here's what I'm trying to do:  
> > > I want to be able to adjust the score when I get a partial match on  
> > > the  
> > > basket field.
> > > 
> > > In this example I asked for 'yuca' and 'yam' and the result contains  
> > > 'yuca'  
> > > and 'potato' (so only 'yuca' is a match). I want to penalise the score  
> > > by  
> > > one point for each missed term.
> > > 
> > > In this case the price contribution to the score is 6 and the basket  
> > > penalty  
> > > should be 1, so the overall score for this should be 5.
> > > 
> > > I prefer to handle this logic in the script that I pass to  
> > > CustomScoreQuery,  
> > > but I'm open to other solutions as well.
> > > 
> > > Thank you

---

<div class="post-metadata">

### Author: ![alfarid23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alfarid23/32/2983_2.png) [@alfarid23](https://discuss.elastic.co/u/alfarid23)
#### Post date: [February 22, 2012, 11:32am UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/5 "2012-02-22T11:32:56Z")

</div>

Greetings!

I have very similar issue related to number of matches. Is there any  
way to access number of matches from custom\_filters\_score?

Thank you!

On Feb 16, 11:36 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> No, there isn't such an option. the closest you can get to is the custom\_filters\_score query ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters)...).
> 
> On Thursday, February 16, 2012 at 12:21 AM, Dragan Chupacabrovic wrote:
> 
> > Thanks Shay, that works great when I want to see the results sorted by score.
> 
> > I'm trying to use score as a percentage match metric. Is there a way to set the score based on the number of matches in the Boolean query? I'd like to be able to tune the penalty function for imperfect matches.
> 
> > On Wed, Feb 15, 2012 at 2:43 PM, Shay Banon \<[kim...@gmail.com](mailto:kim...@gmail.com) ([mailto:kim...@gmail.com](mailto:kim...@gmail.com))\> wrote:
> > 
> > > When you use should clauses in a bool query, the more matches you will have, the higher the score will be, so it should work for you.
> 
> > > On Wednesday, February 15, 2012 at 4:43 PM, Dragan wrote:
> 
> > > > Hi,
> 
> > > > I have the following setup:  
> > > > Every document in ES has a field of type string, that contains a list  
> > > > of  
> > > > items
> 
> > > > basket : ["yuca", "potato"]
> 
> > > > and a field of type long called price
> 
> > > > price: 626
> 
> > > > I'm adjusting the score of my search results based on the difference  
> > > > between  
> > > > the price field and the price I search for.  
> > > > I use CustomScoreQuery and I pass this mvel script to it:
> 
> > > > floor(doc.price.value/100)
> 
> > > > I am using a Boolean filter to get a partial match on the basket  
> > > > field. I'm  
> > > > using pyes (0.16.0), so under the hood the filter looks like this:
> 
> > > > 'bool':{  
> > > > 'minimum\_number\_should\_match':1,  
> > > > 'should':[  
> > > > {  
> > > > 'term':{'basket':'yuca'}  
> > > > },  
> > > > {  
> > > > 'term':{'basket':'yam'}  
> > > > }  
> > > > ]  
> > > > }
> 
> > > > Here's what I'm trying to do:  
> > > > I want to be able to adjust the score when I get a partial match on  
> > > > the  
> > > > basket field.
> 
> > > > In this example I asked for 'yuca' and 'yam' and the result contains  
> > > > 'yuca'  
> > > > and 'potato' (so only 'yuca' is a match). I want to penalise the score  
> > > > by  
> > > > one point for each missed term.
> 
> > > > In this case the price contribution to the score is 6 and the basket  
> > > > penalty  
> > > > should be 1, so the overall score for this should be 5.
> 
> > > > I prefer to handle this logic in the script that I pass to  
> > > > CustomScoreQuery,  
> > > > but I'm open to other solutions as well.
> 
> > > > Thank you

---

<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: [July 6, 2017, 3:38am UTC](https://discuss.elastic.co/t/adjust-score-based-on-the-number-of-matches-in-boolean-filter/6716/6 "2017-07-06T03:38:33Z")

</div>


