geo-shape-query.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. [[query-dsl-geo-shape-query]]
  2. === Geo-shape query
  3. ++++
  4. <titleabbrev>Geo-shape</titleabbrev>
  5. ++++
  6. Filter documents indexed using the `geo_shape` or `geo_point` type.
  7. Requires the <<geo-shape,`geo_shape` Mapping>> or the
  8. <<geo-point,`geo_point` Mapping>>.
  9. The `geo_shape` query uses the same grid square representation as the
  10. `geo_shape` mapping to find documents that have a shape that intersects
  11. with the query shape. It will also use the same Prefix Tree configuration
  12. as defined for the field mapping.
  13. The query supports two ways of defining the query shape, either by
  14. providing a whole shape definition, or by referencing the name of a shape
  15. pre-indexed in another index. Both formats are defined below with
  16. examples.
  17. ==== Inline Shape Definition
  18. Similar to the `geo_shape` type, the `geo_shape` query uses
  19. http://geojson.org[GeoJSON] to represent shapes.
  20. Given the following index with locations as `geo_shape` fields:
  21. [source,console]
  22. --------------------------------------------------
  23. PUT /example
  24. {
  25. "mappings": {
  26. "properties": {
  27. "location": {
  28. "type": "geo_shape"
  29. }
  30. }
  31. }
  32. }
  33. POST /example/_doc?refresh
  34. {
  35. "name": "Wind & Wetter, Berlin, Germany",
  36. "location": {
  37. "type": "point",
  38. "coordinates": [ 13.400544, 52.530286 ]
  39. }
  40. }
  41. --------------------------------------------------
  42. // TESTSETUP
  43. The following query will find the point using {es}'s `envelope` GeoJSON
  44. extension:
  45. [source,console]
  46. --------------------------------------------------
  47. GET /example/_search
  48. {
  49. "query": {
  50. "bool": {
  51. "must": {
  52. "match_all": {}
  53. },
  54. "filter": {
  55. "geo_shape": {
  56. "location": {
  57. "shape": {
  58. "type": "envelope",
  59. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  60. },
  61. "relation": "within"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. The above query can, similarly, be queried on `geo_point` fields.
  70. [source,console]
  71. --------------------------------------------------
  72. PUT /example_points
  73. {
  74. "mappings": {
  75. "properties": {
  76. "location": {
  77. "type": "geo_point"
  78. }
  79. }
  80. }
  81. }
  82. PUT /example_points/_doc/1?refresh
  83. {
  84. "name": "Wind & Wetter, Berlin, Germany",
  85. "location": [13.400544, 52.530286]
  86. }
  87. --------------------------------------------------
  88. // TEST[continued]
  89. Using the same query, the documents with matching `geo_point` fields are
  90. returned.
  91. [source,console]
  92. --------------------------------------------------
  93. GET /example_points/_search
  94. {
  95. "query": {
  96. "bool": {
  97. "must": {
  98. "match_all": {}
  99. },
  100. "filter": {
  101. "geo_shape": {
  102. "location": {
  103. "shape": {
  104. "type": "envelope",
  105. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  106. },
  107. "relation": "intersects"
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TEST[continued]
  116. [source,console-result]
  117. --------------------------------------------------
  118. {
  119. "took" : 17,
  120. "timed_out" : false,
  121. "_shards" : {
  122. "total" : 1,
  123. "successful" : 1,
  124. "skipped" : 0,
  125. "failed" : 0
  126. },
  127. "hits" : {
  128. "total" : {
  129. "value" : 1,
  130. "relation" : "eq"
  131. },
  132. "max_score" : 1.0,
  133. "hits" : [
  134. {
  135. "_index" : "example_points",
  136. "_id" : "1",
  137. "_score" : 1.0,
  138. "_source" : {
  139. "name": "Wind & Wetter, Berlin, Germany",
  140. "location": [13.400544, 52.530286]
  141. }
  142. }
  143. ]
  144. }
  145. }
  146. --------------------------------------------------
  147. // TESTRESPONSE[s/"took" : 17/"took" : $body.took/]
  148. ==== Pre-Indexed Shape
  149. The query also supports using a shape which has already been indexed in another
  150. index. This is particularly useful for when you have a pre-defined list of
  151. shapes and you want to reference the list using
  152. a logical name (for example 'New Zealand') rather than having to provide
  153. coordinates each time. In this situation, it is only necessary to provide:
  154. * `id` - The ID of the document that containing the pre-indexed shape.
  155. * `index` - Name of the index where the pre-indexed shape is. Defaults to
  156. 'shapes'.
  157. * `path` - The field specified as path containing the pre-indexed shape.
  158. Defaults to 'shape'.
  159. * `routing` - The routing of the shape document if required.
  160. The following is an example of using the Filter with a pre-indexed
  161. shape:
  162. [source,console]
  163. --------------------------------------------------
  164. PUT /shapes
  165. {
  166. "mappings": {
  167. "properties": {
  168. "location": {
  169. "type": "geo_shape"
  170. }
  171. }
  172. }
  173. }
  174. PUT /shapes/_doc/deu
  175. {
  176. "location": {
  177. "type": "envelope",
  178. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  179. }
  180. }
  181. GET /example/_search
  182. {
  183. "query": {
  184. "bool": {
  185. "filter": {
  186. "geo_shape": {
  187. "location": {
  188. "indexed_shape": {
  189. "index": "shapes",
  190. "id": "deu",
  191. "path": "location"
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. ==== Spatial Relations
  201. The <<spatial-strategy, geo_shape strategy>> mapping parameter determines which
  202. spatial relation operators may be used at search time.
  203. The following is a complete list of spatial relation operators available when
  204. searching a geo field:
  205. * `INTERSECTS` - (default) Return all documents whose `geo_shape` or `geo_point` field
  206. intersects the query geometry.
  207. * `DISJOINT` - Return all documents whose `geo_shape` or `geo_point` field has nothing in
  208. common with the query geometry.
  209. * `WITHIN` - Return all documents whose `geo_shape` or `geo_point` field is within the query
  210. geometry. Line geometries are not supported.
  211. * `CONTAINS` - Return all documents whose `geo_shape` or `geo_point` field contains the query
  212. geometry.
  213. [discrete]
  214. ==== Ignore Unmapped
  215. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  216. and will not match any documents for this query. This can be useful when
  217. querying multiple indexes which might have different mappings. When set to
  218. `false` (the default value) the query will throw an exception if the field
  219. is not mapped.
  220. [[geo-shape-query-notes]]
  221. ==== Notes
  222. * Geo-shape queries on geo-shapes implemented with
  223. <<prefix-trees, `PrefixTrees`>> will not be executed if
  224. <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set
  225. to false.
  226. * When data is indexed in a `geo_shape` field as an array of shapes, the arrays
  227. are treated as one shape. For this reason, the following requests are
  228. equivalent.
  229. [source,console]
  230. --------------------------------------------------
  231. PUT /test/_doc/1
  232. {
  233. "location": [
  234. {
  235. "coordinates": [46.25,20.14],
  236. "type": "point"
  237. },
  238. {
  239. "coordinates": [47.49,19.04],
  240. "type": "point"
  241. }
  242. ]
  243. }
  244. --------------------------------------------------
  245. [source,console]
  246. --------------------------------------------------
  247. PUT /test/_doc/1
  248. {
  249. "location":
  250. {
  251. "coordinates": [[46.25,20.14],[47.49,19.04]],
  252. "type": "multipoint"
  253. }
  254. }
  255. --------------------------------------------------