elasticsearchelasticsearch-geo-shape

Querying ElasticSearch for all documents with a Geo Point that intersects/within an indexed Geo Shape


I have a collection of geo_shapes pulled from a shape file that I have indexed into ES. I also have a large index of documents with geo_points. I'm trying to run a query that returns all the documents that have a point within a geo_shape, but keep getting zero results returned.

I'm pretty sure the mappings are correct, because I can do a query of a specific lat/long and successfully get back the geo_shape it is in. I can also do a bounding box search of the documents with geo_points and successfully get back a list of documents that match.

Here is the mapping for my geo_shapes:

{
    "geometry": {
        "properties": {
            "type": "geo_shape",
            "tree": "quadtree"
        }
    }
}

and this is the mapping for the documents with geo_points:

{
    "docs": {
        "location": {
            "properties": {
                "coordinates": {
                    "type": "geo_point",
                    "geohash": true
                }
            }
        }
    }
}

the query I'm trying to run is:

{
    "query": "geo_shape": {
        "location": {
            "indexed_shape": {
                "id": "shape_id",
                "type": "locations",
                "index": "index_name",
                "path": "geometry"
            }
        }
    }
}

I'm running the query against the specific index/type that contains the documents with geo_points. If I run it against the entire index then only the geo_shape document returns.


Solution

  • In order for this to work, the locations in your second index need to be mapped as geo_shape (type=Point) as well (not geo_point), like this:

    {
      "locations": {
        "location": {
          "properties": {
            "coordinates": {
              "type": "geo_shape",
              "tree": "quadtree",
              "precision": "1m"
            }
          }
        }
      }
    }
    

    Then your geo_shape query working on pre-indexed shapes will work as expected