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

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!

Probably should specify the mapping as well.

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

}

}
}

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"
}
}
}
}
}
}

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

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

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.

1 Like

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!