# Boost based on field value

**URL:** https://discuss.elastic.co/t/boost-based-on-field-value/4205
**Category:** Elasticsearch
**Created:** [April 8, 2011, 3:02pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205 "2011-04-08T15:02:45Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 8, 2011, 3:02pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/1 "2011-04-08T15:02:45Z")

</div>

Hi,

I was wondering whether it's possible to boost a field based on its value.  
Let me explain my scenario.

At my website, we're indexing products from different shops, but the book  
shops are the ones with most products, so when I search for "iphone", let's  
say, I get most of the first results with books interwingled.

Would it be possible to negatively boost based on whether the record is a  
book (I have a field which specifies the category, so I could identify it  
with no issues)?

The idea would be to boost results that are not books, but without losing  
the books, of course. Just would like to push them further in terms of  
relevance.

Thanks.

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [April 8, 2011, 3:54pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/2 "2011-04-08T15:54:08Z")

</div>

Hi Enrique,

Not sure if I fully understand your use base, but you want to change the  
boost depending on the query? For example, changing the category boost if  
the query is for iPhone? You can look at the query before you send the  
request to ES and change the boost accordingly. I believe every query type  
has a boost attribute.

There is something called a Boost Query, which I have never tried, but is  
not might not be what you are looking for since you need to affect the boost  
on a field that is not search on:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

If you simply want to lower the boost for any results from the book store,  
why not set the boost for the category type in the mapping?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Ivan

On Fri, Apr 8, 2011 at 11:02 AM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> Hi,
> 
> I was wondering whether it's possible to boost a field based on its value.  
> Let me explain my scenario.
> 
> At my website, we're indexing products from different shops, but the book  
> shops are the ones with most products, so when I search for "iphone", let's  
> say, I get most of the first results with books interwingled.
> 
> Would it be possible to negatively boost based on whether the record is a  
> book (I have a field which specifies the category, so I could identify it  
> with no issues)?
> 
> The idea would be to boost results that are not books, but without losing  
> the books, of course. Just would like to push them further in terms of  
> relevance.
> 
> Thanks.

---

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 8, 2011, 4:06pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/3 "2011-04-08T16:06:03Z")

</div>

Issue is I can have other categories, so just boosting the field category  
would have side effects.

Basically I would like to be able to do something like this:

"Get me all the products which match "iphone", but leave the results coming  
from a product whose "category1=books" at the end, so the ones coming from  
other categories, like "electronics or computers" are "more relevant"...

On Fri, Apr 8, 2011 at 5:54 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Hi Enrique,
> 
> Not sure if I fully understand your use base, but you want to change the  
> boost depending on the query? For example, changing the category boost if  
> the query is for iPhone? You can look at the query before you send the  
> request to ES and change the boost accordingly. I believe every query type  
> has a boost attribute.
> 
> There is something called a Boost Query, which I have never tried, but is  
> not might not be what you are looking for since you need to affect the boost  
> on a field that is not search on:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html)
> 
> If you simply want to lower the boost for any results from the book store,  
> why not set the boost for the category type in the mapping?
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/boost-field.html)
> 
> Ivan
> 
> On Fri, Apr 8, 2011 at 11:02 AM, Enrique Medina Montenegro \<  
> [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:
> 
> > Hi,
> > 
> > I was wondering whether it's possible to boost a field based on its value.  
> > Let me explain my scenario.
> > 
> > At my website, we're indexing products from different shops, but the book  
> > shops are the ones with most products, so when I search for "iphone", let's  
> > say, I get most of the first results with books interwingled.
> > 
> > Would it be possible to negatively boost based on whether the record is a  
> > book (I have a field which specifies the category, so I could identify it  
> > with no issues)?
> > 
> > The idea would be to boost results that are not books, but without losing  
> > the books, of course. Just would like to push them further in terms of  
> > relevance.
> > 
> > Thanks.

---

<div class="post-metadata">

### Author: ![AGuereca](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aguereca/32/2357_2.png) [@AGuereca](https://discuss.elastic.co/u/AGuereca)
#### Post date: [April 8, 2011, 4:28pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/4 "2011-04-08T16:28:12Z")

</div>

Hi Enrique,

I would give a try to Custom\_Score:

[http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)

There you can teak the score according to you criteria.

~AG

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [April 8, 2011, 6:21pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/5 "2011-04-08T18:21:18Z")

</div>

Hi Enrique

On Fri, 2011-04-08 at 18:06 +0200, Enrique Medina Montenegro wrote:

> Issue is I can have other categories, so just boosting the field  
> category would have side effects.

You can specify boosts in most (all?) query types - these boosts are  
included in the score calculation.

For instance you could do:

{  
query: {  
term: {  
category: {  
value: "iphone",  
boost: 2  
}}}}

Note: this is a term QUERY not a term FILTER.

For an example, see:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> 

You will need to play with the boost values to figure out what the right  
number is.

Using explain helps with this:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

---

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 11, 2011, 8:32am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/6 "2011-04-11T08:32:49Z")

