nested-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. [[query-dsl-nested-query]]
  2. === Nested query
  3. ++++
  4. <titleabbrev>Nested</titleabbrev>
  5. ++++
  6. Wraps another query to search <<nested,nested>> fields.
  7. The `nested` query searches nested field objects as if they were indexed as
  8. separate documents. If an object matches the search, the `nested` query returns
  9. the root parent document.
  10. [[nested-query-ex-request]]
  11. ==== Example request
  12. [[nested-query-index-setup]]
  13. ===== Index setup
  14. To use the `nested` query, your index must include a <<nested,nested>> field
  15. mapping. For example:
  16. [source,console]
  17. ----
  18. PUT /my-index-000001
  19. {
  20. "mappings": {
  21. "properties": {
  22. "obj1": {
  23. "type": "nested"
  24. }
  25. }
  26. }
  27. }
  28. ----
  29. [[nested-query-ex-query]]
  30. ===== Example query
  31. [source,console]
  32. ----
  33. GET /my-index-000001/_search
  34. {
  35. "query": {
  36. "nested": {
  37. "path": "obj1",
  38. "query": {
  39. "bool": {
  40. "must": [
  41. { "match": { "obj1.name": "blue" } },
  42. { "range": { "obj1.count": { "gt": 5 } } }
  43. ]
  44. }
  45. },
  46. "score_mode": "avg"
  47. }
  48. }
  49. }
  50. ----
  51. // TEST[continued]
  52. [[nested-top-level-params]]
  53. ==== Top-level parameters for `nested`
  54. `path`::
  55. (Required, string) Path to the nested object you wish to search.
  56. `query`::
  57. +
  58. --
  59. (Required, query object) Query you wish to run on nested objects in the `path`.
  60. If an object matches the search, the `nested` query returns the root parent
  61. document.
  62. You can search nested fields using dot notation that includes the complete path,
  63. such as `obj1.name`.
  64. Multi-level nesting is automatically supported, and detected, resulting in an
  65. inner nested query to automatically match the relevant nesting level, rather
  66. than root, if it exists within another nested query.
  67. See <<multi-level-nested-query-ex>> for an example.
  68. --
  69. `score_mode`::
  70. +
  71. --
  72. (Optional, string) Indicates how scores for matching child objects affect the
  73. root parent document's <<relevance-scores,relevance score>>. Valid values
  74. are:
  75. `avg` (Default)::
  76. Use the mean relevance score of all matching child objects.
  77. `max`::
  78. Uses the highest relevance score of all matching child objects.
  79. `min`::
  80. Uses the lowest relevance score of all matching child objects.
  81. `none`::
  82. Do not use the relevance scores of matching child objects. The query assigns
  83. parent documents a score of `0`.
  84. `sum`::
  85. Add together the relevance scores of all matching child objects.
  86. --
  87. `ignore_unmapped`::
  88. +
  89. --
  90. (Optional, boolean) Indicates whether to ignore an unmapped `path` and not
  91. return any documents instead of an error. Defaults to `false`.
  92. If `false`, {es} returns an error if the `path` is an unmapped field.
  93. You can use this parameter to query multiple indices that may not contain the
  94. field `path`.
  95. --
  96. [[nested-query-notes]]
  97. ==== Notes
  98. [[multi-level-nested-query-ex]]
  99. ===== Multi-level nested queries
  100. To see how multi-level nested queries work,
  101. first you need an index that has nested fields.
  102. The following request defines mappings for the `drivers` index
  103. with nested `make` and `model` fields.
  104. [source,console]
  105. ----
  106. PUT /drivers
  107. {
  108. "mappings": {
  109. "properties": {
  110. "driver": {
  111. "type": "nested",
  112. "properties": {
  113. "last_name": {
  114. "type": "text"
  115. },
  116. "vehicle": {
  117. "type": "nested",
  118. "properties": {
  119. "make": {
  120. "type": "text"
  121. },
  122. "model": {
  123. "type": "text"
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. ----
  133. Next, index some documents to the `drivers` index.
  134. [source,console]
  135. ----
  136. PUT /drivers/_doc/1
  137. {
  138. "driver" : {
  139. "last_name" : "McQueen",
  140. "vehicle" : [
  141. {
  142. "make" : "Powell Motors",
  143. "model" : "Canyonero"
  144. },
  145. {
  146. "make" : "Miller-Meteor",
  147. "model" : "Ecto-1"
  148. }
  149. ]
  150. }
  151. }
  152. PUT /drivers/_doc/2?refresh
  153. {
  154. "driver" : {
  155. "last_name" : "Hudson",
  156. "vehicle" : [
  157. {
  158. "make" : "Mifune",
  159. "model" : "Mach Five"
  160. },
  161. {
  162. "make" : "Miller-Meteor",
  163. "model" : "Ecto-1"
  164. }
  165. ]
  166. }
  167. }
  168. ----
  169. // TEST[continued]
  170. You can now use a multi-level nested query
  171. to match documents based on the `make` and `model` fields.
  172. [source,console]
  173. ----
  174. GET /drivers/_search
  175. {
  176. "query": {
  177. "nested": {
  178. "path": "driver",
  179. "query": {
  180. "nested": {
  181. "path": "driver.vehicle",
  182. "query": {
  183. "bool": {
  184. "must": [
  185. { "match": { "driver.vehicle.make": "Powell Motors" } },
  186. { "match": { "driver.vehicle.model": "Canyonero" } }
  187. ]
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. ----
  196. // TEST[continued]
  197. The search request returns the following response:
  198. [source,console-result]
  199. ----
  200. {
  201. "took" : 5,
  202. "timed_out" : false,
  203. "_shards" : {
  204. "total" : 1,
  205. "successful" : 1,
  206. "skipped" : 0,
  207. "failed" : 0
  208. },
  209. "hits" : {
  210. "total" : {
  211. "value" : 1,
  212. "relation" : "eq"
  213. },
  214. "max_score" : 3.7349272,
  215. "hits" : [
  216. {
  217. "_index" : "drivers",
  218. "_id" : "1",
  219. "_score" : 3.7349272,
  220. "_source" : {
  221. "driver" : {
  222. "last_name" : "McQueen",
  223. "vehicle" : [
  224. {
  225. "make" : "Powell Motors",
  226. "model" : "Canyonero"
  227. },
  228. {
  229. "make" : "Miller-Meteor",
  230. "model" : "Ecto-1"
  231. }
  232. ]
  233. }
  234. }
  235. }
  236. ]
  237. }
  238. }
  239. ----
  240. // TESTRESPONSE[s/"took" : 5/"took": $body.took/]