has-parent-query.asciidoc 3.1 KB

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