# Computing the nearest distance from an array of properties containing a geo\_point

**URL:** <https://discuss.elastic.co/t/computing-the-nearest-distance-from-an-array-of-properties-containing-a-geo-point/13270>\
**Category:** Elasticsearch\
**Created:** [August 20, 2013, 12:38pm UTC](https://discuss.elastic.co/t/computing-the-nearest-distance-from-an-array-of-properties-containing-a-geo-point/13270 "2013-08-20T12:38:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicktackes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicktackes/32/1174_2.png) [@nicktackes](https://discuss.elastic.co/u/nicktackes)\
**Post date:** [August 20, 2013, 12:38pm UTC](https://discuss.elastic.co/t/computing-the-nearest-distance-from-an-array-of-properties-containing-a-geo-point/13270/1 "2013-08-20T12:38:43Z")

</div>

Hello,

I am indexing documents that contain multiple locations. Nested within the  
location property, there is a geo\_point property called geo.

I would like to return results containing the calculated distance of the  
nearest geo distance to a given lat/lon

{  
mappings:{"clinicaltrial":{"properties":{  
"armGroup":{"type":"string"},  
"briefSummary":{"type":"string"},  
"briefTitle":{"type":"string"},  
"eligibility":{"type":"string", index:"not\_analyzed"},  
"condition":{"type":"string", index:"not\_analyzed"},  
"exclusionCriteria":{"type":"string", index:"not\_analyzed"},  
"firstReceivedDate":{"type":"string"},  
"gender":{"type":"string"},  
"id":{"type":"string", index:"not\_analyzed"},  
"inclusionCriteria":{"type":"string"},  
"intervention":{  
"properties":{  
"intervention\_name":{"type":"string",  
index:"not\_analyzed"},  
"intervention\_type":{"type":"string",  
index:"not\_analyzed"},  
"description":{"type":"string"}  
}  
},  
"keyword":{"type":"string", index:"not\_analyzed"},  
"lastChangedDate":{"type":"string"},  
"location":{  
"properties":{  
"facility":{  
"properties":{  
"name":{"type":"string",  
index:"not\_analyzed"},  
"address":{  
"properties":{  
"city":{"type":"string",  
index:"not\_analyzed"},  
"state":{"type":"string",  
index:"not\_analyzed"},  
"zip":{"type":"string",  
index:"not\_analyzed"},  
"country":{"type":"string",  
index:"not\_analyzed"}  
}  
},  
"geo":{  
"type":"geo\_point"  
}  
}  
}  
}  
},  
"maxAge":{"type":"long"},  
"mboost":{"type":"long"},  
"medicalGroup":{"type":"string"},  
"minAge":{"type":"long"},  
"phase":{"type":"string"},  
"startDate":{"type":"string"},  
"status":{"type":"string"},  
"studyDesign":{"type":"string"},  
"studyId":{"type":"string"},  
"studyType":{"type":"string"},  
"summary":{"type":"string"},  
"title":{"type":"string"},  
"type":{"type":"string"},  
"verificationDate":{"type":"string"}}}}  
}

In my query, I am using script\_fields to compute distance, How can I modify  
the query so that it returns the nearest computed distance from the  
location array?

{"query":{  
"custom\_score":{  
"query":{  
"match\_all":{}  
},  
"script":"\_score + doc['mboost'].value"  
}  
}, "filter":{

```
"query":{
    "match_phrase":{
        "status":"Recruiting"
    }
}

```

}, "fields":"\_source",  
"script\_fields":{  
"distance":{  
"params":{  
"lat":42.3788774,  
"lon":-72.032366  
},  
"script":"doc['location.facility.geo'].empty ? null :  
doc['location.facility.geo'].distance(lat, lon)"  
}  
},  
"sort":{  
"\_score":"desc"  
}, "from":"0", "size":"10"  
}

--  
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:20am UTC](https://discuss.elastic.co/t/computing-the-nearest-distance-from-an-array-of-properties-containing-a-geo-point/13270/2 "2017-07-06T02:20:31Z")

</div>


