geo-shape-query.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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` type.
  7. Requires the <<geo-shape,`geo_shape` Mapping>>.
  8. The `geo_shape` query uses the same grid square representation as the
  9. `geo_shape` mapping to find documents that have a shape that intersects
  10. with the query shape. It will also use the same Prefix Tree configuration
  11. as defined for the field mapping.
  12. The query supports two ways of defining the query shape, either by
  13. providing a whole shape definition, or by referencing the name of a shape
  14. pre-indexed in another index. Both formats are defined below with
  15. examples.
  16. ==== Inline Shape Definition
  17. Similar to the `geo_shape` type, the `geo_shape` query uses
  18. http://www.geojson.org[GeoJSON] to represent shapes.
  19. Given the following index:
  20. [source,js]
  21. --------------------------------------------------
  22. PUT /example
  23. {
  24. "mappings": {
  25. "properties": {
  26. "location": {
  27. "type": "geo_shape"
  28. }
  29. }
  30. }
  31. }
  32. POST /example/_doc?refresh
  33. {
  34. "name": "Wind & Wetter, Berlin, Germany",
  35. "location": {
  36. "type": "point",
  37. "coordinates": [13.400544, 52.530286]
  38. }
  39. }
  40. --------------------------------------------------
  41. // CONSOLE
  42. // TESTSETUP
  43. The following query will find the point using the Elasticsearch's
  44. `envelope` GeoJSON extension:
  45. [source,js]
  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. // CONSOLE
  70. ==== Pre-Indexed Shape
  71. The Query also supports using a shape which has already been indexed in
  72. another index. This is particularly useful for when
  73. you have a pre-defined list of shapes which are useful to your
  74. application and you want to reference this using a logical name (for
  75. example 'New Zealand') rather than having to provide their coordinates
  76. each time. In this situation it is only necessary to provide:
  77. * `id` - The ID of the document that containing the pre-indexed shape.
  78. * `index` - Name of the index where the pre-indexed shape is. Defaults
  79. to 'shapes'.
  80. * `path` - The field specified as path containing the pre-indexed shape.
  81. Defaults to 'shape'.
  82. * `routing` - The routing of the shape document if required.
  83. The following is an example of using the Filter with a pre-indexed
  84. shape:
  85. [source,js]
  86. --------------------------------------------------
  87. PUT /shapes
  88. {
  89. "mappings": {
  90. "properties": {
  91. "location": {
  92. "type": "geo_shape"
  93. }
  94. }
  95. }
  96. }
  97. PUT /shapes/_doc/deu
  98. {
  99. "location": {
  100. "type": "envelope",
  101. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  102. }
  103. }
  104. GET /example/_search
  105. {
  106. "query": {
  107. "bool": {
  108. "filter": {
  109. "geo_shape": {
  110. "location": {
  111. "indexed_shape": {
  112. "index": "shapes",
  113. "id": "deu",
  114. "path": "location"
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. --------------------------------------------------
  123. // CONSOLE
  124. ==== Spatial Relations
  125. The <<spatial-strategy, geo_shape strategy>> mapping parameter determines
  126. which spatial relation operators may be used at search time.
  127. The following is a complete list of spatial relation operators available:
  128. * `INTERSECTS` - (default) Return all documents whose `geo_shape` field
  129. intersects the query geometry.
  130. * `DISJOINT` - Return all documents whose `geo_shape` field
  131. has nothing in common with the query geometry.
  132. * `WITHIN` - Return all documents whose `geo_shape` field
  133. is within the query geometry.
  134. * `CONTAINS` - Return all documents whose `geo_shape` field
  135. contains the query geometry. Note: this is only supported using the
  136. `recursive` Prefix Tree Strategy deprecated[6.6]
  137. [float]
  138. ==== Ignore Unmapped
  139. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  140. and will not match any documents for this query. This can be useful when
  141. querying multiple indexes which might have different mappings. When set to
  142. `false` (the default value) the query will throw an exception if the field
  143. is not mapped.