nested-query.asciidoc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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,js]
  17. ----
  18. PUT /my_index
  19. {
  20. "mappings": {
  21. "properties" : {
  22. "obj1" : {
  23. "type" : "nested"
  24. }
  25. }
  26. }
  27. }
  28. ----
  29. // CONSOLE
  30. // TESTSETUP
  31. [[nested-query-ex-query]]
  32. ===== Example query
  33. [source,js]
  34. ----
  35. GET /my_index/_search
  36. {
  37. "query": {
  38. "nested" : {
  39. "path" : "obj1",
  40. "query" : {
  41. "bool" : {
  42. "must" : [
  43. { "match" : {"obj1.name" : "blue"} },
  44. { "range" : {"obj1.count" : {"gt" : 5}} }
  45. ]
  46. }
  47. },
  48. "score_mode" : "avg"
  49. }
  50. }
  51. }
  52. ----
  53. // CONSOLE
  54. [[nested-top-level-params]]
  55. ==== Top-level parameters for `nested`
  56. `path`::
  57. (Required, string) Path to the nested object you wish to search.
  58. `query`::
  59. +
  60. --
  61. (Required, query object) Query you wish to run on nested objects in the `path`.
  62. If an object matches the search, the `nested` query returns the root parent
  63. document.
  64. You can search nested fields using dot notation that includes the complete path,
  65. such as `obj1.name`.
  66. Multi-level nesting is automatically supported, and detected, resulting in an
  67. inner nested query to automatically match the relevant nesting level, rather
  68. than root, if it exists within another nested query.
  69. --
  70. `score_mode`::
  71. +
  72. --
  73. (Optional, string) Indicates how scores for matching child objects affect the
  74. root parent document's <<query-filter-context,relevance score>>. Valid values
  75. are:
  76. `avg` (Default)::
  77. Use the mean relevance score of all matching child objects.
  78. `max`::
  79. Uses the highest relevance score of all matching child objects.
  80. `min`::
  81. Uses the lowest relevance score of all matching child objects.
  82. `none`::
  83. Do not use the relevance scores of matching child objects. The query assigns
  84. parent documents a score of `0`.
  85. `sum`::
  86. Add together the relevance scores of all matching child objects.
  87. --
  88. `ignore_unmapped`::
  89. +
  90. --
  91. (Optional, boolean) Indicates whether to ignore an unmapped `path` and not
  92. return any documents instead of an error. Defaults to `false`.
  93. If `false`, {es} returns an error if the `path` is an unmapped field.
  94. You can use this parameter to query multiple indices that may not contain the
  95. field `path`.
  96. --