shape-query.asciidoc 4.5 KB

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