terms-aggregation.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. [[search-aggregations-bucket-terms-aggregation]]
  2. === Terms
  3. A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value.
  4. Example:
  5. [source,js]
  6. --------------------------------------------------
  7. {
  8. "aggs" : {
  9. "genders" : {
  10. "terms" : { "field" : "gender" }
  11. }
  12. }
  13. }
  14. --------------------------------------------------
  15. Response:
  16. [source,js]
  17. --------------------------------------------------
  18. {
  19. ...
  20. "aggregations" : {
  21. "genders" : {
  22. "buckets" : [
  23. {
  24. "key" : "male",
  25. "doc_count" : 10
  26. },
  27. {
  28. "key" : "female",
  29. "doc_count" : 10
  30. },
  31. ]
  32. }
  33. }
  34. }
  35. --------------------------------------------------
  36. By default, the `terms` aggregation will return the buckets for the top ten terms ordered by the `doc_count`. One can
  37. change this default behaviour by setting the `size` parameter.
  38. ==== Size & Shard Size
  39. The `size` parameter can be set to define how many term buckets should be returned out of the overall terms list. By
  40. default, the node coordinating the search process will request each shard to provide its own top `size` term buckets
  41. and once all shards respond, it will reduce the results to the final list that will then be returned to the client.
  42. This means that if the number of unique terms is greater than `size`, the returned list is slightly off and not accurate
  43. (it could be that the term counts are slightly off and it could even be that a term that should have been in the top
  44. size buckets was not returned).
  45. The higher the requested `size` is, the more accurate the results will be, but also, the more expensive it will be to
  46. compute the final results (both due to bigger priority queues that are managed on a shard level and due to bigger data
  47. transfers between the nodes and the client).
  48. The `shard_size` parameter can be used to minimize the extra work that comes with bigger requested `size`. When defined,
  49. it will determine how many terms the coordinating node will request from each shard. Once all the shards responded, the
  50. coordinating node will then reduce them to a final result which will be based on the `size` parameter - this way,
  51. one can increase the accuracy of the returned terms and avoid the overhead of streaming a big list of buckets back to
  52. the client.
  53. NOTE: `shard_size` cannot be smaller than `size` (as it doesn't make much sense). When it is, elasticsearch will
  54. override it and reset it to be equal to `size`.
  55. added[1.1.0] It is possible to not limit the number of terms that are returned by setting `size` to `0`. Don't use this
  56. on high-cardinality fields as this will kill both your CPU since terms need to be return sorted, and your network.
  57. ==== Order
  58. The order of the buckets can be customized by setting the `order` parameter. By default, the buckets are ordered by
  59. their `doc_count` descending. It is also possible to change this behaviour as follows:
  60. Ordering the buckets by their `doc_count` in an ascending manner:
  61. [source,js]
  62. --------------------------------------------------
  63. {
  64. "aggs" : {
  65. "genders" : {
  66. "terms" : {
  67. "field" : "gender",
  68. "order" : { "_count" : "asc" }
  69. }
  70. }
  71. }
  72. }
  73. --------------------------------------------------
  74. Ordering the buckets alphabetically by their terms in an ascending manner:
  75. [source,js]
  76. --------------------------------------------------
  77. {
  78. "aggs" : {
  79. "genders" : {
  80. "terms" : {
  81. "field" : "gender",
  82. "order" : { "_term" : "asc" }
  83. }
  84. }
  85. }
  86. }
  87. --------------------------------------------------
  88. Ordering the buckets by single value metrics sub-aggregation (identified by the aggregation name):
  89. [source,js]
  90. --------------------------------------------------
  91. {
  92. "aggs" : {
  93. "genders" : {
  94. "terms" : {
  95. "field" : "gender",
  96. "order" : { "avg_height" : "desc" }
  97. },
  98. "aggs" : {
  99. "avg_height" : { "avg" : { "field" : "height" } }
  100. }
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. Ordering the buckets by multi value metrics sub-aggregation (identified by the aggregation name):
  106. [source,js]
  107. --------------------------------------------------
  108. {
  109. "aggs" : {
  110. "genders" : {
  111. "terms" : {
  112. "field" : "gender",
  113. "order" : { "stats.avg" : "desc" }
  114. },
  115. "aggs" : {
  116. "height_stats" : { "stats" : { "field" : "height" } }
  117. }
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. ==== Minimum document count
  123. It is possible to only return terms that match more than a configured number of hits using the `min_doc_count` option:
  124. [source,js]
  125. --------------------------------------------------
  126. {
  127. "aggs" : {
  128. "tags" : {
  129. "terms" : {
  130. "field" : "tag",
  131. "min_doc_count": 10
  132. }
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. The above aggregation would only return tags which have been found in 10 hits or more. Default value is `1`.
  138. NOTE: Setting `min_doc_count`=`0` will also return buckets for terms that didn't match any hit. However, some of
  139. the returned terms which have a document count of zero might only belong to deleted documents, so there is
  140. no warranty that a `match_all` query would find a positive document count for those terms.
  141. WARNING: When NOT sorting on `doc_count` descending, high values of `min_doc_count` may return a number of buckets
  142. which is less than `size` because not enough data was gathered from the shards. Missing buckets can be
  143. back by increasing `shard_size`.
  144. ==== Script
  145. Generating the terms using a script:
  146. [source,js]
  147. --------------------------------------------------
  148. {
  149. "aggs" : {
  150. "genders" : {
  151. "terms" : {
  152. "script" : "doc['gender'].value"
  153. }
  154. }
  155. }
  156. }
  157. --------------------------------------------------
  158. ==== Value Script
  159. [source,js]
  160. --------------------------------------------------
  161. {
  162. "aggs" : {
  163. "genders" : {
  164. "terms" : {
  165. "field" : "gender",
  166. "script" : "'Gender: ' +_value"
  167. }
  168. }
  169. }
  170. }
  171. --------------------------------------------------
  172. ==== Filtering Values
  173. It is possible to filter the values for which buckets will be created. This can be done using the `include` and
  174. `exclude` parameters which are based on regular expressions.
  175. [source,js]
  176. --------------------------------------------------
  177. {
  178. "aggs" : {
  179. "tags" : {
  180. "terms" : {
  181. "field" : "tags",
  182. "include" : ".*sport.*",
  183. "exclude" : "water_.*"
  184. }
  185. }
  186. }
  187. }
  188. --------------------------------------------------
  189. In the above example, buckets will be created for all the tags that has the word `sport` in them, except those starting
  190. with `water_` (so the tag `water_sports` will no be aggregated). The `include` regular expression will determine what
  191. values are "allowed" to be aggregated, while the `exclude` determines the values that should not be aggregated. When
  192. both are defined, the `exclude` has precedence, meaning, the `include` is evaluated first and only then the `exclude`.
  193. The regular expression are based on the Java(TM) http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[Pattern],
  194. and as such, they it is also possible to pass in flags that will determine how the compiled regular expression will work:
  195. [source,js]
  196. --------------------------------------------------
  197. {
  198. "aggs" : {
  199. "tags" : {
  200. "terms" : {
  201. "field" : "tags",
  202. "include" : {
  203. "pattern" : ".*sport.*",
  204. "flags" : "CANON_EQ|CASE_INSENSITIVE" <1>
  205. },
  206. "exclude" : {
  207. "pattern" : "water_.*",
  208. "flags" : "CANON_EQ|CASE_INSENSITIVE"
  209. }
  210. }
  211. }
  212. }
  213. }
  214. --------------------------------------------------
  215. <1> the flags are concatenated using the `|` character as a separator
  216. The possible flags that can be used are:
  217. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CANON_EQ[`CANON_EQ`],
  218. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CASE_INSENSITIVE[`CASE_INSENSITIVE`],
  219. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#COMMENTS[`COMMENTS`],
  220. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#DOTALL[`DOTALL`],
  221. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#LITERAL[`LITERAL`],
  222. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#MULTILINE[`MULTILINE`],
  223. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#UNICODE_CASE[`UNICODE_CASE`],
  224. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#UNICODE_CHARACTER_CLASS[`UNICODE_CHARACTER_CLASS`] and
  225. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#UNIX_LINES[`UNIX_LINES`]
  226. ==== Execution hint
  227. There are two mechanisms by which terms aggregations can be executed: either by using field values directly in order to aggregate
  228. data per-bucket (`map`), or by using ordinals of the field values instead of the values themselves (`ordinals`). Although the
  229. latter execution mode can be expected to be slightly faster, it is only available for use when the underlying data source exposes
  230. those terms ordinals. Moreover, it may actually be slower if most field values are unique. Elasticsearch tries to have sensible
  231. defaults when it comes to the execution mode that should be used, but in case you know that one execution mode may perform better
  232. than the other one, you have the ability to "hint" it to Elasticsearch:
  233. [source,js]
  234. --------------------------------------------------
  235. {
  236. "aggs" : {
  237. "tags" : {
  238. "terms" : {
  239. "field" : "tags",
  240. "execution_hint": "map" <1>
  241. }
  242. }
  243. }
  244. }
  245. --------------------------------------------------
  246. <1> the possible values are `map` and `ordinals`
  247. Please note that Elasticsearch will ignore this execution hint if it is not applicable.