# Custom Scoring if term found in specific field

**URL:** <https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512>\
**Category:** Elasticsearch\
**Created:** [October 31, 2012, 3:58pm UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512 "2012-10-31T15:58:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzintarsrerihs](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@dzintarsrerihs](https://discuss.elastic.co/u/dzintarsrerihs)\
**Post date:** [October 31, 2012, 3:58pm UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512/1 "2012-10-31T15:58:56Z")

</div>

Hello!

At this point in my app I need to solve scoring implementation task. What I  
need to achieve is that - if term is found in \*Surname \*field, it can get  
custom boost that is taken from _scoreBoostForSurname_ otherwise (if term  
found in _Name_ then _scoreBoostForSurname_ value is not used)  
Lets assume I have simple three documents:  
{  
"thisID": "1",  
"name": "Elastic",  
"surname": "Search",  
"scoreBoosterForSurname": "0"  
}  
{  
"thisID": "2",  
"name": "Search",  
"surname": "Elastic",  
"scoreBoosterForSurname": "10"  
}  
{  
"thisID": "3",  
"name": "Search",  
"surname": "Elastic",  
"scoreBoosterForSurname": "5"  
}  
And now lets assume I'm querying these documents.  
'  
{  
query: {bool: {must: [{query\_string: {  
query: "Elastic",  
default\_operator: "AND",  
analyze\_wildcard: true,  
fields: [  
"name",  
"surname",  
]  
}}]}}  
}  
'  
What I prefer is that if ordered by score results are ordered and returned  
to me in this order 2,3,1 (values from \*thisID \*field)

So what I assume I need here is not custom score, but custom field  
boosting. Is it really achievable at the moment in Elastic Search? Or is  
there something about custom scoring or even Elastics scoring I don't know  
yet?

I checked on nested objects, but does help to solve my task as well as  
index with child elements as child elements are not included in final  
result when querying parent.

The other options I consider is to use custom score with duplicating user  
entries (documents) and then removing duplicated results at my application  
side.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 31, 2012, 10:50pm UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512/2 "2012-10-31T22:50:15Z")

</div>

I think you can do it by combining dis\_max with custom\_score query:

{  
"query": {  
"dis\_max": {  
"queries": [{  
"query\_string": {  
"query": "Elastic",  
"default\_operator": "AND",  
"analyze\_wildcard": true,  
"default\_field": "name"  
}  
},{  
"custom\_score": {  
"query": {  
"query\_string": {  
"query": "Elastic",  
"default\_operator": "AND",  
"analyze\_wildcard": true,  
"default\_field": "surname"  
}  
},  
"script" : "\_score \* doc['scoreBoosterForSurname'].value"  
}  
}  
]  
}  
}  
}

