nested-query.asciidoc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  19. {
  20. "mappings": {
  21. "properties" : {
  22. "obj1" : {
  23. "type" : "nested"
  24. }
  25. }
  26. }
  27. }
  28. ----
  29. // TESTSETUP
  30. [[nested-query-ex-query]]
  31. ===== Example query
  32. [source,console]
  33. ----
  34. GET /my_index/_search
  35. {
  36. "query": {
  37. "nested" : {
  38. "path" : "obj1",
  39. "query" : {
  40. "bool" : {
  41. "must" : [
  42. { "match" : {"obj1.name" : "blue"} },
  43. { "range" : {"obj1.count" : {"gt" : 5}} }
  44. ]
  45. }
  46. },
  47. "score_mode" : "avg"
  48. }
  49. }
  50. }
  51. ----
  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. --
  68. `score_mode`::
  69. +
  70. --
  71. (Optional, string) Indicates how scores for matching child objects affect the
  72. root parent document's <<relevance-scores,relevance score>>. Valid values
  73. are:
  74. `avg` (Default)::
  75. Use the mean relevance score of all matching child objects.
  76. `max`::
  77. Uses the highest relevance score of all matching child objects.
  78. `min`::
  79. Uses the lowest relevance score of all matching child objects.
  80. `none`::
  81. Do not use the relevance scores of matching child objects. The query assigns
  82. parent documents a score of `0`.
  83. `sum`::
  84. Add together the relevance scores of all matching child objects.
  85. --
  86. `ignore_unmapped`::
  87. +
  88. --
  89. (Optional, boolean) Indicates whether to ignore an unmapped `path` and not
  90. return any documents instead of an error. Defaults to `false`.
  91. If `false`, {es} returns an error if the `path` is an unmapped field.
  92. You can use this parameter to query multiple indices that may not contain the
  93. field `path`.
  94. --