</div>

Clinton,

I'm currently using "query\_string" for my queries, but boost here is applied  
to the whole query, not just a specific field. How could I specify the boost  
for a field in this type of query?

Thanks.

On Fri, Apr 8, 2011 at 8:21 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Enrique
> 
> On Fri, 2011-04-08 at 18:06 +0200, Enrique Medina Montenegro wrote:
> 
> > Issue is I can have other categories, so just boosting the field  
> > category would have side effects.
> 
> You can specify boosts in most (all?) query types - these boosts are  
> included in the score calculation.
> 
> For instance you could do:
> 
> {  
> query: {  
> term: {  
> category: {  
> value: "iphone",  
> boost: 2  
> }}}}
> 
> Note: this is a term QUERY not a term FILTER.
> 
> For an example, see:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html)
> 
> > 
> 
> You will need to play with the boost values to figure out what the right  
> number is.
> 
> Using explain helps with this:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/explain.html)
> 
> clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [April 11, 2011, 8:36am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/7 "2011-04-11T08:36:50Z")

</div>

Hi Enrique

> I'm currently using "query\_string" for my queries, but boost here is  
> applied to the whole query, not just a specific field. How could I  
> specify the boost for a field in this type of query?

[http://lucene.apache.org/java/3\_0\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)

You could do: "title:(war peace)^5 summary:(war peace)^2 war peace"

which would boost the title field by 5, the summary field by 2, and use  
the default boost for the \_all field

clint

---

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 11, 2011, 8:54am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/8 "2011-04-11T08:54:03Z")

</div>

Thanks a lot!

In the meantime, I was searching the custom\_score\_query, but I just cannot  
make the query work (I send it but I never get a response back, and there's  
nothing in the logs). Do you know what could be wrong with this query?

> <https://gist.github.com/emedina/913246>

On Mon, Apr 11, 2011 at 10:36 AM, Clinton Gormley  
[clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hi Enrique
> 
> > I'm currently using "query\_string" for my queries, but boost here is  
> > applied to the whole query, not just a specific field. How could I  
> > specify the boost for a field in this type of query?
> 
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)
> 
> You could do: "title:(war peace)^5 summary:(war peace)^2 war peace"
> 
> which would boost the title field by 5, the summary field by 2, and use  
> the default boost for the \_all field
> 
> clint

---

<div class="post-metadata">

### Author: ![Ludovic\_Levesque](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ludovic_levesque/32/3031_2.png) [@Ludovic\_Levesque](https://discuss.elastic.co/u/Ludovic_Levesque)
#### Post date: [April 11, 2011, 9:04am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/9 "2011-04-11T09:04:45Z")

</div>

Hi Enrique,

in custom\_score queries, the mvel script is directly in the 'script'  
part, not in script.script  
see [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)  
for example

See [https://gist.github.com/913260#gistcomment-26697](https://gist.github.com/913260#gistcomment-26697)

Ludo

On Mon, Apr 11, 2011 at 10:54 AM, Enrique Medina Montenegro  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com) wrote:

> Thanks a lot!  
> In the meantime, I was searching the custom\_score\_query, but I just cannot  
> make the query work (I send it but I never get a response back, and there's  
> nothing in the logs). Do you know what could be wrong with this query?  
> [MVEL for script in custom\_score\_query · GitHub](https://gist.github.com/913246)  
> On Mon, Apr 11, 2011 at 10:36 AM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)  
> wrote:
> 
> > Hi Enrique
> > 
> > > I'm currently using "query\_string" for my queries, but boost here is  
> > > applied to the whole query, not just a specific field. How could I  
> > > specify the boost for a field in this type of query?
> > 
> > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)
> > 
> > You could do: "title:(war peace)^5 summary:(war peace)^2 war peace"
> > 
> > which would boost the title field by 5, the summary field by 2, and use  
> > the default boost for the \_all field
> > 
> > clint

