# Custom score in Elastic Search

**URL:** https://discuss.elastic.co/t/custom-score-in-elastic-search/12053
**Category:** Elasticsearch
**Created:** [May 21, 2013, 11:34am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053 "2013-05-21T11:34:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Dmitry\_Babitsky](https://avatars.discourse-cdn.com/v4/letter/d/85e7bf/32.png) [@Dmitry\_Babitsky](https://discuss.elastic.co/u/Dmitry_Babitsky)
#### Post date: [May 21, 2013, 11:34am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/1 "2013-05-21T11:34:45Z")

</div>

Hi all,

I would like to search in my ES DB and return the results according to some  
customer score.  
Is it possible, if yes, what's the best way to do it, and what would be the  
runtime consequences?

- 
- 

_Case A:_  
I have an index, where each document represents a class, with list of  
students and a list of teachers.

{  
"properties" : {  
"students" : {  
"include\_in\_root" : 1,  
"type" : "nested",  
"properties" : {  
"name" : {  
"first" : "text",  
"last" : "text",  
},  
},  
"email" : { "type" : "string", "index" : "not\_analyzed" },  
},

```
"teachers" : {
    "include_in_root" : 0,
    "include_in_all" : 0,
    "type" : "nested",
    "properties" : {
        "name" : {
            "title" : { "type" : "string", "index" : "not_analyzed" },
            "first" : "text",
            "last" : "text",
            },
        },
    },
},

```

}

I would like to influence Elastic Search score function, such that it will  
give higher score to documents with matched _techers_.

For example, if I have search for "Smith", I would like the documents with \*  
teacher\* Smith be with higher score than documents with students Smith.

_Case B:_

Same mapping as in Case A, but now I would like to give a higher score to  
teachers with title "Dr" or "Prof".

Thanks a lot!

Dmitry.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [May 23, 2013, 9:48am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/2 "2013-05-23T09:48:10Z")

</div>

Use a bool query to run two nested queries: one on students and one on  
teachers, and boost the teachers query over the students query

clint

On 21 May 2013 13:34, Dmitry Babitsky [dimok21@gmail.com](mailto:dimok21@gmail.com) wrote:

> Hi all,
> 
> I would like to search in my ES DB and return the results according to  
> some customer score.  
> Is it possible, if yes, what's the best way to do it, and what would be  
> the runtime consequences?
> 
> - 
> - 
> 
> _Case A:_  
> I have an index, where each document represents a class, with list of  
> students and a list of teachers.
> 
> {  
> "properties" : {  
> "students" : {  
> "include\_in\_root" : 1,  
> "type" : "nested",  
> "properties" : {  
> "name" : {  
> "first" : "text",  
> "last" : "text",  
> },  
> },  
> "email" : { "type" : "string", "index" : "not\_analyzed" },  
> },
> 
> ```
> "teachers" : {
> "include_in_root" : 0,
> "include_in_all" : 0,
> "type" : "nested",
> "properties" : {
> "name" : {
> "title" : { "type" : "string", "index" : "not_analyzed" },
> "first" : "text",
> "last" : "text",
> },
> },
> },
> },
> 
> ```
> 
> }
> 
> I would like to influence Elastic Search score function, such that it will  
> give higher score to documents with matched _techers_.
> 
> For example, if I have search for "Smith", I would like the documents with  
> _teacher_ Smith be with higher score than documents with students Smith.
> 
> _Case B:_
> 
> Same mapping as in Case A, but now I would like to give a higher score to  
> teachers with title "Dr" or "Prof".
> 
> Thanks a lot!
> 
> Dmitry.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Dmitry\_Babitsky](https://avatars.discourse-cdn.com/v4/letter/d/85e7bf/32.png) [@Dmitry\_Babitsky](https://discuss.elastic.co/u/Dmitry_Babitsky)
#### Post date: [May 23, 2013, 11:34am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/3 "2013-05-23T11:34:01Z")

</div>

Thanks a lot!!!

One more question - suppose each teacher and each student have some rank  
from 0 to 100.  
Is is possible to influence the Elastic Search score to take into account  
the student/teacher rank (in addition to the default matching score)?

Thanks again,  
Dmitry.

On Thursday, May 23, 2013 12:48:10 PM UTC+3, Clinton Gormley wrote:

> Use a bool query to run two nested queries: one on students and one on  
> teachers, and boost the teachers query over the students query
> 
> clint
> 
> On 21 May 2013 13:34, Dmitry Babitsky \<[dim...@gmail.com](mailto:dim...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi all,
> > 
> > I would like to search in my ES DB and return the results according to  
> > some customer score.  
> > Is it possible, if yes, what's the best way to do it, and what would be  
> > the runtime consequences?
> > 
> > - 
> > - 
> > 
> > _Case A:_  
> > I have an index, where each document represents a class, with list of  
> > students and a list of teachers.
> > 
> > {  
> > "properties" : {  
> > "students" : {  
> > "include\_in\_root" : 1,  
> > "type" : "nested",  
> > "properties" : {  
> > "name" : {  
> > "first" : "text",  
> > "last" : "text",  
> > },  
> > },  
> > "email" : { "type" : "string", "index" : "not\_analyzed" },  
> > },
> > 
> > ```
> > "teachers" : {
> > "include_in_root" : 0,
> > "include_in_all" : 0,
> > "type" : "nested",
> > "properties" : {
> > "name" : {
> > "title" : { "type" : "string", "index" : "not_analyzed" },
> > "first" : "text",
> > "last" : "text",
> > },
> > },
> > },
> > },
> > 
> > ```
> > 
> > }
> > 
> > I would like to influence Elastic Search score function, such that it  
> > will give higher score to documents with matched _techers_.
> > 
> > For example, if I have search for "Smith", I would like the documents  
> > with _teacher_ Smith be with higher score than documents with students  
> > Smith.
> > 
> > _Case B:_
> > 
> > Same mapping as in Case A, but now I would like to give a higher score to  
> > teachers with title "Dr" or "Prof".
> > 
> > Thanks a lot!
> > 
> > Dmitry.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [May 23, 2013, 11:38am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/4 "2013-05-23T11:38:22Z")

</div>

Yes - look at using a script in a custom\_score query on the nested docs

c

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Dmitry\_Babitsky](https://avatars.discourse-cdn.com/v4/letter/d/85e7bf/32.png) [@Dmitry\_Babitsky](https://discuss.elastic.co/u/Dmitry_Babitsky)
#### Post date: [May 27, 2013, 4:38pm UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/5 "2013-05-27T16:38:29Z")

</div>

Hi Clinton,

Again, many thanks for your help!

Now I ran into another problem.  
If I search for two words-expression (for example: "James Bond"), Elastic  
Search returns all the classes where James Bond either studies or teaches.

However, I would like to get the documents where one of the word (James)  
matches in students, and the other (Bond) matches in teachers (or vice  
versa).

Is there any way to do it in Elastic Search?

Thanks,  
Dmitry.

On Thursday, May 23, 2013 12:48:10 PM UTC+3, Clinton Gormley wrote:

> Use a bool query to run two nested queries: one on students and one on  
> teachers, and boost the teachers query over the students query
> 
> clint
> 
> On 21 May 2013 13:34, Dmitry Babitsky \<[dim...@gmail.com](mailto:dim...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi all,
> > 
> > I would like to search in my ES DB and return the results according to  
> > some customer score.  
> > Is it possible, if yes, what's the best way to do it, and what would be  
> > the runtime consequences?
> > 
> > - 
> > - 
> > 
> > _Case A:_  
> > I have an index, where each document represents a class, with list of  
> > students and a list of teachers.
> > 
> > {  
> > "properties" : {  
> > "students" : {  
> > "include\_in\_root" : 1,  
> > "type" : "nested",  
> > "properties" : {  
> > "name" : {  
> > "first" : "text",  
> > "last" : "text",  
> > },  
> > },  
> > "email" : { "type" : "string", "index" : "not\_analyzed" },  
> > },
> > 
> > ```
> > "teachers" : {
> > "include_in_root" : 0,
> > "include_in_all" : 0,
> > "type" : "nested",
> > "properties" : {
> > "name" : {
> > "title" : { "type" : "string", "index" : "not_analyzed" },
> > "first" : "text",
> > "last" : "text",
> > },
> > },
> > },
> > },
> > 
> > ```
> > 
> > }
> > 
> > I would like to influence Elastic Search score function, such that it  
> > will give higher score to documents with matched _techers_.
> > 
> > For example, if I have search for "Smith", I would like the documents  
> > with _teacher_ Smith be with higher score than documents with students  
> > Smith.
> > 
> > _Case B:_
> > 
> > Same mapping as in Case A, but now I would like to give a higher score to  
> > teachers with title "Dr" or "Prof".
> > 
> > Thanks a lot!
> > 
> > Dmitry.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:34am UTC](https://discuss.elastic.co/t/custom-score-in-elastic-search/12053/6 "2017-07-06T02:34:32Z")

</div>


