# Basic Query regarding custom\_score

**URL:** https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211
**Category:** Elasticsearch
**Created:** [August 12, 2010, 12:10pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211 "2010-08-12T12:10:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![deepu](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@deepu](https://discuss.elastic.co/u/deepu)
#### Post date: [August 12, 2010, 12:10pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/1 "2010-08-12T12:10:26Z")

</div>

Hi,  
I am pretty new to Elastic Search. Trying it out and looks  
awesome till now. I was trying to understand how to use custom\_score  
and from the documentation could not fathom how to use it.

"custom\_score" : {  
"query" : {  
....  
},  
"script" : "score \* doc['my\_numeric\_field'].value"  
}

What is inside that "query" string ??? Can someone give me a basic  
example using custom\_score query dsl ?

Also i would like to know how to solve this problem:

Say you have a forum with lots of threads and each post in a thread  
has a bunch of thanks (similar to "like" functionality we see in  
facebook). Now when i search on that forum index i want it results  
posts. i want to order posts on number of likes it has.

The problem is - "How to index likes ?"

If i keep likes as a field in the post document then i will be  
suffering consistency issues when 2 people simultaneously make thank a  
post. if i put like as a seperate document then there wont be any  
consistency issues but then how can i sort posts in the search results  
according to number of thanks they receive ???

Cheers,  
Deepu.

---

<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: [August 12, 2010, 12:28pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/2 "2010-08-12T12:28:53Z")

</div>

Hiya

> ```
> I am pretty new to Elastic Search. Trying it out and looks
> 
> ```
> 
> awesome till now. I was trying to understand how to use custom\_score  
> and from the documentation could not fathom how to use it.
> 
> "custom\_score" : {  
> "query" : {  
> ....  
> },  
> "script" : "score \* doc['my\_numeric\_field'].value"  
> }
> 
> What is inside that "query" string ??? Can someone give me a basic  
> example using custom\_score query dsl ?

"query" is whatever query you are using to search for documents, which  
could be (eg):

# matches all documents

"query": { "match\_all": {} }

# search all fields for the keywords "foo" and "bar"

"query": { "query\_string": { "query": "foo bar" } }

By default, the results would be ordered by relevance/score (where all  
documents in the match\_all query have the same relevance)

So "custom\_score" gives you a way of customising the relevance of each  
document dynamically. For instance, you could make recent documents more  
relevant than older documents. Or in your case, posts with more "likes"  
would be more relevant than posts with fewer "likes".

Note, however, that this is a difficult calculation to get right. For  
instance, what happens if you have one post which matches 1 keyword but  
has 1000 likes, and another which matches 10 keywords, but has no likes?

Getting the right balance probably requires a lot of experimentation.  
And should be revisited once you have more live data.

> Also i would like to know how to solve this problem:
> 
> Say you have a forum with lots of threads and each post in a thread  
> has a bunch of thanks (similar to "like" functionality we see in  
> facebook). Now when i search on that forum index i want it results  
> posts. i want to order posts on number of likes it has.
> 
> The problem is - "How to index likes ?"
> 
> If i keep likes as a field in the post document then i will be  
> suffering consistency issues when 2 people simultaneously make thank a  
> post. if i put like as a seperate document then there wont be any  
> consistency issues but then how can i sort posts in the search results  
> according to number of thanks they receive ???

You can't "join" documents like you can in a relational database. So if  
you want to use the number of likes to influence the score, then you  
need to store this value in the post document.

So you need to solve the consistency issue, which obviously depends on  
how your application is setup.

Are you storing this data in a database as well? If so, then you could  
just do a COUNT of all the likes that each post has.

If you're not, well you could still store a "likes" document in ES, and  
have it reference the ID of the post document, and do a count using ES.

hth

clint

---

<div class="post-metadata">

### Author: ![deepu](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@deepu](https://discuss.elastic.co/u/deepu)
#### Post date: [August 12, 2010, 12:54pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/3 "2010-08-12T12:54:46Z")

</div>

Thanks for the detailed explanation.

one more question.

In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
default score calculated by ES using relevancy et al ???

Cheers,  
Deepu.

On Aug 12, 5:28 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hiya
> 
> > ```
> > I am pretty new to Elastic Search. Trying it out and looks
> > 
> > ```
> > 
> > awesome till now. I was trying to understand how to use custom\_score  
> > and from the documentation could not fathom how to use it.
> 
> > "custom\_score" : {  
> > "query" : {  
> > ....  
> > },  
> > "script" : "score \* doc['my\_numeric\_field'].value"  
> > }
> 
> > What is inside that "query" string ??? Can someone give me a basic  
> > example using custom\_score query dsl ?
> 
> "query" is whatever query you are using to search for documents, which  
> could be (eg):
> 
> # matches all documents
> 
> "query": { "match\_all": {} }
> 
> # search all fields for the keywords "foo" and "bar"
> 
> "query": { "query\_string": { "query": "foo bar" } }
> 
> By default, the results would be ordered by relevance/score (where all  
> documents in the match\_all query have the same relevance)
> 
> So "custom\_score" gives you a way of customising the relevance of each  
> document dynamically. For instance, you could make recent documents more  
> relevant than older documents. Or in your case, posts with more "likes"  
> would be more relevant than posts with fewer "likes".
> 
> Note, however, that this is a difficult calculation to get right. For  
> instance, what happens if you have one post which matches 1 keyword but  
> has 1000 likes, and another which matches 10 keywords, but has no likes?
> 
> Getting the right balance probably requires a lot of experimentation.  
> And should be revisited once you have more live data.
> 
> > Also i would like to know how to solve this problem:
> 
> > Say you have a forum with lots of threads and each post in a thread  
> > has a bunch of thanks (similar to "like" functionality we see in  
> > facebook). Now when i search on that forum index i want it results  
> > posts. i want to order posts on number of likes it has.
> 
> > The problem is - "How to index likes ?"
> 
> > If i keep likes as a field in the post document then i will be  
> > suffering consistency issues when 2 people simultaneously make thank a  
> > post. if i put like as a seperate document then there wont be any  
> > consistency issues but then how can i sort posts in the search results  
> > according to number of thanks they receive ???
> 
> You can't "join" documents like you can in a relational database. So if  
> you want to use the number of likes to influence the score, then you  
> need to store this value in the post document.
> 
> So you need to solve the consistency issue, which obviously depends on  
> how your application is setup.
> 
> Are you storing this data in a database as well? If so, then you could  
> just do a COUNT of all the likes that each post has.
> 
> If you're not, well you could still store a "likes" document in ES, and  
> have it reference the ID of the post document, and do a count using ES.
> 
> hth
> 
> 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: [August 12, 2010, 1:21pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/4 "2010-08-12T13:21:27Z")

</div>

On Thu, 2010-08-12 at 05:54 -0700, deepu wrote:

> Thanks for the detailed explanation.
> 
> one more question.
> 
> In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
> default score calculated by ES using relevancy et al ???

Correct, but I think that this has changed to \_score in master - not  
sure - try it out

clint

---

<div class="post-metadata">

### Author: ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)
#### Post date: [August 12, 2010, 1:39pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/5 "2010-08-12T13:39:41Z")

</div>

I think it did not change, see:  
[http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom\_score1.json](http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom_score1.json)

On Thu, Aug 12, 2010 at 3:21 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> On Thu, 2010-08-12 at 05:54 -0700, deepu wrote:
> 
> > Thanks for the detailed explanation.
> > 
> > one more question.
> > 
> > In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
> > default score calculated by ES using relevancy et al ???
> 
> Correct, but I think that this has changed to \_score in master - not  
> sure - try it out
> 
> clint

---

<div class="post-metadata">

### Author: ![ajgamer](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@ajgamer](https://discuss.elastic.co/u/ajgamer)
#### Post date: [August 12, 2010, 2:37pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/6 "2010-08-12T14:37:38Z")

</div>

I think score has been changed to \_score, check this discussion out  
[http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1](http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1)

On Thu, Aug 12, 2010 at 7:09 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> I think it did not change, see:
> 
> [http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom\_score1.json](http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom_score1.json)
> 
> On Thu, Aug 12, 2010 at 3:21 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:
> 
> > On Thu, 2010-08-12 at 05:54 -0700, deepu wrote:
> > 
> > > Thanks for the detailed explanation.
> > > 
> > > one more question.
> > > 
> > > In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
> > > default score calculated by ES using relevancy et al ???
> > 
> > Correct, but I think that this has changed to \_score in master - not  
> > sure - try it out
> > 
> > clint

---

<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: [August 12, 2010, 3:40pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/7 "2010-08-12T15:40:17Z")

</div>

It changed to \_score when sorting by it, but as a variable to the custom  
score query script, it is still score... . Mmmm, confusing, I will alias it  
to also \_score, so you can use both.

-shay.banon

On Thu, Aug 12, 2010 at 5:37 PM, Abbie Joseph [abie.joseph14@gmail.com](mailto:abie.joseph14@gmail.com)wrote:

> I think score has been changed to \_score, check this discussion out  
> [http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1](http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1)
> 
> On Thu, Aug 12, 2010 at 7:09 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com)wrote:
> 
> > I think it did not change, see:
> > 
> > [http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom\_score1.json](http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom_score1.json)
> > 
> > On Thu, Aug 12, 2010 at 3:21 PM, Clinton Gormley \<[clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)
> > 
> > > wrote:
> > 
> > > On Thu, 2010-08-12 at 05:54 -0700, deepu wrote:
> > > 
> > > > Thanks for the detailed explanation.
> > > > 
> > > > one more question.
> > > > 
> > > > In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
> > > > default score calculated by ES using relevancy et al ???
> > > 
> > > Correct, but I think that this has changed to \_score in master - not  
> > > sure - try it out
> > > 
> > > clint

