top-children-query.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. In order to support parent-child joins, all of the (string) parent IDs
  59. must be resident in memory (in the <<index-modules-fielddata,field data cache>>.
  60. Additionaly, every child document is mapped to its parent using a long
  61. value (approximately). It is advisable to keep the string parent ID short
  62. in order to reduce memory usage.
  63. You can check how much memory is being used by the ID cache using the
  64. <<indices-stats,indices stats>> or <<cluster-nodes-stats,nodes stats>>
  65. APIS, eg:
  66. [source,js]
  67. --------------------------------------------------
  68. curl -XGET "http://localhost:9200/_stats/id_cache?pretty&human"
  69. --------------------------------------------------