# Get results with distance filter

**URL:** <https://discuss.elastic.co/t/get-results-with-distance-filter/152245>\
**Category:** Elasticsearch\
**Created:** [October 12, 2018, 1:22pm UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245 "2018-10-12T13:22:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_mostafa](https://avatars.discourse-cdn.com/v4/letter/e/74df32/32.png) [@elastic\_mostafa](https://discuss.elastic.co/u/elastic_mostafa)\
**Post date:** [October 12, 2018, 1:22pm UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/1 "2018-10-12T13:22:26Z")

</div>

Hello ,  
I would like to get the results where coordinates are less than 15 km from this point for example: [35.3,-2.5] but I have this error : no [query] registered for [station\_location]

my request is :  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/1/51b7a61357d19999c568de5f0b6f9ba313c5d031.png)

here the response :  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/9/19aaffb3e4cf3b2830838c88bf29cd0afa4af781.png)

the index mapping :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/8/78b37fc663488194f8cb0aafe21500d8a57c0c5f.png)  
...  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/8/18a1319c330cf615a14922e5e70cdb329a73fa9e.png)

here on document example :  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/8/f8dcf88c7d79003011fc20cd8b8c9f71306514ac.png)

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 12, 2018, 1:29pm UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/2 "2018-10-12T13:29:03Z")

</div>

I am not sure I understand what your question or problem is. Could you please clarify?

---

<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 15, 2018, 8:08am UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/3 "2018-10-15T08:08:33Z")

</div>

I think the query is wrong and Elasticsearch is looking for a query called `station_location`. Instead you need to place the query `geo_distance`:

```auto
             "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "station_location" : {
                        "lat" : 35.5,
                        "lon" : -2.5
                    }
                }
            }

```

---

<div class="post-metadata">

**Author:** ![elastic\_mostafa](https://avatars.discourse-cdn.com/v4/letter/e/74df32/32.png) [@elastic\_mostafa](https://discuss.elastic.co/u/elastic_mostafa)\
**Post date:** [October 17, 2018, 3:48pm UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/4 "2018-10-17T15:48:50Z")

</div>

Thank you very much , it works well,

> ```
> {
> "query": {
> "bool" : {
> "must" : {
> "match_all" : {}
> },
> "filter" : {
> "geo_distance" : {
> "distance" : "10500m",
> "station_location" : "35.5,-2.5"
> }
> }
> }
> }
> 
> ```
> 
> }

**one last question how to get with the results the distance?**

---

<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 18, 2018, 6:41am UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/5 "2018-10-18T06:41:46Z")

</div>

To get the distance of the document in km:

```auto
{
  "_source": "*", 
  "script_fields": {
    "distance" : {
      "script" : {
        "source": "doc['station_location'].arcDistance(params.lat,params.lon) * 0.001",
        "params": {
          "lat": 2.27,
          "lon": 50.3
        }
      }
    }
  },
"query": {
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "10500m",
                "station_location" : "35.5,-2.5"
            }
        }
    }
}

```

---

<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:** [November 15, 2018, 6:41am UTC](https://discuss.elastic.co/t/get-results-with-distance-filter/152245/6 "2018-11-15T06:41:53Z")

</div>

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