# Aggregate min geo distance from a collection of location properties

**URL:** <https://discuss.elastic.co/t/aggregate-min-geo-distance-from-a-collection-of-location-properties/13304>\
**Category:** Elasticsearch\
**Created:** [August 22, 2013, 7:10pm UTC](https://discuss.elastic.co/t/aggregate-min-geo-distance-from-a-collection-of-location-properties/13304 "2013-08-22T19:10:17Z")\
**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 22, 2013, 7:10pm UTC](https://discuss.elastic.co/t/aggregate-min-geo-distance-from-a-collection-of-location-properties/13304/1 "2013-08-22T19:10:17Z")

</div>

Is there a way to promote a min geo distance to the root?

I have a location property of type object that contains a geo\_point and  
there is often multiple locations per root object.

```
            "location":{
                "type":"nested",
                "properties":{
                    "facility":{
                        "type":"nested",
                        "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",  
"include\_in\_root":true  
}  
}  
}  
}  
},

I have tried a variety of things within the script fields tag.  
Initially, i was not using the nested type and was expecting an array of  
doc.location.facility.geo to exist but that was never the case. It was  
always a single org.elasticsearch.index.mapper.geo.GeoPointDocFieldData  
object. I then switched to nested in hopes of being able to access the  
array of locations that i could then traverse to locate the min geo  
distance.

I see that there is more sophisticated aggregate support coming in 1.0, but  
in the current release is there any way other than a script field to do  
this?

initially attempted with mapping of type 'object'. I was expecting that i  
would get an array of geopointdocfields.... i dont get an array however

```
"script_fields":{
    "distance":{
        "params":{
            "lat":42.3788774,
            "lon":-72.032366
        },
        "script": "c=doc['location.facility.geo'].empty ? null : 

```

doc['location.facility.geo']; nearest\_distance=java.lang.Double.MAX\_VALUE;  
if(c!=null){for(cur : c) {dist=cur.distance(lat, lon); if(dist \<  
nearest\_distance){ nearest\_distance = dist}}}}; nearest\_distance"  
}  
},

tried this approach with nested type on both location and facility (as  
shown in the above mapping). I also was experimenting with include\_in\_root  
for geo.  
"script\_fields":{  
"distance":{  
"params":{  
"lat":42.3788774,  
"lon":-72.032366  
},  
"script": " c=doc[\u0027location\u0027].empty ? null:  
doc[\u0027location\u0027]; nearest\_distance=java.lang.Double.MAX\_VALUE;  
if(c!=null){for(cur : c)  
{if(cur["facility"]["geo"]!=null){dist=cur["facility"]["geo"].distance(lat,  
lon); if(dist \< nearest\_distance){ nearest\_distance = dist}}}};  
nearest\_distance"  
}  
},

--  
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/aggregate-min-geo-distance-from-a-collection-of-location-properties/13304/2 "2017-07-06T02:20:05Z")

</div>


