geo-shape-query.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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,console]
  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. // 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. "bool": {
  50. "must": {
  51. "match_all": {}
  52. },
  53. "filter": {
  54. "geo_shape": {
  55. "location": {
  56. "shape": {
  57. "type": "envelope",
  58. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  59. },
  60. "relation": "within"
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. ==== Pre-Indexed Shape
  69. The Query also supports using a shape which has already been indexed in
  70. another index. This is particularly useful for when
  71. you have a pre-defined list of shapes which are useful to your
  72. application and you want to reference this using a logical name (for
  73. example 'New Zealand') rather than having to provide their coordinates
  74. each time. In this situation it is only necessary to provide:
  75. * `id` - The ID of the document that containing the pre-indexed shape.
  76. * `index` - Name of the index where the pre-indexed shape is. Defaults
  77. to 'shapes'.
  78. * `path` - The field specified as path containing the pre-indexed shape.
  79. Defaults to 'shape'.
  80. * `routing` - The routing of the shape document if required.
  81. The following is an example of using the Filter with a pre-indexed
  82. shape:
  83. [source,console]
  84. --------------------------------------------------
  85. PUT /shapes
  86. {
  87. "mappings": {
  88. "properties": {
  89. "location": {
  90. "type": "geo_shape"
  91. }
  92. }
  93. }
  94. }
  95. PUT /shapes/_doc/deu
  96. {
  97. "location": {
  98. "type": "envelope",
  99. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  100. }
  101. }
  102. GET /example/_search
  103. {
  104. "query": {
  105. "bool": {
  106. "filter": {
  107. "geo_shape": {
  108. "location": {
  109. "indexed_shape": {
  110. "index": "shapes",
  111. "id": "deu",
  112. "path": "location"
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. ==== Spatial Relations
  122. The <<spatial-strategy, geo_shape strategy>> mapping parameter determines
  123. which spatial relation operators may be used at search time.
  124. The following is a complete list of spatial relation operators available:
  125. * `INTERSECTS` - (default) Return all documents whose `geo_shape` field
  126. intersects the query geometry.
  127. * `DISJOINT` - Return all documents whose `geo_shape` field
  128. has nothing in common with the query geometry.
  129. * `WITHIN` - Return all documents whose `geo_shape` field
  130. is within the query geometry.
  131. * `CONTAINS` - Return all documents whose `geo_shape` field
  132. contains the query geometry. Note: this is only supported using the
  133. `recursive` Prefix Tree Strategy deprecated[6.6]
  134. [float]
  135. ==== Ignore Unmapped
  136. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  137. and will not match any documents for this query. This can be useful when
  138. querying multiple indexes which might have different mappings. When set to
  139. `false` (the default value) the query will throw an exception if the field
  140. is not mapped.