nested-query.asciidoc 2.2 KB

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