# Is there a way to contribute to the relevance score of documents using distance?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-contribute-to-the-relevance-score-of-documents-using-distance/10140>\
**Category:** Elasticsearch\
**Created:** [December 20, 2012, 9:17pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-contribute-to-the-relevance-score-of-documents-using-distance/10140 "2012-12-20T21:17:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [December 20, 2012, 9:17pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-contribute-to-the-relevance-score-of-documents-using-distance/10140/1 "2012-12-20T21:17:34Z")

</div>

I want to boost the score of records that are close to some geo position.  
Elastic has filters for geo, but that just includes or excludes records.  
Is there a way to manipulate the score using the distance? I know there  
is a custom\_score query where I could specify something like "script" : "\_score

- doc['my\_numeric\_field'].value". The question is, how would I use the  
distance in the calculation, and not the value of the numeric field?

--

---

<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:** [December 20, 2012, 10:20pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-contribute-to-the-relevance-score-of-documents-using-distance/10140/2 "2012-12-20T22:20:01Z")

</div>

You can calculate the distance using the following  
expression: doc['your\_geo\_field'].distanceInKm(lat, lon) and then use it to  
figure out the boost factor. For example this script: "\_score \*  
(doc['your\_geo\_field'].distanceInKm(40, -70) \< 100 : 2.0: 1.0)" will apply  
boost 2.0 to all records that are located within 100km from the location  
(40, -70). Alternatively, you can use Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)to boost records using a geo\_distance filter. So, a faster equivalent of  
the above expression would be:

{  
"custom\_filters\_score": {  
"query": {  
.... some query ....  
},  
"filters": [{  
"filter": {  
"geo\_distance": {  
"distance": "100km",  
"pin.location": {  
"lat": 40,  
"lon": -70  
}  
}  
},  
"boost": 2.0  
}],  
"score\_mode": "first"  
}  
}

On Thursday, December 20, 2012 4:17:34 PM UTC-5, Mike wrote:

> I want to boost the score of records that are close to some geo position.  
> Elastic has filters for geo, but that just includes or excludes records.  
> Is there a way to manipulate the score using the distance? I know there  
> is a custom\_score query where I could specify something like "script" : "\_score
> 
> - doc['my\_numeric\_field'].value". The question is, how would I use the  
> distance in the calculation, and not the value of the numeric field?

--

---

<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:59am UTC](https://discuss.elastic.co/t/is-there-a-way-to-contribute-to-the-relevance-score-of-documents-using-distance/10140/3 "2017-07-06T02:59:00Z")

</div>


