nested-query.asciidoc 2.7 KB

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