has-parent-query.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [[query-dsl-has-parent-query]]
  2. === Has parent query
  3. ++++
  4. <titleabbrev>Has parent</titleabbrev>
  5. ++++
  6. The `has_parent` query accepts a query and a parent type. The query is
  7. executed in the parent document space, which is specified by the parent
  8. type. This query returns child documents which associated parents have
  9. matched. For the rest `has_parent` query has the same options and works
  10. in the same manner as the `has_child` query.
  11. [source,js]
  12. --------------------------------------------------
  13. GET /_search
  14. {
  15. "query": {
  16. "has_parent" : {
  17. "parent_type" : "blog",
  18. "query" : {
  19. "term" : {
  20. "tag" : "something"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. --------------------------------------------------
  27. // CONSOLE
  28. Note that the `has_parent` is a slow query compared to other queries in the
  29. query dsl due to the fact that it performs a join. The performance degrades
  30. as the number of matching parent documents increases. If you care about query
  31. performance you should not use this query. However if you do happen to use
  32. this query then use it as less as possible. Each `has_parent` query that gets
  33. added to a search request can increase query time significantly.
  34. [float]
  35. ==== Scoring capabilities
  36. The `has_parent` also has scoring support. The default is `false` which
  37. ignores the score from the parent document. The score is in this
  38. case equal to the boost on the `has_parent` query (Defaults to 1). If
  39. the score is set to `true`, then the score of the matching parent
  40. document is aggregated into the child documents belonging to the
  41. matching parent document. The score mode can be specified with the
  42. `score` field inside the `has_parent` query:
  43. [source,js]
  44. --------------------------------------------------
  45. GET /_search
  46. {
  47. "query": {
  48. "has_parent" : {
  49. "parent_type" : "blog",
  50. "score" : true,
  51. "query" : {
  52. "term" : {
  53. "tag" : "something"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // CONSOLE
  61. [float]
  62. ==== Ignore Unmapped
  63. When set to `true` the `ignore_unmapped` option will ignore an unmapped `type`
  64. and will not match any documents for this query. This can be useful when
  65. querying multiple indexes which might have different mappings. When set to
  66. `false` (the default value) the query will throw an exception if the `type`
  67. is not mapped.
  68. [float]
  69. ==== Sorting
  70. Child documents can't be sorted by fields in matching parent documents via the
  71. regular sort options. If you need to sort child documents by field in the parent
  72. documents then you should use the `function_score` query and then just sort
  73. by `_score`.
  74. Sorting tags by parent document' `view_count` field:
  75. [source,js]
  76. --------------------------------------------------
  77. GET /_search
  78. {
  79. "query": {
  80. "has_parent" : {
  81. "parent_type" : "blog",
  82. "score" : true,
  83. "query" : {
  84. "function_score" : {
  85. "script_score": {
  86. "script": "_score * doc['view_count'].value"
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. // CONSOLE