# Sorting by \_geo\_distance returns the wrong order

**URL:** <https://discuss.elastic.co/t/sorting-by--geo-distance-returns-the-wrong-order/20231>\
**Category:** Elasticsearch\
**Created:** [October 14, 2014, 8:04am UTC](https://discuss.elastic.co/t/sorting-by--geo-distance-returns-the-wrong-order/20231 "2014-10-14T08:04:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soren\_Martius](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/soren_martius/32/1202_2.png) [@Soren\_Martius](https://discuss.elastic.co/u/Soren_Martius)\
**Post date:** [October 14, 2014, 8:04am UTC](https://discuss.elastic.co/t/sorting-by--geo-distance-returns-the-wrong-order/20231/1 "2014-10-14T08:04:00Z")

</div>

Hello,

In my Index i have several locations with some information and geo data. In  
the detail view of an location i try to query for locations which are near  
by the current one.  
I am confused, cause my query does not return the correct order of  
locations. If you check the geo locations you will see, that the order is  
wrong.  
The bool query is there for preventing getting the current location in the  
result ( identified by the locations slug ).

In the following example, please notice that the query is run with the geo  
point of "adlon hotel", so the first returned location should be "Hotel  
Prens Berlin Kreuzberg".

Here is the query:

curl -XGET '[http://localhost:9200/search/location/\_search](http://localhost:9200/search/location/_search)' -d  
'{"sort":[{"\_geo\_distance":{"pin":[52.51515,13.38019],"order":"asc","unit":"km","distance\_type":"arc"}}],"query":{"bool":{"must\_not":[{"match":{"slug":"adlon-hotel"}}]}},"size":3}'

Here is my mapping:

{

"search" : {

```
"mappings" : {

  "location" : {

    "_meta" : {

      "model" : 

```

"AnchorVentures\Bundle\LocationBundle\Entity\Location"

```
    },

    "_all" : {

      "auto_boost" : true

    },

    "properties" : {

      "address" : {

        "properties" : {

          "pin" : {

            "type" : "geo_point",

            "store" : true,

            "lat_lon" : true

          },

          "zipcode" : {

            "type" : "string",

            "store" : true

          }

        }

      },

      "city" : {

        "type" : "nested",

        "properties" : {

          "name" : {

            "type" : "string",

            "boost" : 10.0,

            "store" : true

          }

        }

      },

      "countedRatings" : {

        "type" : "string",

        "store" : true

      },

      "createdAt" : {

        "type" : "string",

        "store" : true

      },

      "details" : {

        "type" : "nested",

        "properties" : {

          "guestsQuantity" : {

            "type" : "string",

            "store" : true

          },

          "isCatering" : {

            "type" : "string",

            "store" : true

          },

          "isIndoor" : {

            "type" : "string",

            "store" : true

          },

          "isOutdoor" : {

            "type" : "string",

            "store" : true

          },

          "isOvernightAccommodation" : {

            "type" : "string",

            "store" : true

          },

          "price" : {

            "type" : "string",

            "store" : true

          },

          "roomQuantity" : {

            "type" : "string",

            "store" : true

          }

        }

      },

      "name" : {

        "type" : "string",

        "store" : true

      },

      "slug" : {

        "type" : "string",

        "store" : true

      },

      "updatedAt" : {

        "type" : "string",

        "store" : true

      }

    }

  }

}

```

}

}

And finally some documents

{

"took" : 2,

"timed\_out" : false,

"\_shards" : {

```
"total" : 5,

"successful" : 5,

"failed" : 0

```

},

"hits" : {

```
"total" : 10,

"max_score" : 1.0,

"hits" : [ {

  "_index" : "search",

  "_type" : "location",

  "_id" : "207",

  "_score" : 1.0,

  "_source":{"name":"Badeschiff","slug":"badeschiff","countedRatings":0,

```

"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":1,"isIndoor":true,"isOutdoor"  
:true,"isCatering":true,"isOvernightAccommodation":false,"roomQuantity":1,  
"guestsQuantity":50},"city":{"name":"Berlin"},"address":{"zipcode":"12435",  
"pin":"52.4970479,13.4525442"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "214",

  "_score" : 1.0,

  "_source":{"name":"Le Plat du Jour","slug":"le-plat-du-jour",

```

"countedRatings":0,"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":5,"isIndoor":true,"isOutdoor"  
:true,"isCatering":true,"isOvernightAccommodation":false,"roomQuantity":4,  
"guestsQuantity":50},"city":{"name":"Hamburg"},"address":{"zipcode":"20095",  
"pin":"53.54911,9.99439"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "206",

  "_score" : 1.0,

  "_source":{"name":"Spreequartier","slug":"spreequartier",

```

"countedRatings":0,"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":5,"isIndoor":true,"isOutdoor"  
:true,"isCatering":true,"isOvernightAccommodation":false,"roomQuantity":3,  
"guestsQuantity":80},"city":{"name":"Berlin"},"address":{"zipcode":"10245",  
"pin":"52.5018737,13.4484104"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "213",

  "_score" : 1.0,

  "_source":{"name":"Club Heim im Schanzenpark","slug":

```

"club-heim-im-schanzenpark","countedRatings":1,"createdAt":  
"2014-10-13T10:40:58+02:00","updatedAt":"2014-10-13T10:40:58+02:00",  
"details":{"price":5,"isIndoor":true,"isOutdoor":true,"isCatering":true,  
"isOvernightAccommodation":false,"roomQuantity":4,"guestsQuantity":150},  
"city":{"name":"Hamburg"},"address":{"zipcode":"20357","pin":  
"53.56473,9.97387"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "209",

  "_score" : 1.0,

  "_source":{"name":"Hotel Prens Berlin Kreuzberg","slug":

```

"hotel-prens-berlin-kreuzberg","countedRatings":0,"createdAt":  
"2014-10-13T10:40:58+02:00","updatedAt":"2014-10-13T10:40:58+02:00",  
"details":{"price":4,"isIndoor":true,"isOutdoor":true,"isCatering":true,  
"isOvernightAccommodation":false,"roomQuantity":50,"guestsQuantity":500},  
"city":{"name":"Berlin"},"address":{"zipcode":"10967","pin":  
"52.49517,13.42109"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "211",

  "_score" : 1.0,

  "_source":{"name":"Bahrenfelder Trabrennbahn","slug":

```

"bahrenfelder-trabrennbahn","countedRatings":1,"createdAt":  
"2014-10-13T10:40:58+02:00","updatedAt":"2014-10-13T10:40:58+02:00",  
"details":{"price":2,"isIndoor":true,"isOutdoor":false,"isCatering":true,  
"isOvernightAccommodation":false,"roomQuantity":1,"guestsQuantity":300},  
"city":{"name":"Hamburg"},"address":{"zipcode":"22761","pin":  
"53.57829,9.89291"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "208",

  "_score" : 1.0,

  "_source":{"name":"Die Fabrik","slug":"die-fabrik","countedRatings":0,

```

"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":4,"isIndoor":true,"isOutdoor"  
:true,"isCatering":true,"isOvernightAccommodation":false,"roomQuantity":20,  
"guestsQuantity":250},"city":{"name":"Berlin"},"address":{"zipcode":"10997",  
"pin":"52.49875,13.44583"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "210",

  "_score" : 1.0,

  "_source":{"name":"Ivbergs Hotel Charlottenburg","slug":

```

"ivbergs-hotel-charlottenburg","countedRatings":0,"createdAt":  
"2014-10-13T10:40:58+02:00","updatedAt":"2014-10-13T10:40:58+02:00",  
"details":{"price":3,"isIndoor":true,"isOutdoor":true,"isCatering":true,  
"isOvernightAccommodation":false,"roomQuantity":35,"guestsQuantity":150},  
"city":{"name":"Berlin"},"address":{"zipcode":"14057","pin":  
"52.507,13.29408"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "205",

  "_score" : 1.0,

  "_source":{"name":"Adlon Hotel","slug":"adlon-hotel","countedRatings":

```

4,"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":2,"isIndoor":true,"isOutdoor"  
:false,"isCatering":true,"isOvernightAccommodation":true,"roomQuantity":4,  
"guestsQuantity":150},"city":{"name":"Berlin"},"address":{"zipcode":"10117",  
"pin":"52.51515,13.38019"}}

```
}, {

  "_index" : "search",

  "_type" : "location",

  "_id" : "212",

  "_score" : 1.0,

  "_source":{"name":"Astrabar","slug":"astrabar","countedRatings":0,

```

"createdAt":"2014-10-13T10:40:58+02:00","updatedAt":  
"2014-10-13T10:40:58+02:00","details":{"price":1,"isIndoor":true,"isOutdoor"  
:false,"isCatering":true,"isOvernightAccommodation":false,"roomQuantity":1,  
"guestsQuantity":50},"city":{"name":"Hamburg"},"address":{"zipcode":"20359",  
"pin":"53.5488403,9.9607657"}}

```
} ]

```

}

}

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a20a6cbc-5b1b-4696-aa0b-17adca4c36ba%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a20a6cbc-5b1b-4696-aa0b-17adca4c36ba%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Soren\_Martius](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/soren_martius/32/1202_2.png) [@Soren\_Martius](https://discuss.elastic.co/u/Soren_Martius)\
**Post date:** [October 14, 2014, 4:57pm UTC](https://discuss.elastic.co/t/sorting-by--geo-distance-returns-the-wrong-order/20231/2 "2014-10-14T16:57:14Z")

</div>

i solved it myself. I needed to set slug in my mappings to "not\_analyzed"  
and i change the query to this one:

{  
"from": 0,  
"size": 3,  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must\_not": [  
{  
"match": {  
"slug": "adlon-hotel"  
}  
}  
]  
}  
},  
"filter": {  
"exists": {  
"field": "pin"  
}  
}  
}  
},  
"sort": [  
{  
"\_geo\_distance": {  
"pin": {  
"lat": 52.51515,  
"lon": 13.38019  
},  
"order": "asc",  
"unit": "km",  
"distance\_type": "arc"  
}  
}  
]  
}

