has-parent-query.asciidoc 3.2 KB

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