has-child-query.asciidoc 3.0 KB

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