Am Dienstag, 14. Oktober 2014 10:04:00 UTC+2 schrieb Sören Martius:

> Hello,
> 
> In my Index i have several locations with some information and geo data.  
> In the detail view of an location i try to query for locations which are  
> near by the current one.  
> I am confused, cause my query does not return the correct order of  
> locations. If you check the geo locations you will see, that the order is  
> wrong.  
> The bool query is there for preventing getting the current location in the  
> result ( identified by the locations slug ).
> 
> In the following example, please notice that the query is run with the geo  
> point of "adlon hotel", so the first returned location should be "Hotel  
> Prens Berlin Kreuzberg".
> 
> Here is the query:
> 
> curl -XGET '[http://localhost:9200/search/location/\_search](http://localhost:9200/search/location/_search)' -d  
> '{"sort":[{"\_geo\_distance":{"pin":[52.51515,13.38019],"order":"asc","unit":"km","distance\_type":"arc"}}],"query":{"bool":{"must\_not":[{"match":{"slug":"adlon-hotel"}}]}},"size":3}'
> 
> Here is my mapping:
> 
> {
> 
> "search" : {
> 
> ```
> "mappings" : {
> 
> "location" : {
> 
> "_meta" : {
> 
> "model" : 
> 
> ```
> 
> "AnchorVentures\Bundle\LocationBundle\Entity\Location"
> 
> ```
> },
> 
> "_all" : {
> 
> "auto_boost" : true
> 
> },
> 
> "properties" : {
> 
> "address" : {
> 
> "properties" : {
> 
> "pin" : {
> 
> "type" : "geo_point",
> 
> "store" : true,
> 
> "lat_lon" : true
> 
> },
> 
> "zipcode" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> }
> 
> }
> 
> },
> 
> "city" : {
> 
> "type" : "nested",
> 
> "properties" : {
> 
> "name" : {
> 
> "type" : "string",
> 
> "boost" : 10.0,
> 
> "store" : true
> 
> }
> 
> }
> 
> },
> 
> "countedRatings" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "createdAt" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "details" : {
> 
> "type" : "nested",
> 
> "properties" : {
> 
> "guestsQuantity" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "isCatering" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "isIndoor" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "isOutdoor" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "isOvernightAccommodation" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "price" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "roomQuantity" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> }
> 
> }
> 
> },
> 
> "name" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "slug" : {
> 
> "type" : "string",
> 
> "store" : true
> 
> },
> 
> "updatedAt" : {
> 
> ```
> 
> \<span style="color:  
> ...

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1438f59c-e889-42b3-ab98-274a0c7b4d48%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1438f59c-e889-42b3-ab98-274a0c7b4d48%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:56am UTC](https://discuss.elastic.co/t/sorting-by--geo-distance-returns-the-wrong-order/20231/3 "2017-07-06T00:56:14Z")

</div>


