nested-query.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. [[query-dsl-nested-query]]
  2. === Nested Query
  3. Nested query allows to query nested objects / docs (see
  4. <<nested,nested mapping>>). The
  5. query is executed against the nested objects / docs as if they were
  6. indexed as separate docs (they are, internally) and resulting in the
  7. root parent doc (or parent nested mapping). Here is a sample mapping we
  8. will work with:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "type1" : {
  13. "properties" : {
  14. "obj1" : {
  15. "type" : "nested"
  16. }
  17. }
  18. }
  19. }
  20. --------------------------------------------------
  21. And here is a sample nested query usage:
  22. [source,js]
  23. --------------------------------------------------
  24. {
  25. "nested" : {
  26. "path" : "obj1",
  27. "score_mode" : "avg",
  28. "query" : {
  29. "bool" : {
  30. "must" : [
  31. {
  32. "match" : {"obj1.name" : "blue"}
  33. },
  34. {
  35. "range" : {"obj1.count" : {"gt" : 5}}
  36. }
  37. ]
  38. }
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. The query `path` points to the nested object path, and the `query`
  44. includes the query that will run on the nested docs matching the
  45. direct path, and joining with the root parent docs. Note that any
  46. fields referenced inside the query must use the complete path (fully
  47. qualified).
  48. The `score_mode` allows to set how inner children matching affects
  49. scoring of parent. It defaults to `avg`, but can be `sum`, `min`,
  50. `max` and `none`.
  51. There is also an `ignore_unmapped` option which, when set to `true` will
  52. ignore an unmapped `path` and will not match any documents for this query.
  53. This can be useful when querying multiple indexes which might have different
  54. mappings. When set to `false` (the default value) the query will throw an
  55. exception if the `path` is not mapped.
  56. Multi level nesting is automatically supported, and detected, resulting
  57. in an inner nested query to automatically match the relevant nesting
  58. level (and not root) if it exists within another nested query.