# Geoip distance query

**URL:** <https://discuss.elastic.co/t/geoip-distance-query/114983>\
**Category:** Elasticsearch\
**Created:** [January 11, 2018, 12:20am UTC](https://discuss.elastic.co/t/geoip-distance-query/114983 "2018-01-11T00:20:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bm1391](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@bm1391](https://discuss.elastic.co/u/bm1391)\
**Post date:** [January 11, 2018, 12:20am UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/1 "2018-01-11T00:20:36Z")

</div>

Hello all,

Ive been taking a look at the geoip distance query and it seems to be working fine. However, I'm looking for help to only return the results that are not within my distance query. So in this case, any results that are not within the 100 miles of my coordinates.

I have tried complementing this with the must\_not boolean and also the range query ( on the distance part) but no success

Here is query:

filter: {  
"geo\_distance" : {  
"distance" : "100mi" {  
"geoip.location": {  
"lat" : 42,  
"lon" : 60  
}  
}  
}  
}

Update:  
using ES 5.6.1:

Found out there is a geo\_distance\_range filter that has been deprecated in 6.0

---

<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:** [January 11, 2018, 7:07am UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/2 "2018-01-11T07:07:29Z")

</div>

I believe that the must not should work. What is the full query at the end?

---

<div class="post-metadata">

**Author:** ![bm1391](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@bm1391](https://discuss.elastic.co/u/bm1391)\
**Post date:** [January 11, 2018, 1:58pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/3 "2018-01-11T13:58:13Z")

</div>

Thank you for the reply:  
Here is the full query, again when I issue this query without the 'must\_not' it returns the documents within the 100 miles. But I can't seem to return the results that are not within the 100 miles. Thank you

```
{
"query": {
    "bool" : {
        "must_not" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "100mi",
                "geoip.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

```

}

---

<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:** [January 11, 2018, 2:33pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/4 "2018-01-11T14:33:59Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

But you added match all in the must not clause. Why this?

---

<div class="post-metadata">

**Author:** ![bm1391](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@bm1391](https://discuss.elastic.co/u/bm1391)\
**Post date:** [January 11, 2018, 2:49pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/5 "2018-01-11T14:49:38Z")

</div>

Sorry about that, edited. I believe this is resolved. Here is updated query. Thank you!

```
"query": {
  "bool" : {
    "must_not" : {
          "geo_distance" : {
              "distance" : "100mi",
              "geoip.location" : {
                  "lat" : 40,
                  "lon" : -70
              }
          }
    }
  }
}
```

---

<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:** [January 11, 2018, 3:01pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/6 "2018-01-11T15:01:56Z")

</div>

Does it work as expected with this query now?

If not, could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![bm1391](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@bm1391](https://discuss.elastic.co/u/bm1391)\
**Post date:** [January 11, 2018, 3:07pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/7 "2018-01-11T15:07:34Z")

</div>

Thanks for the heads up. And yes, query works great. GEOIP seems to be a bit inaccurate but it is only returning results outside of my mileage threshold set.

---

<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:** [February 8, 2018, 3:07pm UTC](https://discuss.elastic.co/t/geoip-distance-query/114983/8 "2018-02-08T15:07:40Z")

</div>

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