global-aggregation.asciidoc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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,js]
  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. // CONSOLE
  29. // TEST[setup:sales]
  30. <1> The `global` aggregation has an empty body
  31. <2> The sub-aggregations that are registered for this `global` aggregation
  32. The above aggregation demonstrates how one would compute aggregations
  33. (`avg_price` in this example) on all the documents in the search context,
  34. regardless of the query (in our example, it will compute the average price over
  35. all products in our catalog, not just on the "shirts").
  36. The response for the above aggregation:
  37. [source,js]
  38. --------------------------------------------------
  39. {
  40. ...
  41. "aggregations" : {
  42. "all_products" : {
  43. "doc_count" : 7, <1>
  44. "avg_price" : {
  45. "value" : 140.71428571428572 <2>
  46. }
  47. },
  48. "t_shirts": {
  49. "value" : 128.33333333333334 <3>
  50. }
  51. }
  52. }
  53. --------------------------------------------------
  54. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  55. <1> The number of documents that were aggregated (in our case, all documents
  56. within the search context)
  57. <2> The average price of all products in the index
  58. <3> The average price of all t-shirts