global-aggregation.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. [[search-aggregations-bucket-global-aggregation]]
  2. === Global Aggregation
  3. Defines a single bucket of all the documents within the search execution
  4. context. This context is defined by the indices and the document types you're
  5. searching on, but is *not* influenced by the search query itself.
  6. NOTE: Global aggregators can only be placed as top level aggregators because
  7. it doesn't make sense to embed a global aggregator within another
  8. bucket aggregator.
  9. Example:
  10. [source,console]
  11. --------------------------------------------------
  12. POST /sales/_search?size=0
  13. {
  14. "query" : {
  15. "match" : { "type" : "t-shirt" }
  16. },
  17. "aggs" : {
  18. "all_products" : {
  19. "global" : {}, <1>
  20. "aggs" : { <2>
  21. "avg_price" : { "avg" : { "field" : "price" } }
  22. }
  23. },
  24. "t_shirts": { "avg" : { "field" : "price" } }
  25. }
  26. }
  27. --------------------------------------------------
  28. // TEST[setup:sales]
  29. <1> The `global` aggregation has an empty body
  30. <2> The sub-aggregations that are registered for this `global` aggregation
  31. The above aggregation demonstrates how one would compute aggregations
  32. (`avg_price` in this example) on all the documents in the search context,
  33. regardless of the query (in our example, it will compute the average price over
  34. all products in our catalog, not just on the "shirts").
  35. The response for the above aggregation:
  36. [source,js]
  37. --------------------------------------------------
  38. {
  39. ...
  40. "aggregations" : {
  41. "all_products" : {
  42. "doc_count" : 7, <1>
  43. "avg_price" : {
  44. "value" : 140.71428571428572 <2>
  45. }
  46. },
  47. "t_shirts": {
  48. "value" : 128.33333333333334 <3>
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  54. <1> The number of documents that were aggregated (in our case, all documents
  55. within the search context)
  56. <2> The average price of all products in the index
  57. <3> The average price of all t-shirts