| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 | [[query-dsl-geo-shape-query]]=== Geo-shape query++++<titleabbrev>Geo-shape</titleabbrev>++++Filter documents indexed using the `geo_shape` or `geo_point` type.Requires the <<geo-shape,`geo_shape` Mapping>> or the <<geo-point,`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 intersectswith the query shape. It will also use the same Prefix Tree configurationas defined for the field mapping.The query supports two ways of defining the query shape, either byproviding a whole shape definition, or by referencing the name of a shapepre-indexed in another index. Both formats are defined below withexamples.==== Inline Shape DefinitionSimilar to the `geo_shape` type, the `geo_shape` query useshttp://geojson.org[GeoJSON] to represent shapes.Given the following index with locations as `geo_shape` fields:[source,console]--------------------------------------------------PUT /example{  "mappings": {    "properties": {      "location": {        "type": "geo_shape"      }    }  }}POST /example/_doc?refresh{  "name": "Wind & Wetter, Berlin, Germany",  "location": {    "type": "point",    "coordinates": [ 13.400544, 52.530286 ]  }}--------------------------------------------------// TESTSETUPThe following query will find the point using {es}'s `envelope` GeoJSON extension:[source,console]--------------------------------------------------GET /example/_search{  "query": {    "bool": {      "must": {        "match_all": {}      },      "filter": {        "geo_shape": {          "location": {            "shape": {              "type": "envelope",              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]            },            "relation": "within"          }        }      }    }  }}--------------------------------------------------The above query can, similarly, be queried on `geo_point` fields.[source,console]--------------------------------------------------PUT /example_points{  "mappings": {    "properties": {      "location": {        "type": "geo_point"      }    }  }}PUT /example_points/_doc/1?refresh{  "name": "Wind & Wetter, Berlin, Germany",  "location": [13.400544, 52.530286]}--------------------------------------------------// TEST[continued]Using the same query, the documents with matching `geo_point` fields are returned.[source,console]--------------------------------------------------GET /example_points/_search{  "query": {    "bool": {      "must": {        "match_all": {}      },      "filter": {        "geo_shape": {          "location": {            "shape": {              "type": "envelope",              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]            },            "relation": "intersects"          }        }      }    }  }}--------------------------------------------------// TEST[continued][source,console-result]--------------------------------------------------{  "took" : 17,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "example_points",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "name": "Wind & Wetter, Berlin, Germany",          "location": [13.400544, 52.530286]        }      }    ]  }}--------------------------------------------------// TESTRESPONSE[s/"took" : 17/"took" : $body.took/]==== Pre-Indexed ShapeThe 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-indexedshape:[source,console]--------------------------------------------------PUT /shapes{  "mappings": {    "properties": {      "location": {        "type": "geo_shape"      }    }  }}PUT /shapes/_doc/deu{  "location": {    "type": "envelope",    "coordinates" : [[13.0, 53.0], [14.0, 52.0]]  }}GET /example/_search{  "query": {    "bool": {      "filter": {        "geo_shape": {          "location": {            "indexed_shape": {              "index": "shapes",              "id": "deu",              "path": "location"            }          }        }      }    }  }}--------------------------------------------------==== Spatial RelationsThe <<spatial-strategy, 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` fieldintersects 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.[discrete]==== Ignore UnmappedWhen set to `true` the `ignore_unmapped` option will ignore an unmapped fieldand will not match any documents for this query. This can be useful whenquerying multiple indexes which might have different mappings. When set to`false` (the default value) the query will throw an exception if the fieldis not mapped.==== Shape Types supported for Geo-PointWhen searching a field of type `geo_point` the following shape types are not supported:* `POINT`* `LINE`* `MULTIPOINT`* `MULTILINE`[[geo-shape-query-notes]]==== Notes* Geo-shape queries on geo-shapes implemented with   <<prefix-trees, `PrefixTrees`>> will not be executed if   <<query-dsl-allow-expensive-queries, `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.[source,console]--------------------------------------------------PUT /test/_doc/1{  "location": [    {      "coordinates": [46.25,20.14],      "type": "point"    },    {      "coordinates": [47.49,19.04],      "type": "point"    }  ]}--------------------------------------------------[source,console]--------------------------------------------------PUT /test/_doc/1{  "location":     {      "coordinates": [[46.25,20.14],[47.49,19.04]],      "type": "multipoint"    }}--------------------------------------------------
 |