geo-shape-query.asciidoc 4.7 KB

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