has-child-query.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [[query-dsl-has-child-query]]
  2. === Has child query
  3. ++++
  4. <titleabbrev>Has child</titleabbrev>
  5. ++++
  6. Returns parent documents whose <<parent-join,joined>> child documents match a
  7. provided query. You can create parent-child relationships between documents in
  8. the same index using a <<parent-join,join>> field mapping.
  9. [WARNING]
  10. ====
  11. Because it performs a join, the `has_child` is slow compared to other queries.
  12. Its performance degrades as the number of matching child documents pointing to
  13. unique parent documents increases. Each `has_child` query in a search can
  14. increase query time significantly.
  15. If you care about query performance, do not use this query. If you need to use
  16. the `has_child` query, use it as rarely as possible.
  17. ====
  18. [[has-child-query-ex-request]]
  19. ==== Example request
  20. [[has-child-index-setup]]
  21. ===== Index setup
  22. To use the `has_child` query, your index must include a <<parent-join,join>>
  23. field mapping. For example:
  24. [source,console]
  25. ----
  26. PUT /my_index
  27. {
  28. "mappings": {
  29. "properties" : {
  30. "my-join-field" : {
  31. "type" : "join",
  32. "relations": {
  33. "parent": "child"
  34. }
  35. }
  36. }
  37. }
  38. }
  39. ----
  40. // TESTSETUP
  41. [[has-child-query-ex-query]]
  42. ===== Example query
  43. [source,console]
  44. ----
  45. GET /_search
  46. {
  47. "query": {
  48. "has_child" : {
  49. "type" : "child",
  50. "query" : {
  51. "match_all" : {}
  52. },
  53. "max_children": 10,
  54. "min_children": 2,
  55. "score_mode" : "min"
  56. }
  57. }
  58. }
  59. ----
  60. [[has-child-top-level-params]]
  61. ==== Top-level parameters for `has_child`
  62. `type`::
  63. (Required, string) Name of the child relationship mapped for the
  64. <<parent-join,join>> field.
  65. `query`::
  66. (Required, query object) Query you wish to run on child documents of the `type`
  67. field. If a child document matches the search, the query returns the parent
  68. document.
  69. `ignore_unmapped`::
  70. +
  71. --
  72. (Optional, boolean) Indicates whether to ignore an unmapped `type` and not
  73. return any documents instead of an error. Defaults to `false`.
  74. If `false`, {es} returns an error if the `type` is unmapped.
  75. You can use this parameter to query multiple indices that may not contain the
  76. `type`.
  77. --
  78. `max_children`::
  79. (Optional, integer) Maximum number of child documents that match the `query`
  80. allowed for a returned parent document. If the parent document exceeds this
  81. limit, it is excluded from the search results.
  82. `min_children`::
  83. (Optional, integer) Minimum number of child documents that match the `query`
  84. required to match the query for a returned parent document. If the parent
  85. document does not meet this limit, it is excluded from the search results.
  86. `score_mode`::
  87. +
  88. --
  89. (Optional, string) Indicates how scores for matching child documents affect the
  90. root parent document's <<relevance-scores,relevance score>>. Valid values
  91. are:
  92. `none` (Default)::
  93. Do not use the relevance scores of matching child documents. The query assigns
  94. parent documents a score of `0`.
  95. `avg`::
  96. Use the mean relevance score of all matching child documents.
  97. `max`::
  98. Uses the highest relevance score of all matching child documents.
  99. `min`::
  100. Uses the lowest relevance score of all matching child documents.
  101. `sum`::
  102. Add together the relevance scores of all matching child documents.
  103. --
  104. [[has-child-query-notes]]
  105. ==== Notes
  106. [[has-child-query-performance]]
  107. ===== Sorting
  108. You cannot sort the results of a `has_child` query using standard
  109. <<request-body-search-sort,sort options>>.
  110. If you need to sort returned documents by a field in their child documents, use
  111. a `function_score` query and sort by `_score`. For example, the following query
  112. sorts returned documents by the `click_count` field of their child documents.
  113. [source,console]
  114. ----
  115. GET /_search
  116. {
  117. "query": {
  118. "has_child" : {
  119. "type" : "child",
  120. "query" : {
  121. "function_score" : {
  122. "script_score": {
  123. "script": "_score * doc['click_count'].value"
  124. }
  125. }
  126. },
  127. "score_mode" : "max"
  128. }
  129. }
  130. }
  131. ----