Here is a full example: [custom\_boost.sh · GitHub](https://gist.github.com/3990458)

On Wednesday, October 31, 2012 11:58:56 AM UTC-4, dzintarsrerihs wrote:

> Hello!
> 
> At this point in my app I need to solve scoring implementation task. What  
> I need to achieve is that - if term is found in \*Surname \*field, it can  
> get custom boost that is taken from _scoreBoostForSurname_ otherwise (if  
> term found in _Name_ then _scoreBoostForSurname_ value is not used)  
> Lets assume I have simple three documents:  
> {  
> "thisID": "1",  
> "name": "Elastic",  
> "surname": "Search",  
> "scoreBoosterForSurname": "0"  
> }  
> {  
> "thisID": "2",  
> "name": "Search",  
> "surname": "Elastic",  
> "scoreBoosterForSurname": "10"  
> }  
> {  
> "thisID": "3",  
> "name": "Search",  
> "surname": "Elastic",  
> "scoreBoosterForSurname": "5"  
> }  
> And now lets assume I'm querying these documents.  
> '  
> {  
> query: {bool: {must: [{query\_string: {  
> query: "Elastic",  
> default\_operator: "AND",  
> analyze\_wildcard: true,  
> fields: [  
> "name",  
> "surname",  
> ]  
> }}]}}  
> }  
> '  
> What I prefer is that if ordered by score results are ordered and returned  
> to me in this order 2,3,1 (values from \*thisID \*field)
> 
> So what I assume I need here is not custom score, but custom field  
> boosting. Is it really achievable at the moment in Elastic Search? Or is  
> there something about custom scoring or even Elastics scoring I don't know  
> yet?
> 
> I checked on nested objects, but does help to solve my task as well as  
> index with child elements as child elements are not included in final  
> result when querying parent.
> 
> The other options I consider is to use custom score with duplicating user  
> entries (documents) and then removing duplicated results at my application  
> side.

--

---

<div class="post-metadata">

**Author:** ![dzintarsrerihs](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@dzintarsrerihs](https://discuss.elastic.co/u/dzintarsrerihs)\
**Post date:** [November 1, 2012, 7:40am UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512/3 "2012-11-01T07:40:12Z")

</div>

Ok, thank you for your answer. I will see how it works and then give a  
feedback on this.

Have a great day!  
Dzinars

ceturtdiena, 2012. gada 1. novembris 00:50:16 UTC+2, Igor Motov rakstīja:

> I think you can do it by combining dis\_max with custom\_score query:
> 
> {  
> "query": {  
> "dis\_max": {  
> "queries": [{  
> "query\_string": {  
> "query": "Elastic",  
> "default\_operator": "AND",  
> "analyze\_wildcard": true,  
> "default\_field": "name"  
> }  
> },{  
> "custom\_score": {  
> "query": {  
> "query\_string": {  
> "query": "Elastic",  
> "default\_operator": "AND",  
> "analyze\_wildcard": true,  
> "default\_field": "surname"  
> }  
> },  
> "script" : "\_score \* doc['scoreBoosterForSurname'].value"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> Here is a full example: [custom\_boost.sh · GitHub](https://gist.github.com/3990458)
> 
> On Wednesday, October 31, 2012 11:58:56 AM UTC-4, dzintarsrerihs wrote:
> 
> > Hello!
> > 
> > At this point in my app I need to solve scoring implementation task. What  
> > I need to achieve is that - if term is found in \*Surname \*field, it can  
> > get custom boost that is taken from _scoreBoostForSurname_ otherwise (if  
> > term found in _Name_ then _scoreBoostForSurname_ value is not used)  
> > Lets assume I have simple three documents:  
> > {  
> > "thisID": "1",  
> > "name": "Elastic",  
> > "surname": "Search",  
> > "scoreBoosterForSurname": "0"  
> > }  
> > {  
> > "thisID": "2",  
> > "name": "Search",  
> > "surname": "Elastic",  
> > "scoreBoosterForSurname": "10"  
> > }  
> > {  
> > "thisID": "3",  
> > "name": "Search",  
> > "surname": "Elastic",  
> > "scoreBoosterForSurname": "5"  
> > }  
> > And now lets assume I'm querying these documents.  
> > '  
> > {  
> > query: {bool: {must: [{query\_string: {  
> > query: "Elastic",  
> > default\_operator: "AND",  
> > analyze\_wildcard: true,  
> > fields: [  
> > "name",  
> > "surname",  
> > ]  
> > }}]}}  
> > }  
> > '  
> > What I prefer is that if ordered by score results are ordered and  
> > returned to me in this order 2,3,1 (values from \*thisID \*field)
> > 
> > So what I assume I need here is not custom score, but custom field  
> > boosting. Is it really achievable at the moment in Elastic Search? Or is  
> > there something about custom scoring or even Elastics scoring I don't know  
> > yet?
> > 
> > I checked on nested objects, but does help to solve my task as well as  
> > index with child elements as child elements are not included in final  
> > result when querying parent.
> > 
> > The other options I consider is to use custom score with duplicating user  
> > entries (documents) and then removing duplicated results at my application  
> > side.

--

---

<div class="post-metadata">

**Author:** ![dzintarsrerihs](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@dzintarsrerihs](https://discuss.elastic.co/u/dzintarsrerihs)\
**Post date:** [November 1, 2012, 12:41pm UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512/4 "2012-11-01T12:41:51Z")

</div>

Yes, this is exactly what I needed. Tested it with more complex queries and  
got the result desired.  
Once again thank you for your response.

Best Regards,  
Dzintars Rerihs

ceturtdiena, 2012. gada 1. novembris 00:50:16 UTC+2, Igor Motov rakstīja:

> I think you can do it by combining dis\_max with custom\_score query:
> 
> {  
> "query": {  
> "dis\_max": {  
> "queries": [{  
> "query\_string": {  
> "query": "Elastic",  
> "default\_operator": "AND",  
> "analyze\_wildcard": true,  
> "default\_field": "name"  
> }  
> },{  
> "custom\_score": {  
> "query": {  
> "query\_string": {  
> "query": "Elastic",  
> "default\_operator": "AND",  
> "analyze\_wildcard": true,  
> "default\_field": "surname"  
> }  
> },  
> "script" : "\_score \* doc['scoreBoosterForSurname'].value"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> Here is a full example: [custom\_boost.sh · GitHub](https://gist.github.com/3990458)
> 
> On Wednesday, October 31, 2012 11:58:56 AM UTC-4, dzintarsrerihs wrote:
> 
> > Hello!
> > 
> > At this point in my app I need to solve scoring implementation task. What  
> > I need to achieve is that - if term is found in \*Surname \*field, it can  
> > get custom boost that is taken from _scoreBoostForSurname_ otherwise (if  
> > term found in _Name_ then _scoreBoostForSurname_ value is not used)  
> > Lets assume I have simple three documents:  
> > {  
> > "thisID": "1",  
> > "name": "Elastic",  
> > "surname": "Search",  
> > "scoreBoosterForSurname": "0"  
> > }  
> > {  
> > "thisID": "2",  
> > "name": "Search",  
> > "surname": "Elastic",  
> > "scoreBoosterForSurname": "10"  
> > }  
> > {  
> > "thisID": "3",  
> > "name": "Search",  
> > "surname": "Elastic",  
> > "scoreBoosterForSurname": "5"  
> > }  
> > And now lets assume I'm querying these documents.  
> > '  
> > {  
> > query: {bool: {must: [{query\_string: {  
> > query: "Elastic",  
> > default\_operator: "AND",  
> > analyze\_wildcard: true,  
> > fields: [  
> > "name",  
> > "surname",  
> > ]  
> > }}]}}  
> > }  
> > '  
> > What I prefer is that if ordered by score results are ordered and  
> > returned to me in this order 2,3,1 (values from \*thisID \*field)
> > 
> > So what I assume I need here is not custom score, but custom field  
> > boosting. Is it really achievable at the moment in Elastic Search? Or is  
> > there something about custom scoring or even Elastics scoring I don't know  
> > yet?
> > 
> > I checked on nested objects, but does help to solve my task as well as  
> > index with child elements as child elements are not included in final  
> > result when querying parent.
> > 
> > The other options I consider is to use custom score with duplicating user  
> > entries (documents) and then removing duplicated results at my application  
> > side.

--

---

<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:06am UTC](https://discuss.elastic.co/t/custom-scoring-if-term-found-in-specific-field/9512/5 "2017-07-06T03:06:34Z")

</div>


