has-parent-query.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. [float]
  26. ==== Scoring capabilities
  27. The `has_parent` also has scoring support. The default is `false` which
  28. ignores the score from the parent document. The score is in this
  29. case equal to the boost on the `has_parent` query (Defaults to 1). If
  30. the score is set to `true`, then the score of the matching parent
  31. document is aggregated into the child documents belonging to the
  32. matching parent document. The score mode can be specified with the
  33. `score` field inside the `has_parent` query:
  34. [source,js]
  35. --------------------------------------------------
  36. GET /_search
  37. {
  38. "query": {
  39. "has_parent" : {
  40. "parent_type" : "blog",
  41. "score" : true,
  42. "query" : {
  43. "term" : {
  44. "tag" : "something"
  45. }
  46. }
  47. }
  48. }
  49. }
  50. --------------------------------------------------
  51. // CONSOLE
  52. [float]
  53. ==== Ignore Unmapped
  54. When set to `true` the `ignore_unmapped` option will ignore an unmapped `type`
  55. and will not match any documents for this query. This can be useful when
  56. querying multiple indexes which might have different mappings. When set to
  57. `false` (the default value) the query will throw an exception if the `type`
  58. is not mapped.
  59. [float]
  60. ==== Sorting
  61. Child documents can't be sorted by fields in matching parent documents via the
  62. regular sort options. If you need to sort child documents by field in the parent
  63. documents then you can should use the `function_score` query and then just sort
  64. by `_score`.
  65. Sorting tags by parent document' `view_count` field:
  66. [source,js]
  67. --------------------------------------------------
  68. GET /_search
  69. {
  70. "query": {
  71. "has_parent" : {
  72. "parent_type" : "blog",
  73. "score" : true,
  74. "query" : {
  75. "function_score" : {
  76. "script_score": {
  77. "script": "_score * doc['view_count'].value"
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. // CONSOLE