|
| 1 | +[[query-dsl-shape-query]] |
| 2 | +[role="xpack"] |
| 3 | +[testenv="basic"] |
| 4 | +=== Shape query |
| 5 | +++++ |
| 6 | +<titleabbrev>Shape</titleabbrev> |
| 7 | +++++ |
| 8 | + |
| 9 | +Queries documents that contain fields indexed using the `shape` type. |
| 10 | + |
| 11 | +Requires the <<shape,`shape` Mapping>>. |
| 12 | + |
| 13 | +The query supports two ways of defining the target shape, either by |
| 14 | +providing a whole shape definition, or by referencing the name, or id, of a shape |
| 15 | +pre-indexed in another index. Both formats are defined below with |
| 16 | +examples. |
| 17 | + |
| 18 | +==== Inline Shape Definition |
| 19 | + |
| 20 | +Similar to the `geo_shape` query, the `shape` query uses |
| 21 | +http://www.geojson.org[GeoJSON] or |
| 22 | +https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry[Well Known Text] |
| 23 | +(WKT) to represent shapes. |
| 24 | + |
| 25 | +Given the following index: |
| 26 | + |
| 27 | +[source,js] |
| 28 | +-------------------------------------------------- |
| 29 | +PUT /example |
| 30 | +{ |
| 31 | + "mappings": { |
| 32 | + "properties": { |
| 33 | + "geometry": { |
| 34 | + "type": "shape" |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +
|
| 40 | +POST /example/_doc?refresh |
| 41 | +{ |
| 42 | + "name": "Lucky Landing", |
| 43 | + "location": { |
| 44 | + "type": "point", |
| 45 | + "coordinates": [1355.400544, 5255.530286] |
| 46 | + } |
| 47 | +} |
| 48 | +-------------------------------------------------- |
| 49 | +// CONSOLE |
| 50 | +// TESTSETUP |
| 51 | + |
| 52 | +The following query will find the point using the Elasticsearch's |
| 53 | +`envelope` GeoJSON extension: |
| 54 | + |
| 55 | +[source,js] |
| 56 | +-------------------------------------------------- |
| 57 | +GET /example/_search |
| 58 | +{ |
| 59 | + "query":{ |
| 60 | + "shape": { |
| 61 | + "geometry": { |
| 62 | + "shape": { |
| 63 | + "type": "envelope", |
| 64 | + "coordinates" : [[1355.0, 5355.0], [1400.0, 5200.0]] |
| 65 | + }, |
| 66 | + "relation": "within" |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +-------------------------------------------------- |
| 72 | +// CONSOLE |
| 73 | + |
| 74 | +==== Pre-Indexed Shape |
| 75 | + |
| 76 | +The Query also supports using a shape which has already been indexed in |
| 77 | +another index. This is particularly useful for when |
| 78 | +you have a pre-defined list of shapes which are useful to your |
| 79 | +application and you want to reference this using a logical name (for |
| 80 | +example 'New Zealand') rather than having to provide their coordinates |
| 81 | +each time. In this situation it is only necessary to provide: |
| 82 | + |
| 83 | +* `id` - The ID of the document that containing the pre-indexed shape. |
| 84 | +* `index` - Name of the index where the pre-indexed shape is. Defaults |
| 85 | +to 'shapes'. |
| 86 | +* `path` - The field specified as path containing the pre-indexed shape. |
| 87 | +Defaults to 'shape'. |
| 88 | +* `routing` - The routing of the shape document if required. |
| 89 | + |
| 90 | +The following is an example of using the Filter with a pre-indexed |
| 91 | +shape: |
| 92 | + |
| 93 | +[source,js] |
| 94 | +-------------------------------------------------- |
| 95 | +PUT /shapes |
| 96 | +{ |
| 97 | + "mappings": { |
| 98 | + "properties": { |
| 99 | + "geometry": { |
| 100 | + "type": "shape" |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +
|
| 106 | +PUT /shapes/_doc/footprint |
| 107 | +{ |
| 108 | + "geometry": { |
| 109 | + "type": "envelope", |
| 110 | + "coordinates" : [[1355.0, 5355.0], [1400.0, 5200.0]] |
| 111 | + } |
| 112 | +} |
| 113 | +
|
| 114 | +GET /example/_search |
| 115 | +{ |
| 116 | + "query": { |
| 117 | + "shape": { |
| 118 | + "geometry": { |
| 119 | + "indexed_shape": { |
| 120 | + "index": "shapes", |
| 121 | + "id": "footprint", |
| 122 | + "path": "geometry" |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +-------------------------------------------------- |
| 129 | +// CONSOLE |
| 130 | + |
| 131 | +==== Spatial Relations |
| 132 | + |
| 133 | +The following is a complete list of spatial relation operators available: |
| 134 | + |
| 135 | +* `INTERSECTS` - (default) Return all documents whose `geo_shape` field |
| 136 | +intersects the query geometry. |
| 137 | +* `DISJOINT` - Return all documents whose `geo_shape` field |
| 138 | +has nothing in common with the query geometry. |
| 139 | +* `WITHIN` - Return all documents whose `geo_shape` field |
| 140 | +is within the query geometry. |
| 141 | + |
| 142 | +[float] |
| 143 | +==== Ignore Unmapped |
| 144 | + |
| 145 | +When set to `true` the `ignore_unmapped` option will ignore an unmapped field |
| 146 | +and will not match any documents for this query. This can be useful when |
| 147 | +querying multiple indexes which might have different mappings. When set to |
| 148 | +`false` (the default value) the query will throw an exception if the field |
| 149 | +is not mapped. |
0 commit comments