nested-query.asciidoc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. [[query-dsl-nested-query]]
  2. === Nested Query
  3. Nested query allows to query nested objects / docs (see
  4. <<mapping-nested-type,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` (or
  44. `filter`) includes the query that will run on the nested docs matching
  45. the 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`, `max` and
  50. `none`.
  51. Multi level nesting is automatically supported, and detected, resulting
  52. in an inner nested query to automatically match the relevant nesting
  53. level (and not root) if it exists within another nested query.