has-child-query.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. [[query-dsl-has-child-query]]
  2. === Has Child Query
  3. The `has_child` filter accepts a query and the child type to run against, and
  4. results in parent documents that have child docs matching the query. Here is
  5. an example:
  6. [source,js]
  7. --------------------------------------------------
  8. GET /_search
  9. {
  10. "query": {
  11. "has_child" : {
  12. "type" : "blog_tag",
  13. "query" : {
  14. "term" : {
  15. "tag" : "something"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. // CONSOLE
  23. [float]
  24. ==== Scoring capabilities
  25. The `has_child` also has scoring support. The
  26. supported score modes are `min`, `max`, `sum`, `avg` or `none`. The default is
  27. `none` and yields the same behaviour as in previous versions. If the
  28. score mode is set to another value than `none`, the scores of all the
  29. matching child documents are aggregated into the associated parent
  30. documents. The score type can be specified with the `score_mode` field
  31. inside the `has_child` query:
  32. [source,js]
  33. --------------------------------------------------
  34. GET /_search
  35. {
  36. "query": {
  37. "has_child" : {
  38. "type" : "blog_tag",
  39. "score_mode" : "min",
  40. "query" : {
  41. "term" : {
  42. "tag" : "something"
  43. }
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. // CONSOLE
  50. [float]
  51. ==== Min/Max Children
  52. The `has_child` query allows you to specify that a minimum and/or maximum
  53. number of children are required to match for the parent doc to be considered
  54. a match:
  55. [source,js]
  56. --------------------------------------------------
  57. GET /_search
  58. {
  59. "query": {
  60. "has_child" : {
  61. "type" : "blog_tag",
  62. "score_mode" : "min",
  63. "min_children": 2, <1>
  64. "max_children": 10, <1>
  65. "query" : {
  66. "term" : {
  67. "tag" : "something"
  68. }
  69. }
  70. }
  71. }
  72. }
  73. --------------------------------------------------
  74. // CONSOLE
  75. <1> Both `min_children` and `max_children` are optional.
  76. The `min_children` and `max_children` parameters can be combined with
  77. the `score_mode` parameter.
  78. [float]
  79. ==== Ignore Unmapped
  80. When set to `true` the `ignore_unmapped` option will ignore an unmapped `type`
  81. and will not match any documents for this query. This can be useful when
  82. querying multiple indexes which might have different mappings. When set to
  83. `false` (the default value) the query will throw an exception if the `type`
  84. is not mapped.
  85. [float]
  86. ==== Sorting
  87. Parent documents can't be sorted by fields in matching child documents via the
  88. regular sort options. If you need to sort parent document by field in the child
  89. documents then you can should use the `function_score` query and then just sort
  90. by `_score`.
  91. Sorting blogs by child documents' `click_count` field:
  92. [source,js]
  93. --------------------------------------------------
  94. GET /_search
  95. {
  96. "query": {
  97. "has_child" : {
  98. "type" : "blog_tag",
  99. "score_mode" : "max",
  100. "query" : {
  101. "function_score" : {
  102. "script_score": {
  103. "script": "_score * doc['click_count'].value"
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. // CONSOLE