global-aggregation.asciidoc 2.0 KB

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