---

<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: [August 12, 2010, 3:41pm UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/8 "2010-08-12T15:41:40Z")

</div>

Done: [Query DSL: custom score script, allow to use `_score` as well as `score` as the underlying query score · Issue #316 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/issue/316).

On Thu, Aug 12, 2010 at 6:40 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> It changed to \_score when sorting by it, but as a variable to the custom  
> score query script, it is still score... . Mmmm, confusing, I will alias it  
> to also \_score, so you can use both.
> 
> -shay.banon
> 
> On Thu, Aug 12, 2010 at 5:37 PM, Abbie Joseph [abie.joseph14@gmail.com](mailto:abie.joseph14@gmail.com)wrote:
> 
> > I think score has been changed to \_score, check this discussion out  
> > [http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1](http://groups.google.com/a/elasticsearch.com/group/users/msg/58b74f9527a06099?pli=1)
> > 
> > On Thu, Aug 12, 2010 at 7:09 PM, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com)wrote:
> > 
> > > I think it did not change, see:
> > > 
> > > [http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom\_score1.json](http://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/custom_score1.json)
> > > 
> > > On Thu, Aug 12, 2010 at 3:21 PM, Clinton Gormley \<  
> > > [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)\> wrote:
> > > 
> > > > On Thu, 2010-08-12 at 05:54 -0700, deepu wrote:
> > > > 
> > > > > Thanks for the detailed explanation.
> > > > > 
> > > > > one more question.
> > > > > 
> > > > > In "script" : "score \* doc['my\_numeric\_field'].value" - score means  
> > > > > default score calculated by ES using relevancy et al ???
> > > > 
> > > > Correct, but I think that this has changed to \_score in master - not  
> > > > sure - try it out
> > > > 
> > > > 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:20am UTC](https://discuss.elastic.co/t/basic-query-regarding-custom-score/3211/9 "2017-07-06T04:20:53Z")

</div>


