has-child-query.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. GET /_search
  9. {
  10. "query": {
  11. "has_child" : {
  12. "type" : "blog_tag",
  13. "query" : {
  14. "term" : {
  15. "tag" : "something"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. // CONSOLE
  23. Note that the `has_child` is a slow query compared to other queries in the
  24. query dsl due to the fact that it performs a join. The performance degrades
  25. as the number of matching child documents pointing to unique parent documents
  26. increases. If you care about query performance you should not use this query.
  27. However if you do happen to use this query then use it as little as possible.
  28. Each `has_child` query that gets added to a search request can increase query
  29. time significantly.
  30. [float]
  31. ==== Scoring capabilities
  32. The `has_child` also has scoring support. The
  33. supported score modes are `min`, `max`, `sum`, `avg` or `none`. The default is
  34. `none` and yields the same behaviour as in previous versions. If the
  35. score mode is set to another value than `none`, the scores of all the
  36. matching child documents are aggregated into the associated parent
  37. documents. The score type can be specified with the `score_mode` field
  38. inside the `has_child` query:
  39. [source,js]
  40. --------------------------------------------------
  41. GET /_search
  42. {
  43. "query": {
  44. "has_child" : {
  45. "type" : "blog_tag",
  46. "score_mode" : "min",
  47. "query" : {
  48. "term" : {
  49. "tag" : "something"
  50. }
  51. }
  52. }
  53. }
  54. }
  55. --------------------------------------------------
  56. // CONSOLE
  57. [float]
  58. ==== Min/Max Children
  59. The `has_child` query allows you to specify that a minimum and/or maximum
  60. number of children are required to match for the parent doc to be considered
  61. a match:
  62. [source,js]
  63. --------------------------------------------------
  64. GET /_search
  65. {
  66. "query": {
  67. "has_child" : {
  68. "type" : "blog_tag",
  69. "score_mode" : "min",
  70. "min_children": 2, <1>
  71. "max_children": 10, <1>
  72. "query" : {
  73. "term" : {
  74. "tag" : "something"
  75. }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. <1> Both `min_children` and `max_children` are optional.
  83. The `min_children` and `max_children` parameters can be combined with
  84. the `score_mode` parameter.
  85. [float]
  86. ==== Ignore Unmapped
  87. When set to `true` the `ignore_unmapped` option will ignore an unmapped `type`
  88. and will not match any documents for this query. This can be useful when
  89. querying multiple indexes which might have different mappings. When set to
  90. `false` (the default value) the query will throw an exception if the `type`
  91. is not mapped.
  92. [float]
  93. ==== Sorting
  94. Parent documents can't be sorted by fields in matching child documents via the
  95. regular sort options. If you need to sort parent document by field in the child
  96. documents then you should use the `function_score` query and then just sort
  97. by `_score`.
  98. Sorting blogs by child documents' `click_count` field:
  99. [source,js]
  100. --------------------------------------------------
  101. GET /_search
  102. {
  103. "query": {
  104. "has_child" : {
  105. "type" : "blog_tag",
  106. "score_mode" : "max",
  107. "query" : {
  108. "function_score" : {
  109. "script_score": {
  110. "script": "_score * doc['click_count'].value"
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // CONSOLE