aggregations.asciidoc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. [[search-aggregations]]
  2. == Aggregations
  3. The aggregations framework helps provide aggregated data based on a search query. It is based on simple building blocks
  4. called aggregations, that can be composed in order to build complex summaries of the data.
  5. An aggregation can be seen as a _unit-of-work_ that builds analytic information over a set of documents. The context of
  6. the execution defines what this document set is (e.g. a top-level aggregation executes within the context of the executed
  7. query/filters of the search request).
  8. There are many different types of aggregations, each with its own purpose and output. To better understand these types,
  9. it is often easier to break them into two main families:
  10. _Bucketing_::
  11. A family of aggregations that build buckets, where each bucket is associated with a _key_ and a document
  12. criterion. When the aggregation is executed, all the buckets criteria are evaluated on every document in
  13. the context and when a criterion matches, the document is considered to "fall in" the relevant bucket.
  14. By the end of the aggregation process, we'll end up with a list of buckets - each one with a set of
  15. documents that "belong" to it.
  16. _Metric_::
  17. Aggregations that keep track and compute metrics over a set of documents.
  18. The interesting part comes next. Since each bucket effectively defines a document set (all documents belonging to
  19. the bucket), one can potentially associate aggregations on the bucket level, and those will execute within the context
  20. of that bucket. This is where the real power of aggregations kicks in: *aggregations can be nested!*
  21. NOTE: Bucketing aggregations can have sub-aggregations (bucketing or metric). The sub-aggregations will be computed for
  22. the buckets which their parent aggregation generates. There is no hard limit on the level/depth of nested
  23. aggregations (one can nest an aggregation under a "parent" aggregation, which is itself a sub-aggregation of
  24. another higher-level aggregation).
  25. [float]
  26. === Structuring Aggregations
  27. The following snippet captures the basic structure of aggregations:
  28. [source,js]
  29. --------------------------------------------------
  30. "aggregations" : {
  31. "<aggregation_name>" : {
  32. "<aggregation_type>" : {
  33. <aggregation_body>
  34. }
  35. [,"aggregations" : { [<sub_aggregation>]+ } ]?
  36. }
  37. [,"<aggregation_name_2>" : { ... } ]*
  38. }
  39. --------------------------------------------------
  40. The `aggregations` object (the key `aggs` can also be used) in the JSON holds the aggregations to be computed. Each aggregation
  41. is associated with a logical name that the user defines (e.g. if the aggregation computes the average price, then it would
  42. make sense to name it `avg_price`). These logical names will also be used to uniquely identify the aggregations in the
  43. response. Each aggregation has a specific type (`<aggregation_type>` in the above snippet) and is typically the first
  44. key within the named aggregation body. Each type of aggregation defines its own body, depending on the nature of the
  45. aggregation (e.g. an `avg` aggregation on a specific field will define the field on which the average will be calculated).
  46. At the same level of the aggregation type definition, one can optionally define a set of additional aggregations,
  47. though this only makes sense if the aggregation you defined is of a bucketing nature. In this scenario, the
  48. sub-aggregations you define on the bucketing aggregation level will be computed for all the buckets built by the
  49. bucketing aggregation. For example, if you define a set of aggregations under the `range` aggregation, the
  50. sub-aggregations will be computed for the range buckets that are defined.
  51. [float]
  52. ==== Values Source
  53. Some aggregations work on values extracted from the aggregated documents. Typically, the values will be extracted from
  54. a specific document field which is set using the `field` key for the aggregations. It is also possible to define a
  55. <<modules-scripting,`script`>> which will generate the values (per document).
  56. When both `field` and `script` settings are configured for the aggregation, the script will be treated as a
  57. `value script`. While normal scripts are evaluated on a document level (i.e. the script has access to all the data
  58. associated with the document), value scripts are evaluated on the *value* level. In this mode, the values are extracted
  59. from the configured `field` and the `script` is used to apply a "transformation" over these value/s.
  60. ["NOTE",id="aggs-script-note"]
  61. ===============================
  62. When working with scripts, the `lang` and `params` settings can also be defined. The former defines the scripting
  63. language which is used (assuming the proper language is available in Elasticsearch, either by default or as a plugin). The latter
  64. enables defining all the "dynamic" expressions in the script as parameters, which enables the script to keep itself static
  65. between calls (this will ensure the use of the cached compiled scripts in Elasticsearch).
  66. ===============================
  67. Scripts can generate a single value or multiple values per document. When generating multiple values, one can use the
  68. `script_values_sorted` settings to indicate whether these values are sorted or not. Internally, Elasticsearch can
  69. perform optimizations when dealing with sorted values (for example, with the `min` aggregations, knowing the values are
  70. sorted, Elasticsearch will skip the iterations over all the values and rely on the first value in the list to be the
  71. minimum value among all other values associated with the same document).
  72. [float]
  73. === Metrics Aggregations
  74. The aggregations in this family compute metrics based on values extracted in one way or another from the documents that
  75. are being aggregated. The values are typically extracted from the fields of the document (using the field data), but
  76. can also be generated using scripts.
  77. Numeric metrics aggregations are a special type of metrics aggregation which output numeric values. Some aggregations output
  78. a single numeric metric (e.g. `avg`) and are called `single-value numeric metrics aggregation`, others generate multiple
  79. metrics (e.g. `stats`) and are called `multi-value numeric metrics aggregation`. The distinction between single-value and
  80. multi-value numeric metrics aggregations plays a role when these aggregations serve as direct sub-aggregations of some
  81. bucket aggregations (some bucket aggregations enable you to sort the returned buckets based on the numeric metrics in each bucket).
  82. [float]
  83. === Bucket Aggregations
  84. Bucket aggregations don't calculate metrics over fields like the metrics aggregations do, but instead, they create
  85. buckets of documents. Each bucket is associated with a criterion (depending on the aggregation type) which determines
  86. whether or not a document in the current context "falls" into it. In other words, the buckets effectively define document
  87. sets. In addition to the buckets themselves, the `bucket` aggregations also compute and return the number of documents
  88. that "fell in" to each bucket.
  89. Bucket aggregations, as opposed to `metrics` aggregations, can hold sub-aggregations. These sub-aggregations will be
  90. aggregated for the buckets created by their "parent" bucket aggregation.
  91. There are different bucket aggregators, each with a different "bucketing" strategy. Some define a single bucket, some
  92. define fixed number of multiple buckets, and others dynamically create the buckets during the aggregation process.
  93. [float]
  94. === Caching heavy aggregations
  95. Frequently used aggregations (e.g. for display on the home page of a website)
  96. can be cached for faster responses. These cached results are the same results
  97. that would be returned by an uncached aggregation -- you will never get stale
  98. results.
  99. See <<index-modules-shard-query-cache>> for more details.
  100. [float]
  101. === Returning only aggregation results
  102. There are many occasions when aggregations are required but search hits are not. For these cases the hits can be ignored by
  103. adding `search_type=count` to the request URL parameters. For example:
  104. [source,js]
  105. --------------------------------------------------
  106. $ curl -XGET 'http://localhost:9200/twitter/tweet/_search?search_type=count' -d '{
  107. "aggregations": {
  108. "my_agg": {
  109. "terms": {
  110. "field": "text"
  111. }
  112. }
  113. }
  114. }
  115. '
  116. --------------------------------------------------
  117. Setting `search_type` to `count` avoids executing the fetch phase of the search making the request more efficient. See
  118. <<search-request-search-type>> for more information on the `search_type` parameter.
  119. include::aggregations/metrics.asciidoc[]
  120. include::aggregations/bucket.asciidoc[]