---

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 11, 2011, 9:11am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/10 "2011-04-11T09:11:05Z")

</div>

Ludovic,

I tried your query, but it doesn't do anything, i.e. I'm using  
elasticsearch-head JS client, and it displays requesting... but I never get  
a response back.

Any clue?

Regards.

On Mon, Apr 11, 2011 at 11:04 AM, Ludovic Levesque [luddic@gmail.com](mailto:luddic@gmail.com) wrote:

> Hi Enrique,
> 
> in custom\_score queries, the mvel script is directly in the 'script'  
> part, not in script.script  
> see  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)  
> for example
> 
> See [https://gist.github.com/913260#gistcomment-26697](https://gist.github.com/913260#gistcomment-26697)
> 
> Ludo
> 
> On Mon, Apr 11, 2011 at 10:54 AM, Enrique Medina Montenegro  
> [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com) wrote:
> 
> > Thanks a lot!  
> > In the meantime, I was searching the custom\_score\_query, but I just  
> > cannot  
> > make the query work (I send it but I never get a response back, and  
> > there's  
> > nothing in the logs). Do you know what could be wrong with this query?  
> > [MVEL for script in custom\_score\_query · GitHub](https://gist.github.com/913246)  
> > On Mon, Apr 11, 2011 at 10:36 AM, Clinton Gormley \<  
> > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > wrote:
> > 
> > > Hi Enrique
> > > 
> > > > I'm currently using "query\_string" for my queries, but boost here is  
> > > > applied to the whole query, not just a specific field. How could I  
> > > > specify the boost for a field in this type of query?
> > > 
> > > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)
> > > 
> > > You could do: "title:(war peace)^5 summary:(war peace)^2 war peace"
> > > 
> > > which would boost the title field by 5, the summary field by 2, and use  
> > > the default boost for the \_all field
> > > 
> > > clint

---

<div class="post-metadata">

### Author: ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)
#### Post date: [April 11, 2011, 2:46pm UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/11 "2011-04-11T14:46:30Z")

</div>

Just for the records, I eventually accomplished what I was looking for with  
this query:

[https://gist.github.com/913260#gistcomment-26720](https://gist.github.com/913260#gistcomment-26720)

On Mon, Apr 11, 2011 at 11:11 AM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> Ludovic,
> 
> I tried your query, but it doesn't do anything, i.e. I'm using  
> elasticsearch-head JS client, and it displays requesting... but I never get  
> a response back.
> 
> Any clue?
> 
> Regards.
> 
> On Mon, Apr 11, 2011 at 11:04 AM, Ludovic Levesque [luddic@gmail.com](mailto:luddic@gmail.com)wrote:
> 
> > Hi Enrique,
> > 
> > in custom\_score queries, the mvel script is directly in the 'script'  
> > part, not in script.script  
> > see  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)  
> > for example
> > 
> > See [https://gist.github.com/913260#gistcomment-26697](https://gist.github.com/913260#gistcomment-26697)
> > 
> > Ludo
> > 
> > On Mon, Apr 11, 2011 at 10:54 AM, Enrique Medina Montenegro  
> > [e.medina.m@gmail.com](mailto:e.medina.m@gmail.com) wrote:
> > 
> > > Thanks a lot!  
> > > In the meantime, I was searching the custom\_score\_query, but I just  
> > > cannot  
> > > make the query work (I send it but I never get a response back, and  
> > > there's  
> > > nothing in the logs). Do you know what could be wrong with this query?  
> > > [MVEL for script in custom\_score\_query · GitHub](https://gist.github.com/913246)  
> > > On Mon, Apr 11, 2011 at 10:36 AM, Clinton Gormley \<  
> > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\>  
> > > wrote:
> > > 
> > > > Hi Enrique
> > > > 
> > > > > I'm currently using "query\_string" for my queries, but boost here is  
> > > > > applied to the whole query, not just a specific field. How could I  
> > > > > specify the boost for a field in this type of query?
> > > > 
> > > > [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)
> > > > 
> > > > You could do: "title:(war peace)^5 summary:(war peace)^2 war peace"
> > > > 
> > > > which would boost the title field by 5, the summary field by 2, and use  
> > > > the default boost for the \_all field
> > > > 
> > > > clint

---

<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, 4:08am UTC](https://discuss.elastic.co/t/boost-based-on-field-value/4205/12 "2017-07-06T04:08:36Z")

</div>


