has-parent-query.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. [[query-dsl-has-parent-query]]
  2. === Has parent query
  3. ++++
  4. <titleabbrev>Has parent</titleabbrev>
  5. ++++
  6. Returns child documents whose <<parent-join,joined>> parent document matches 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_parent` query is slow compared to other queries.
  12. Its performance degrades as the number of matching parent documents increases.
  13. Each `has_parent` query in a search can increase query time significantly.
  14. ====
  15. [[has-parent-query-ex-request]]
  16. ==== Example request
  17. [[has-parent-index-setup]]
  18. ===== Index setup
  19. To use the `has_parent` 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. "tag" : {
  34. "type" : "keyword"
  35. }
  36. }
  37. }
  38. }
  39. ----
  40. // CONSOLE
  41. // TESTSETUP
  42. [[has-parent-query-ex-query]]
  43. ===== Example query
  44. [source,js]
  45. ----
  46. GET /my-index/_search
  47. {
  48. "query": {
  49. "has_parent" : {
  50. "parent_type" : "parent",
  51. "query" : {
  52. "term" : {
  53. "tag" : {
  54. "value" : "Elasticsearch"
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. ----
  62. // CONSOLE
  63. [[has-parent-top-level-params]]
  64. ==== Top-level parameters for `has_parent`
  65. `parent_type`::
  66. (Required, string) Name of the parent relationship mapped for the
  67. <<parent-join,join>> field.
  68. `query`::
  69. (Required, query object) Query you wish to run on parent documents of the
  70. `parent_type` field. If a parent document matches the search, the query returns
  71. its child documents.
  72. `score`::
  73. +
  74. --
  75. (Optional, boolean) Indicates whether the <<query-filter-context,relevance
  76. score>> of a matching parent document is aggregated into its child documents.
  77. Defaults to `false`.
  78. If `false`, {es} ignores the relevance score of the parent document. {es} also
  79. assigns each child document a relevance score equal to the `query`'s `boost`,
  80. which defaults to `1`.
  81. If `true`, the relevance score of the matching parent document is aggregated
  82. into its child documents' relevance scores.
  83. --
  84. `ignore_unmapped`::
  85. +
  86. --
  87. (Optional, boolean) Indicates whether to ignore an unmapped `parent_type` and
  88. not return any documents instead of an error. Defaults to `false`.
  89. If `false`, {es} returns an error if the `parent_type` is unmapped.
  90. You can use this parameter to query multiple indices that may not contain the
  91. `parent_type`.
  92. --
  93. [[has-parent-query-notes]]
  94. ==== Notes
  95. [[has-parent-query-performance]]
  96. ===== Sorting
  97. You cannot sort the results of a `has_parent` query using standard
  98. <<search-request-sort,sort options>>.
  99. If you need to sort returned documents by a field in their parent documents, use
  100. a `function_score` query and sort by `_score`. For example, the following query
  101. sorts returned documents by the `view_count` field of their parent documents.
  102. [source,js]
  103. ----
  104. GET /_search
  105. {
  106. "query": {
  107. "has_parent" : {
  108. "parent_type" : "parent",
  109. "score" : true,
  110. "query" : {
  111. "function_score" : {
  112. "script_score": {
  113. "script": "_score * doc['view_count'].value"
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. ----
  121. // CONSOLE