top-children-query.asciidoc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. [[query-dsl-top-children-query]]
  2. === Top Children Query
  3. The `top_children` query runs the child query with an estimated hits
  4. size, and out of the hit docs, aggregates it into parent docs. If there
  5. aren't enough parent docs matching the requested from/size search
  6. request, then it is run again with a wider (more hits) search.
  7. The `top_children` also provide scoring capabilities, with the ability
  8. to specify `max`, `sum` or `avg` as the score type.
  9. One downside of using the `top_children` is that if there are more child
  10. docs matching the required hits when executing the child query, then the
  11. `total_hits` result of the search response will be incorrect.
  12. How many hits are asked for in the first child query run is controlled
  13. using the `factor` parameter (defaults to `5`). For example, when asking
  14. for 10 parent docs (with `from` set to 0), then the child query will
  15. execute with 50 hits expected. If not enough parents are found (in our
  16. example 10), and there are still more child docs to query, then the
  17. child search hits are expanded by multiplying by the
  18. `incremental_factor` (defaults to `2`).
  19. The required parameters are the `query` and `type` (the child type to
  20. execute the query on). Here is an example with all different parameters,
  21. including the default values:
  22. [source,js]
  23. --------------------------------------------------
  24. {
  25. "top_children" : {
  26. "type": "blog_tag",
  27. "query" : {
  28. "term" : {
  29. "tag" : "something"
  30. }
  31. },
  32. "score" : "max",
  33. "factor" : 5,
  34. "incremental_factor" : 2
  35. }
  36. }
  37. --------------------------------------------------
  38. [float]
  39. ==== Scope
  40. A `_scope` can be defined on the query allowing to run facets on the
  41. same scope name that will work against the child documents. For example:
  42. [source,js]
  43. --------------------------------------------------
  44. {
  45. "top_children" : {
  46. "_scope" : "my_scope",
  47. "type": "blog_tag",
  48. "query" : {
  49. "term" : {
  50. "tag" : "something"
  51. }
  52. }
  53. }
  54. }
  55. --------------------------------------------------
  56. [float]
  57. ==== Memory Considerations
  58. With the current implementation, all `_parent` field values and all `_id`
  59. field values of parent documents are loaded into memory (heap) via field data
  60. in order to support fast lookups, so make sure there is enough memory for it.