# Geo\_point sort by distance

**URL:** <https://discuss.elastic.co/t/geo-point-sort-by-distance/201804>\
**Category:** Elasticsearch\
**Created:** [October 1, 2019, 1:10pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804 "2019-10-01T13:10:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrezenzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrezenzen/32/76681_2.png) [@andrezenzen](https://discuss.elastic.co/u/andrezenzen)\
**Post date:** [October 1, 2019, 1:10pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/1 "2019-10-01T13:10:32Z")

</div>

I would like to know what would be the best way to perform a search and performing a sorting by distance, today \_geo\_distance filters and does not sort. What would be the best solution in which you need to perform two "should" queries and return sorted by distance.

GET places/\_search  
{  
"query": {  
"bool": {  
"should": [  
{  
"match": {  
"formattedAddress": {  
"query": "santa marta",  
"operator": "and"  
}  
}  
},  
{  
"geo\_distance": {  
"distance": "5km",  
"coords": {  
"lat": -27.87,  
"lon": -54.43  
}  
}  
}  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [October 1, 2019, 1:15pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/2 "2019-10-01T13:15:45Z")

</div>

The best ways is using geo distance sorting:

[https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#geo-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#geo-sorting)

---

<div class="post-metadata">

**Author:** ![andrezenzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrezenzen/32/76681_2.png) [@andrezenzen](https://discuss.elastic.co/u/andrezenzen)\
**Post date:** [October 1, 2019, 1:19pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/3 "2019-10-01T13:19:12Z")

</div>

This api of the sort "\_geo\_distance" does not sort but filters in all the tests I performed, I would like to perform the search in the fields informed in the search and sort by the search results.

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [October 1, 2019, 1:27pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/4 "2019-10-01T13:27:14Z")

</div>

What do you mean with filter? Could you provide an example? I would expect that the query bellow would apply the `geo_distance` and match `filters` and then it would sort those result by distance:

```auto
GET places/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "formattedAddress": {
              "query": "santa marta",
              "operator": "and"
            }
          }
        },
        {
          "geo_distance": {
            "distance": "5km",
            "coords": {
              "lat": -27.87,
              "lon": -54.43
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "coords": {
          "lat": -27.87,
          "lon": -54.43
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![andrezenzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrezenzen/32/76681_2.png) [@andrezenzen](https://discuss.elastic.co/u/andrezenzen)\
**Post date:** [October 1, 2019, 1:41pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/5 "2019-10-01T13:41:47Z")

</div>

otherwise, how should you query to search for a term and give relevance in terms that the distance is close.

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [October 1, 2019, 1:43pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/6 "2019-10-01T13:43:39Z")

</div>

Just remove the geo\_distance filter:

```auto
GET places/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "formattedAddress": {
              "query": "santa marta",
              "operator": "and"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "coords": {
          "lat": -27.87,
          "lon": -54.43
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![andrezenzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrezenzen/32/76681_2.png) [@andrezenzen](https://discuss.elastic.co/u/andrezenzen)\
**Post date:** [October 1, 2019, 1:48pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/7 "2019-10-01T13:48:53Z")

</div>

Thank you very much

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 1, 2019, 2:00pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/8 "2019-10-01T14:00:23Z")

</div>

I'd also recommend filtering a bit by distance first.  
I mean that if you have 10 million documents matching `santa marta`, it means that you have to sort 10 million points. That could take some time.

I did not test recently with all the optimizations that went into the geo part, but my experience has been to add a geo filter as well to narrow down the number of documents to sort on.  
For example, if you know that you are not going to give results which are more than 10km away from `-54.43,-27.87`, you can add a filter of 10km range around this point.

This is what I described in [this blog post](https://www.elastic.co/fr/blog/enriching-your-postal-addresses-with-the-elastic-stack-part-2). Have a look at " Searching by Geo Point" part.

HTH. @Ignacio_Vera correct me if this is not needed anymore with some internal optimizations.

---

<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:** [October 29, 2019, 2:00pm UTC](https://discuss.elastic.co/t/geo-point-sort-by-distance/201804/9 "2019-10-29T14:00:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
