# Boosting at query time based on property value

**URL:** <https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386>\
**Category:** Elasticsearch\
**Created:** [May 11, 2011, 10:20pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386 "2011-05-11T22:20:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannick\_Smits](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yannick\_Smits](https://discuss.elastic.co/u/Yannick_Smits)\
**Post date:** [May 11, 2011, 10:20pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/1 "2011-05-11T22:20:08Z")

</div>

Say I have this kind of records indexed in ES:

{  
"book" {  
"\_id": 1234,  
"author" : 100147,  
"categories" : [3, 5, 7, 11],  
"content" : "some text "  
}  
}

Say my user has defined a preference that it is more interested in  
certain authors than others, so for instance would like to see books  
from author 100147, 100234 and 100602 higher up in the search results  
than other books. How can you now apply this boost at query time?  
(same goes for categories btw).

---

<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:** [May 12, 2011, 9:00am UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/2 "2011-05-12T09:00:39Z")

</div>

You can use a custom\_score query, pass those values as params to the script, and boost based on that. [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)  
On Thursday, May 12, 2011 at 1:20 AM, Yannick Smits wrote:

> Say I have this kind of records indexed in ES:
> 
> {  
> "book" {  
> "\_id": 1234,  
> "author" : 100147,  
> "categories" : [3, 5, 7, 11],  
> "content" : "some text "  
> }  
> }
> 
> Say my user has defined a preference that it is more interested in  
> certain authors than others, so for instance would like to see books  
> from author 100147, 100234 and 100602 higher up in the search results  
> than other books. How can you now apply this boost at query time?  
> (same goes for categories btw).

---

<div class="post-metadata">

**Author:** ![Yannick\_Smits\_2](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yannick\_Smits\_2](https://discuss.elastic.co/u/Yannick_Smits_2)\
**Post date:** [May 19, 2011, 2:41pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/3 "2011-05-19T14:41:04Z")

</div>

Thanks, I’ve meditated a week on your answer but can’t seem to grasp how that would work.

Should I use the script property to write some code and do some if statements? Like

“script”: “(doc['author'].value == 100147) ? 2 : 1”

Will this work or is there a more efficient way I’m not seeing?

Thanks,

Yannick “newbie”

From: Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
Sent: donderdag 12 mei 2011 11:01  
To: [users@elasticsearch.com](mailto:users@elasticsearch.com)  
Subject: Re: boosting at query time based on property value

You can use a custom\_score query, pass those values as params to the script, and boost based on that. [http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)

On Thursday, May 12, 2011 at 1:20 AM, Yannick Smits wrote:

Say I have this kind of records indexed in ES:

{  
"book" {  
"\_id": 1234,  
"author" : 100147,  
"categories" : [3, 5, 7, 11],  
"content" : "some text "  
}  
}

Say my user has defined a preference that it is more interested in  
certain authors than others, so for instance would like to see books  
from author 100147, 100234 and 100602 higher up in the search results  
than other books. How can you now apply this boost at query time?  
(same goes for categories btw).

---

<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:** [May 19, 2011, 3:46pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/4 "2011-05-19T15:46:21Z")

</div>

Yannick,

I had a similar requirement which I solved using this code (Java), just in  
case it can inspire yours. It consisted of discriminating the results  
belonging to a special category (sorry about the spanish field names):

```
        queryCustom = customScoreQuery(querystring).script("if

```

(doc['categorias2'].multiValued) {foreach(cat:  
doc['categorias2'].values){if(cat=='Libros'){return \_score/1.5;}}return  
\_score;}else if(doc['categorias2'].stringValue=='Libros'){return  
\_score/2;}else{return \_score;}");

On Thu, May 19, 2011 at 4:41 PM, Yannick Smits [mailinglists@goyaweb.nl](mailto:mailinglists@goyaweb.nl)wrote:

> Thanks, I’ve meditated a week on your answer but can’t seem to grasp how  
> that would work.
> 
> Should I use the script property to write some code and do some if  
> statements? Like
> 
> “script”: “(doc['author'].value == 100147) ? 2 : 1”
> 
> Will this work or is there a more efficient way I’m not seeing?
> 
> Thanks,
> 
> Yannick “newbie”
> 
> _From:_ Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
> _Sent:_ donderdag 12 mei 2011 11:01  
> _To:_ [users@elasticsearch.com](mailto:users@elasticsearch.com)  
> _Subject:_ Re: boosting at query time based on property value
> 
> You can use a custom\_score query, pass those values as params to the  
> script, and boost based on that.  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)
> 
> On Thursday, May 12, 2011 at 1:20 AM, Yannick Smits wrote:
> 
> Say I have this kind of records indexed in ES:
> 
> {  
> "book" {  
> "\_id": 1234,  
> "author" : 100147,  
> "categories" : [3, 5, 7, 11],  
> "content" : "some text "  
> }  
> }
> 
> Say my user has defined a preference that it is more interested in  
> certain authors than others, so for instance would like to see books  
> from author 100147, 100234 and 100602 higher up in the search results  
> than other books. How can you now apply this boost at query time?  
> (same goes for categories btw).

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [May 20, 2011, 7:58pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/5 "2011-05-20T19:58:20Z")

</div>

A lucene query works for this:

{ "query":{  
"query\_string" : {  
"default\_field" : "author",  
"query" : "\* OR author:ab^1 OR author:xy^10"  
}  
}}

> Will this work or is there a more efficient way I’m not seeing?

using native script should be also a bit more efficient

---

<div class="post-metadata">

**Author:** ![Yannick\_Smits\_2](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yannick\_Smits\_2](https://discuss.elastic.co/u/Yannick_Smits_2)\
**Post date:** [May 20, 2011, 8:20pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/6 "2011-05-20T20:20:33Z")

</div>

Great! Could you explain to me the terms "lucene query" and "native script"?

Thanks,  
Yannick

-----Original Message-----  
From: Karussell [[mailto:tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com)]  
Sent: vrijdag 20 mei 2011 21:58  
To: users  
Subject: Re: boosting at query time based on property value

A lucene query works for this:

{ "query":{  
"query\_string" : {  
"default\_field" : "author",  
"query" : "\* OR author:ab^1 OR author:xy^10"  
}  
}}

> Will this work or is there a more efficient way I'm not seeing?

using native script should be also a bit more efficient

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [May 20, 2011, 8:41pm UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/7 "2011-05-20T20:41:13Z")

</div>

With lucene query I mean that a 'standard' lucene query is used:

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

Native script was introduced lately in ES. You can write scripts with  
java (but have all the drawbacks of it):

> **[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.

On May 20, 10:20 pm, Yannick Smits [mailingli...@goyaweb.nl](mailto:mailingli...@goyaweb.nl) wrote:

> Great! Could you explain to me the terms "lucene query" and "native script"?
> 
> Thanks,  
> Yannick
> 
> -----Original Message-----  
> From: Karussell [[mailto:tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com)]  
> Sent: vrijdag 20 mei 2011 21:58  
> To: users  
> Subject: Re: boosting at query time based on property value
> 
> A lucene query works for this:
> 
> { "query":{  
> "query\_string" : {  
> "default\_field" : "author",  
> "query" : "\* OR author:ab^1 OR author:xy^10"  
> }  
> }}
> 
> > Will this work or is there a more efficient way I'm not seeing?
> 
> using native script should be also a bit more efficient

---

<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:05am UTC](https://discuss.elastic.co/t/boosting-at-query-time-based-on-property-value/4386/8 "2017-07-06T04:05:43Z")

</div>


