has-child-query.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 `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. added[1.3.0]
  52. The `has_child` query allows you to specify that a minimum and/or maximum
  53. number of children are required to match for the parent doc to be considered
  54. a match:
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "has_child" : {
  59. "type" : "blog_tag",
  60. "score_mode" : "sum",
  61. "min_children": 2, <1>
  62. "max_children": 10, <1>
  63. "query" : {
  64. "term" : {
  65. "tag" : "something"
  66. }
  67. }
  68. }
  69. }
  70. --------------------------------------------------
  71. <1> Both `min_children` and `max_children` are optional.
  72. The `min_children` and `max_children` parameters can be combined with
  73. the `score_mode` parameter.
  74. [float]
  75. ==== Memory Considerations
  76. In order to support parent-child joins, all of the (string) parent IDs
  77. must be resident in memory (in the <<index-modules-fielddata,field data cache>>.
  78. Additionaly, every child document is mapped to its parent using a long
  79. value (approximately). It is advisable to keep the string parent ID short
  80. in order to reduce memory usage.
  81. You can check how much memory is being used by the ID cache using the
  82. <<indices-stats,indices stats>> or <<cluster-nodes-stats,nodes stats>>
  83. APIS, eg:
  84. [source,js]
  85. --------------------------------------------------
  86. curl -XGET "http://localhost:9200/_stats/id_cache?pretty&human"
  87. --------------------------------------------------