nested-query.asciidoc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.
  46. The `score_mode` allows to set how inner children matching affects
  47. scoring of parent. It defaults to `avg`, but can be `total`, `max` and
  48. `none`.
  49. Multi level nesting is automatically supported, and detected, resulting
  50. in an inner nested query to automatically match the relevant nesting
  51. level (and not root) if it exists within another nested query.