has-child-query.asciidoc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. [[query-dsl-has-child-query]]
  2. == Has Child Query
  3. The `has_child` filter accepts a query and the child type to run against, and
  4. results in parent documents that have child docs matching the query. Here is
  5. an example:
  6. [source,js]
  7. --------------------------------------------------
  8. {
  9. "has_child" : {
  10. "type" : "blog_tag",
  11. "query" : {
  12. "term" : {
  13. "tag" : "something"
  14. }
  15. }
  16. }
  17. }
  18. --------------------------------------------------
  19. An important difference with the `top_children` query is that this query
  20. is always executed in two iterations whereas the `top_children` query
  21. can be executed in one or more iteration. When using the `has_child`
  22. query the `total_hits` is always correct.
  23. [float]
  24. === Scoring capabilities
  25. The `has_child` also has scoring support. The
  26. supported score types are `min`, `max`, `sum`, `avg` or `none`. The default is
  27. `none` and yields the same behaviour as in previous versions. If the
  28. score type is set to another value than `none`, the scores of all the
  29. matching child documents are aggregated into the associated parent
  30. documents. The score type can be specified with the `score_mode` field
  31. inside the `has_child` query:
  32. [source,js]
  33. --------------------------------------------------
  34. {
  35. "has_child" : {
  36. "type" : "blog_tag",
  37. "score_mode" : "sum",
  38. "query" : {
  39. "term" : {
  40. "tag" : "something"
  41. }
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. [float]
  47. === Min/Max Children
  48. The `has_child` query allows you to specify that a minimum and/or maximum
  49. number of children are required to match for the parent doc to be considered
  50. a match:
  51. [source,js]
  52. --------------------------------------------------
  53. {
  54. "has_child" : {
  55. "type" : "blog_tag",
  56. "score_mode" : "sum",
  57. "min_children": 2, <1>
  58. "max_children": 10, <1>
  59. "query" : {
  60. "term" : {
  61. "tag" : "something"
  62. }
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. <1> Both `min_children` and `max_children` are optional.
  68. The `min_children` and `max_children` parameters can be combined with
  69. the `score_mode` parameter.
  70. [float]
  71. === Memory Considerations
  72. In order to support parent-child joins, all of the (string) parent IDs
  73. must be resident in memory (in the <<index-modules-fielddata,field data cache>>.
  74. Additionally, every child document is mapped to its parent using a long
  75. value (approximately). It is advisable to keep the string parent ID short
  76. in order to reduce memory usage.
  77. You can check how much memory is being used by the ID cache using the
  78. <<indices-stats,indices stats>> or <<cluster-nodes-stats,nodes stats>>
  79. APIS, eg:
  80. [source,js]
  81. --------------------------------------------------
  82. curl -XGET "http://localhost:9200/_stats/id_cache?pretty&human"
  83. --------------------------------------------------