has-child-query.asciidoc 4.0 KB

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