nested-query.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. PUT /my_index
  12. {
  13. "mappings": {
  14. "type1" : {
  15. "properties" : {
  16. "obj1" : {
  17. "type" : "nested"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. --------------------------------------------------
  24. // CONSOLE
  25. // TESTSETUP
  26. And here is a sample nested query usage:
  27. [source,js]
  28. --------------------------------------------------
  29. GET /_search
  30. {
  31. "query": {
  32. "nested" : {
  33. "path" : "obj1",
  34. "score_mode" : "avg",
  35. "query" : {
  36. "bool" : {
  37. "must" : [
  38. { "match" : {"obj1.name" : "blue"} },
  39. { "range" : {"obj1.count" : {"gt" : 5}} }
  40. ]
  41. }
  42. }
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. The query `path` points to the nested object path, and the `query`
  49. includes the query that will run on the nested docs matching the
  50. direct path, and joining with the root parent docs. Note that any
  51. fields referenced inside the query must use the complete path (fully
  52. qualified).
  53. The `score_mode` allows to set how inner children matching affects
  54. scoring of parent. It defaults to `avg`, but can be `sum`, `min`,
  55. `max` and `none`.
  56. There is also an `ignore_unmapped` option which, when set to `true` will
  57. ignore an unmapped `path` and will not match any documents for this query.
  58. This can be useful when querying multiple indexes which might have different
  59. mappings. When set to `false` (the default value) the query will throw an
  60. exception if the `path` is not mapped.
  61. Multi level nesting is automatically supported, and detected, resulting
  62. in an inner nested query to automatically match the relevant nesting
  63. level (and not root) if it exists within another nested query.