has-child-query.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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,js]
  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. // CONSOLE
  41. // TESTSETUP
  42. [[has-child-query-ex-query]]
  43. ===== Example query
  44. [source,js]
  45. ----
  46. GET /_search
  47. {
  48. "query": {
  49. "has_child" : {
  50. "type" : "child",
  51. "query" : {
  52. "match_all" : {}
  53. },
  54. "max_children": 10,
  55. "min_children": 2,
  56. "score_mode" : "min"
  57. }
  58. }
  59. }
  60. ----
  61. // CONSOLE
  62. [[has-child-top-level-params]]
  63. ==== Top-level parameters for `has_child`
  64. `type`::
  65. (Required, string) Name of the child relationship mapped for the
  66. <<parent-join,join>> field.
  67. `query`::
  68. (Required, query object) Query you wish to run on child documents of the `type`
  69. field. If a child document matches the search, the query returns the parent
  70. document.
  71. `ignore_unmapped`::
  72. +
  73. --
  74. (Optional, boolean) Indicates whether to ignore an unmapped `type` and not
  75. return any documents instead of an error. Defaults to `false`.
  76. If `false`, {es} returns an error if the `type` is unmapped.
  77. You can use this parameter to query multiple indices that may not contain the
  78. `type`.
  79. --
  80. `max_children`::
  81. (Optional, integer) Maximum number of child documents that match the `query`
  82. allowed for a returned parent document. If the parent document exceeds this
  83. limit, it is excluded from the search results.
  84. `min_children`::
  85. (Optional, integer) Minimum number of child documents that match the `query`
  86. required to match the query for a returned parent document. If the parent
  87. document does not meet this limit, it is excluded from the search results.
  88. `score_mode`::
  89. +
  90. --
  91. (Optional, string) Indicates how scores for matching child documents affect the
  92. root parent document's <<relevance-scores,relevance score>>. Valid values
  93. are:
  94. `none` (Default)::
  95. Do not use the relevance scores of matching child documents. The query assigns
  96. parent documents a score of `0`.
  97. `avg`::
  98. Use the mean relevance score of all matching child documents.
  99. `max`::
  100. Uses the highest relevance score of all matching child documents.
  101. `min`::
  102. Uses the lowest relevance score of all matching child documents.
  103. `sum`::
  104. Add together the relevance scores of all matching child documents.
  105. --
  106. [[has-child-query-notes]]
  107. ==== Notes
  108. [[has-child-query-performance]]
  109. ===== Sorting
  110. You cannot sort the results of a `has_child` query using standard
  111. <<request-body-search-sort,sort options>>.
  112. If you need to sort returned documents by a field in their child documents, use
  113. a `function_score` query and sort by `_score`. For example, the following query
  114. sorts returned documents by the `click_count` field of their child documents.
  115. [source,js]
  116. ----
  117. GET /_search
  118. {
  119. "query": {
  120. "has_child" : {
  121. "type" : "child",
  122. "query" : {
  123. "function_score" : {
  124. "script_score": {
  125. "script": "_score * doc['click_count'].value"
  126. }
  127. }
  128. },
  129. "score_mode" : "max"
  130. }
  131. }
  132. }
  133. ----
  134. // CONSOLE