has-child-query.asciidoc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. [float]
  20. === Scoring capabilities
  21. The `has_child` also has scoring support. The
  22. supported score types are `min`, `max`, `sum`, `avg` or `none`. The default is
  23. `none` and yields the same behaviour as in previous versions. If the
  24. score type is set to another value than `none`, the scores of all the
  25. matching child documents are aggregated into the associated parent
  26. documents. The score type can be specified with the `score_mode` field
  27. inside the `has_child` query:
  28. [source,js]
  29. --------------------------------------------------
  30. {
  31. "has_child" : {
  32. "type" : "blog_tag",
  33. "score_mode" : "sum",
  34. "query" : {
  35. "term" : {
  36. "tag" : "something"
  37. }
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. [float]
  43. === Min/Max Children
  44. The `has_child` query allows you to specify that a minimum and/or maximum
  45. number of children are required to match for the parent doc to be considered
  46. a match:
  47. [source,js]
  48. --------------------------------------------------
  49. {
  50. "has_child" : {
  51. "type" : "blog_tag",
  52. "score_mode" : "sum",
  53. "min_children": 2, <1>
  54. "max_children": 10, <1>
  55. "query" : {
  56. "term" : {
  57. "tag" : "something"
  58. }
  59. }
  60. }
  61. }
  62. --------------------------------------------------
  63. <1> Both `min_children` and `max_children` are optional.
  64. The `min_children` and `max_children` parameters can be combined with
  65. the `score_mode` parameter.
  66. [float]
  67. === Memory Considerations
  68. In order to support parent-child joins, all of the (string) parent IDs
  69. must be resident in memory (in the <<index-modules-fielddata,field data cache>>.
  70. Additionally, every child document is mapped to its parent using a long
  71. value (approximately). It is advisable to keep the string parent ID short
  72. in order to reduce memory usage.
  73. You can check how much memory is being used by the `_parent` field in the fielddata cache
  74. using the <<indices-stats,indices stats>> or <<cluster-nodes-stats,nodes stats>>
  75. APIS, eg:
  76. [source,js]
  77. --------------------------------------------------
  78. curl -XGET "http://localhost:9200/_stats/fielddata?pretty&human&fielddata_fields=_parent"
  79. --------------------------------------------------