geo-shape-query.asciidoc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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` Filter uses
  15. http://www.geojson.org[GeoJSON] to represent shapes.
  16. Given a document that looks like this:
  17. [source,js]
  18. --------------------------------------------------
  19. {
  20. "name": "Wind & Wetter, Berlin, Germany",
  21. "location": {
  22. "type": "Point",
  23. "coordinates": [13.400544, 52.530286]
  24. }
  25. }
  26. --------------------------------------------------
  27. The following query will find the point using the Elasticsearch's
  28. `envelope` GeoJSON extension:
  29. [source,js]
  30. --------------------------------------------------
  31. GET /_search
  32. {
  33. "query":{
  34. "bool": {
  35. "must": {
  36. "match_all": {}
  37. },
  38. "filter": {
  39. "geo_shape": {
  40. "location": {
  41. "shape": {
  42. "type": "envelope",
  43. "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  44. },
  45. "relation": "within"
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. // CONSOLE
  54. ==== Pre-Indexed Shape
  55. The Query also supports using a shape which has already been indexed in
  56. another index and/or index type. This is particularly useful for when
  57. you have a pre-defined list of shapes which are useful to your
  58. application and you want to reference this using a logical name (for
  59. example 'New Zealand') rather than having to provide their coordinates
  60. each time. In this situation it is only necessary to provide:
  61. * `id` - The ID of the document that containing the pre-indexed shape.
  62. * `index` - Name of the index where the pre-indexed shape is. Defaults
  63. to 'shapes'.
  64. * `type` - Index type where the pre-indexed shape is.
  65. * `path` - The field specified as path containing the pre-indexed shape.
  66. Defaults to 'shape'.
  67. The following is an example of using the Filter with a pre-indexed
  68. shape:
  69. [source,js]
  70. --------------------------------------------------
  71. GET /_search
  72. {
  73. "query": {
  74. "bool": {
  75. "must": {
  76. "match_all": {}
  77. },
  78. "filter": {
  79. "geo_shape": {
  80. "location": {
  81. "indexed_shape": {
  82. "id": "DEU",
  83. "type": "countries",
  84. "index": "shapes",
  85. "path": "location"
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. ==== Spatial Relations
  96. The <<spatial-strategy, geo_shape strategy>> mapping parameter determines
  97. which spatial relation operators may be used at search time.
  98. The following is a complete list of spatial relation operators available:
  99. * `INTERSECTS` - (default) Return all documents whose `geo_shape` field
  100. intersects the query geometry.
  101. * `DISJOINT` - Return all documents whose `geo_shape` field
  102. has nothing in common with the query geometry.
  103. * `WITHIN` - Return all documents whose `geo_shape` field
  104. is within the query geometry.
  105. * `CONTAINS` - Return all documents whose `geo_shape` field
  106. contains the query geometry.
  107. [float]
  108. ==== Ignore Unmapped
  109. When set to `true` the `ignore_unmapped` option will ignore an unmapped field
  110. and will not match any documents for this query. This can be useful when
  111. querying multiple indexes which might have different mappings. When set to
  112. `false` (the default value) the query will throw an exception if the field
  113. is not mapped.