Geo-shape query

Filter documents indexed using the geo_shape or geo_point type.

Requires the geo_shape Mapping or the geo_point Mapping.

The geo_shape query uses the same grid square representation as the geo_shape mapping to find documents that have a shape that intersects with the query shape. It will also use the same Prefix Tree configuration as defined for the field mapping.

The query supports two ways of defining the query shape, either by providing a whole shape definition, or by referencing the name of a shape pre-indexed in another index. Both formats are defined below with examples.

Inline Shape Definition

Similar to the geo_shape type, the geo_shape query uses GeoJSON to represent shapes.

Given the following index with locations as geo_shape fields:

  1. PUT /example
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }
  11. POST /example/_doc?refresh
  12. {
  13. "name": "Wind & Wetter, Berlin, Germany",
  14. "location": {
  15. "type": "point",
  16. "coordinates": [ 13.400544, 52.530286 ]
  17. }
  18. }

The following query will find the point using Elasticsearch’s envelope GeoJSON extension:

  1. GET /example/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_shape": {
  10. "location": {
  11. "shape": {
  12. "type": "envelope",
  13. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  14. },
  15. "relation": "within"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

The above query can, similarly, be queried on geo_point fields.

  1. PUT /example_points
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. PUT /example_points/_doc/1?refresh
  12. {
  13. "name": "Wind & Wetter, Berlin, Germany",
  14. "location": [13.400544, 52.530286]
  15. }

Using the same query, the documents with matching geo_point fields are returned.

  1. GET /example_points/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match_all": {}
  7. },
  8. "filter": {
  9. "geo_shape": {
  10. "location": {
  11. "shape": {
  12. "type": "envelope",
  13. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  14. },
  15. "relation": "intersects"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  1. {
  2. "took" : 17,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "example_points",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "name": "Wind & Wetter, Berlin, Germany",
  24. "location": [13.400544, 52.530286]
  25. }
  26. }
  27. ]
  28. }
  29. }

Pre-Indexed Shape

The query also supports using a shape which has already been indexed in another index. This is particularly useful for when you have a pre-defined list of shapes and you want to reference the list using a logical name (for example New Zealand) rather than having to provide coordinates each time. In this situation, it is only necessary to provide:

  • id - The ID of the document that containing the pre-indexed shape.
  • index - Name of the index where the pre-indexed shape is. Defaults to shapes.
  • path - The field specified as path containing the pre-indexed shape. Defaults to shape.
  • routing - The routing of the shape document if required.

The following is an example of using the Filter with a pre-indexed shape:

  1. PUT /shapes
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }
  11. PUT /shapes/_doc/deu
  12. {
  13. "location": {
  14. "type": "envelope",
  15. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  16. }
  17. }
  18. GET /example/_search
  19. {
  20. "query": {
  21. "bool": {
  22. "filter": {
  23. "geo_shape": {
  24. "location": {
  25. "indexed_shape": {
  26. "index": "shapes",
  27. "id": "deu",
  28. "path": "location"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }

Spatial Relations

The geo_shape strategy mapping parameter determines which spatial relation operators may be used at search time.

The following is a complete list of spatial relation operators available when searching a field of type geo_shape:

  • INTERSECTS - (default) Return all documents whose geo_shape field intersects the query geometry.
  • DISJOINT - Return all documents whose geo_shape field has nothing in common with the query geometry.
  • WITHIN - Return all documents whose geo_shape field is within the query geometry.
  • CONTAINS - Return all documents whose geo_shape field contains the query geometry.

When searching a field of type geo_point there is a single supported spatial relation operator:

  • INTERSECTS - (default) Return all documents whose geo_point field intersects the query geometry.

Ignore Unmapped

When set to true the ignore_unmapped option will ignore an unmapped field and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to false (the default value) the query will throw an exception if the field is not mapped.

Shape Types supported for Geo-Point

When searching a field of type geo_point the following shape types are not supported:

  • POINT
  • LINE
  • MULTIPOINT
  • MULTILINE

Notes

  • Geo-shape queries on geo-shapes implemented with PrefixTrees will not be executed if search.allow_expensive_queries is set to false.
  • When data is indexed in a geo_shape field as an array of shapes, the arrays are treated as one shape. For this reason, the following requests are equivalent.
  1. PUT /test/_doc/1
  2. {
  3. "location": [
  4. {
  5. "coordinates": [46.25,20.14],
  6. "type": "point"
  7. },
  8. {
  9. "coordinates": [47.49,19.04],
  10. "type": "point"
  11. }
  12. ]
  13. }
  1. PUT /test/_doc/1
  2. {
  3. "location":
  4. {
  5. "coordinates": [[46.25,20.14],[47.49,19.04]],
  6. "type": "multipoint"
  7. }
  8. }