# Geo point distance with multiple locations in array

**URL:** <https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897>\
**Category:** Elasticsearch\
**Created:** [July 19, 2011, 4:48pm UTC](https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897 "2011-07-19T16:48:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sakaraut](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@sakaraut](https://discuss.elastic.co/u/sakaraut)\
**Post date:** [July 19, 2011, 4:48pm UTC](https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897/1 "2011-07-19T16:48:29Z")

</div>

I am trying to sort items which have location array. Each item can  
have more than one location and I am using the following sorting  
query:  
{

```
"sort" : [
    {
        "_geo_distance" : {
            "pin.location" : [60, 20],
            "order" : "asc",
            "unit" : "km"
        }
    }
],

```

"query": {"match\_all": {}}  
}'

Then I read the "sort" value to get distance of item location's to the  
given location(lat:20,long:60). This works correctly if the item has  
only one location but if there is more than one as in here:  
{  
"message" : "Locations",  
"pin": {  
"location": [[60,20],[50,20]]  
}  
}  
The "sort" value becomes 1045.90911935061 not 0.0 as expected. It  
doesn't matter if I am using desc or asc as the sorting order, but it  
still gives the same output and calculates the distance from the last  
array item.

Is there a way to get the distance to the closest location from that  
array and sort items using some other query?  
(Other than create index entries for each location)

---

<div class="post-metadata">

**Author:** ![sakaraut](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@sakaraut](https://discuss.elastic.co/u/sakaraut)\
**Post date:** [July 19, 2011, 4:50pm UTC](https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897/2 "2011-07-19T16:50:05Z")

</div>

Here is the link for curl queries and data: [Sorting geopoints in array · GitHub](https://gist.github.com/1093017)

On Jul 19, 7:48 pm, sakaraut [sakari.rautiai...@gmail.com](mailto:sakari.rautiai...@gmail.com) wrote:

> I am trying to sort items which have location array. Each item can  
> have more than one location and I am using the following sorting  
> query:  
> {
> 
> ```
> "sort" : [
> {
> "_geo_distance" : {
> "pin.location" : [60, 20],
> "order" : "asc",
> "unit" : "km"
> }
> }
> ],
> 
> ```
> 
> "query": {"match\_all": {}}
> 
> }'
> 
> Then I read the "sort" value to get distance of item location's to the  
> given location(lat:20,long:60). This works correctly if the item has  
> only one location but if there is more than one as in here:  
> {  
> "message" : "Locations",  
> "pin": {  
> "location": [[60,20],[50,20]]  
> }}
> 
> The "sort" value becomes 1045.90911935061 not 0.0 as expected. It  
> doesn't matter if I am using desc or asc as the sorting order, but it  
> still gives the same output and calculates the distance from the last  
> array item.
> 
> Is there a way to get the distance to the closest location from that  
> array and sort items using some other query?  
> (Other than create index entries for each location)

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 19, 2011, 9:14pm UTC](https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897/3 "2011-07-19T21:14:33Z")

</div>

Sort has little meaning when there are multiple values, so its not really  
aimed at solving that. If you want specific location to sort on, then index  
it separately.

On Tue, Jul 19, 2011 at 7:50 PM, sakaraut [sakari.rautiainen@gmail.com](mailto:sakari.rautiainen@gmail.com)wrote:

> Here is the link for curl queries and data:  
> [Sorting geopoints in array · GitHub](https://gist.github.com/1093017)
> 
> On Jul 19, 7:48 pm, sakaraut [sakari.rautiai...@gmail.com](mailto:sakari.rautiai...@gmail.com) wrote:
> 
> > I am trying to sort items which have location array. Each item can  
> > have more than one location and I am using the following sorting  
> > query:  
> > {
> > 
> > ```
> > "sort" : [
> > {
> > "_geo_distance" : {
> > "pin.location" : [60, 20],
> > "order" : "asc",
> > "unit" : "km"
> > }
> > }
> > ],
> > 
> > ```
> > 
> > "query": {"match\_all": {}}
> > 
> > }'
> > 
> > Then I read the "sort" value to get distance of item location's to the  
> > given location(lat:20,long:60). This works correctly if the item has  
> > only one location but if there is more than one as in here:  
> > {  
> > "message" : "Locations",  
> > "pin": {  
> > "location": [[60,20],[50,20]]  
> > }}
> > 
> > The "sort" value becomes 1045.90911935061 not 0.0 as expected. It  
> > doesn't matter if I am using desc or asc as the sorting order, but it  
> > still gives the same output and calculates the distance from the last  
> > array item.
> > 
> > Is there a way to get the distance to the closest location from that  
> > array and sort items using some other query?  
> > (Other than create index entries for each location)

---

<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, 4:00am UTC](https://discuss.elastic.co/t/geo-point-distance-with-multiple-locations-in-array/4897/4 "2017-07-06T04:00:08Z")

</div>


