# Get documents with a location + radius that contain a provided geopoint

**URL:** <https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279>\
**Category:** Elasticsearch\
**Created:** [August 4, 2016, 8:00pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279 "2016-08-04T20:00:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)\
**Post date:** [August 4, 2016, 8:00pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/1 "2016-08-04T20:00:28Z")

</div>

Here is a simple query

GET /sen/operatinglocation/\_search  
{  
"query": {  
"filtered" : {  
"query" : {  
"match\_all" : {}  
},  
"filter" : {  
"geo\_distance" : {  
"distance" : "20km",  
"location" : {  
"lat" : 33.737348,  
"lon" : -117.772003  
}  
}  
}  
}  
}  
}

How can I replace "20km" distance with a variable distance based on the document being searched?

Thanks!

---

<div class="post-metadata">

**Author:** ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)\
**Post date:** [August 4, 2016, 8:08pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/2 "2016-08-04T20:08:35Z")

</div>

Probably should specify the mapping as well.

POST /sen/  
{  
"mappings" : {  
"operatinglocation" : {  
"properties" : {  
"id" : {  
"type" : "integer"  
},  
"radius" : {  
"type" : "integer"  
},  
"location" : {  
"type" : "geo\_point"  
}  
}

```
}

```

}  
}

---

<div class="post-metadata">

**Author:** ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)\
**Post date:** [August 4, 2016, 9:09pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/3 "2016-08-04T21:09:54Z")

</div>

I've tried now shifting gears and storing the location as a geo\_shape. Store location as a geo\_shape of circle with the correct radius. This seems to make the most sense, and might be more optimized for ES (but I'm not an expert by any means). Unfortunately, using the online documentation, i get a null pointer exception when trying to query the data.

Here is some information:

The mapping:  
POST /sen/  
{  
"mappings" : {  
"operatinglocation" : {  
"properties" : {  
"id" : {  
"type" : "integer"  
},  
"location" : {  
"type" : "geo\_shape"  
}  
}

```
}

```

}  
}

Here is a document:  
POST /sen/operatinglocation/  
{  
"id" : 1,  
"location" : {  
"type" : "circle",  
"coordinates" : [-117.780551, 35.706162],  
"radius" : "25mi"  
}  
}

This is the query that returns a null pointer exception:  
GET /sen/operatinglocation/\_search  
{  
"query":{  
"bool": {  
"must": {  
"match\_all": {}  
},  
"filter": {  
"geo\_shape": {  
"location": {  
"shape": {  
"type": "circle",  
"coordinates" : [[33.737348, -117.772003]]  
},  
"relation": "within"  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 5, 2016, 3:59am UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/4 "2016-08-05T03:59:25Z")

</div>

First up, thanks for providing such an awesome post, with full reproduction!

Second, the docs could make this clearer, but this works;

```auto
GET /sen/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "point",
              "coordinates": 
                [ -117.780552,35.706161
              ]
            },
            "relation": "contains"
          }
        }
      }
    }
  }
}

```

Basically I changed the `relation` to a `contains`, because you are looking for a point that is contained within that shape. `within` and `contains` both mean; does the queried shape _entirely_ exist within the indexed shape. But the former is for shapes, the latter for points.  
You may want to consider looking at `intersects` as well.

I'll raise a GH issue to get the docs updated to make all this clearer too.

---

<div class="post-metadata">

**Author:** ![MatthewHartz](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@MatthewHartz](https://discuss.elastic.co/u/MatthewHartz)\
**Post date:** [August 5, 2016, 4:21pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/5 "2016-08-05T16:21:25Z")

</div>

> [@warkolm](#):
>
> GET /sen/\_search  
> {  
> "query": {  
> "bool": {  
> "must": {  
> "match\_all": {}  
> },  
> "filter": {  
> "geo\_shape": {  
> "location": {  
> "shape": {  
> "type": "point",  
> "coordinates":  
> [ -117.780552,35.706161  
> ]  
> },  
> "relation": "contains"  
> }  
> }  
> }  
> }  
> }  
> }

Thanks for the response. Yeah I think I figured it out yesterday with the whole "filtered" methodology, but I believe that is deprecated. But this should be what I need!

Thanks!

---

<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 5, 2017, 10:29pm UTC](https://discuss.elastic.co/t/get-documents-with-a-location-radius-that-contain-a-provided-geopoint/57279/6 "2017-07-05T22:29:45Z")

</div>


