terms-aggregation.asciidoc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. [[search-aggregations-bucket-terms-aggregation]]
  2. === Terms Aggregation
  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. "doc_count_error_upper_bound": 0, <1>
  23. "sum_other_doc_count": 0, <2>
  24. "buckets" : [ <3>
  25. {
  26. "key" : "male",
  27. "doc_count" : 10
  28. },
  29. {
  30. "key" : "female",
  31. "doc_count" : 10
  32. },
  33. ]
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. <1> an upper bound of the error on the document counts for each term, see <<search-aggregations-bucket-terms-aggregation-approximate-counts,below>>
  39. <2> when there are lots of unique terms, elasticsearch only returns the top terms; this number is the sum of the document counts for all buckets that are not part of the response
  40. <3> the list of the top buckets, the meaning of `top` being defined by the <<search-aggregations-bucket-terms-aggregation-order,order>>
  41. By default, the `terms` aggregation will return the buckets for the top ten terms ordered by the `doc_count`. One can
  42. change this default behaviour by setting the `size` parameter.
  43. ==== Size
  44. The `size` parameter can be set to define how many term buckets should be returned out of the overall terms list. By
  45. default, the node coordinating the search process will request each shard to provide its own top `size` term buckets
  46. and once all shards respond, it will reduce the results to the final list that will then be returned to the client.
  47. This means that if the number of unique terms is greater than `size`, the returned list is slightly off and not accurate
  48. (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
  49. size buckets was not returned). If set to `0`, the `size` will be set to `Integer.MAX_VALUE`.
  50. [[search-aggregations-bucket-terms-aggregation-approximate-counts]]
  51. ==== Document counts are approximate
  52. As described above, the document counts (and the results of any sub aggregations) in the terms aggregation are not always
  53. accurate. This is because each shard provides its own view of what the ordered list of terms should be and these are
  54. combined to give a final view. Consider the following scenario:
  55. A request is made to obtain the top 5 terms in the field product, ordered by descending document count from an index with
  56. 3 shards. In this case each shard is asked to give its top 5 terms.
  57. [source,js]
  58. --------------------------------------------------
  59. {
  60. "aggs" : {
  61. "products" : {
  62. "terms" : {
  63. "field" : "product",
  64. "size" : 5
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. The terms for each of the three shards are shown below with their
  71. respective document counts in brackets:
  72. [width="100%",cols="^2,^2,^2,^2",options="header"]
  73. |=========================================================
  74. | | Shard A | Shard B | Shard C
  75. | 1 | Product A (25) | Product A (30) | Product A (45)
  76. | 2 | Product B (18) | Product B (25) | Product C (44)
  77. | 3 | Product C (6) | Product F (17) | Product Z (36)
  78. | 4 | Product D (3) | Product Z (16) | Product G (30)
  79. | 5 | Product E (2) | Product G (15) | Product E (29)
  80. | 6 | Product F (2) | Product H (14) | Product H (28)
  81. | 7 | Product G (2) | Product I (10) | Product Q (2)
  82. | 8 | Product H (2) | Product Q (6) | Product D (1)
  83. | 9 | Product I (1) | Product J (8) |
  84. | 10 | Product J (1) | Product C (4) |
  85. |=========================================================
  86. The shards will return their top 5 terms so the results from the shards will be:
  87. [width="100%",cols="^2,^2,^2,^2",options="header"]
  88. |=========================================================
  89. | | Shard A | Shard B | Shard C
  90. | 1 | Product A (25) | Product A (30) | Product A (45)
  91. | 2 | Product B (18) | Product B (25) | Product C (44)
  92. | 3 | Product C (6) | Product F (17) | Product Z (36)
  93. | 4 | Product D (3) | Product Z (16) | Product G (30)
  94. | 5 | Product E (2) | Product G (15) | Product E (29)
  95. |=========================================================
  96. Taking the top 5 results from each of the shards (as requested) and combining them to make a final top 5 list produces
  97. the following:
  98. [width="40%",cols="^2,^2"]
  99. |=========================================================
  100. | 1 | Product A (100)
  101. | 2 | Product Z (52)
  102. | 3 | Product C (50)
  103. | 4 | Product G (45)
  104. | 5 | Product B (43)
  105. |=========================================================
  106. Because Product A was returned from all shards we know that its document count value is accurate. Product C was only
  107. returned by shards A and C so its document count is shown as 50 but this is not an accurate count. Product C exists on
  108. shard B, but its count of 4 was not high enough to put Product C into the top 5 list for that shard. Product Z was also
  109. returned only by 2 shards but the third shard does not contain the term. There is no way of knowing, at the point of
  110. combining the results to produce the final list of terms, that there is an error in the document count for Product C and
  111. not for Product Z. Product H has a document count of 44 across all 3 shards but was not included in the final list of
  112. terms because it did not make it into the top five terms on any of the shards.
  113. ==== Shard Size
  114. The higher the requested `size` is, the more accurate the results will be, but also, the more expensive it will be to
  115. compute the final results (both due to bigger priority queues that are managed on a shard level and due to bigger data
  116. transfers between the nodes and the client).
  117. The `shard_size` parameter can be used to minimize the extra work that comes with bigger requested `size`. When defined,
  118. it will determine how many terms the coordinating node will request from each shard. Once all the shards responded, the
  119. coordinating node will then reduce them to a final result which will be based on the `size` parameter - this way,
  120. one can increase the accuracy of the returned terms and avoid the overhead of streaming a big list of buckets back to
  121. the client. If set to `0`, the `shard_size` will be set to `Integer.MAX_VALUE`.
  122. NOTE: `shard_size` cannot be smaller than `size` (as it doesn't make much sense). When it is, elasticsearch will
  123. override it and reset it to be equal to `size`.
  124. It is possible to not limit the number of terms that are returned by setting `size` to `0`. Don't use this
  125. on high-cardinality fields as this will kill both your CPU since terms need to be return sorted, and your network.
  126. The default `shard_size` is a multiple of the `size` parameter which is dependant on the number of shards.
  127. ==== Calculating Document Count Error
  128. There are two error values which can be shown on the terms aggregation. The first gives a value for the aggregation as
  129. a whole which represents the maximum potential document count for a term which did not make it into the final list of
  130. terms. This is calculated as the sum of the document count from the last term returned from each shard .For the example
  131. given above the value would be 46 (2 + 15 + 29). This means that in the worst case scenario a term which was not returned
  132. could have the 4th highest document count.
  133. [source,js]
  134. --------------------------------------------------
  135. {
  136. ...
  137. "aggregations" : {
  138. "products" : {
  139. "doc_count_error_upper_bound" : 46,
  140. "buckets" : [
  141. {
  142. "key" : "Product A",
  143. "doc_count" : 100
  144. },
  145. {
  146. "key" : "Product Z",
  147. "doc_count" : 52
  148. },
  149. ...
  150. ]
  151. }
  152. }
  153. }
  154. --------------------------------------------------
  155. ==== Per bucket document count error
  156. experimental[]
  157. The second error value can be enabled by setting the `show_term_doc_count_error` parameter to true. This shows an error value
  158. for each term returned by the aggregation which represents the 'worst case' error in the document count and can be useful when
  159. deciding on a value for the `shard_size` parameter. This is calculated by summing the document counts for the last term returned
  160. by all shards which did not return the term. In the example above the error in the document count for Product C would be 15 as
  161. Shard B was the only shard not to return the term and the document count of the last termit did return was 15. The actual document
  162. count of Product C was 54 so the document count was only actually off by 4 even though the worst case was that it would be off by
  163. 15. Product A, however has an error of 0 for its document count, since every shard returned it we can be confident that the count
  164. returned is accurate.
  165. [source,js]
  166. --------------------------------------------------
  167. {
  168. ...
  169. "aggregations" : {
  170. "products" : {
  171. "doc_count_error_upper_bound" : 46,
  172. "buckets" : [
  173. {
  174. "key" : "Product A",
  175. "doc_count" : 100,
  176. "doc_count_error_upper_bound" : 0
  177. },
  178. {
  179. "key" : "Product Z",
  180. "doc_count" : 52,
  181. "doc_count_error_upper_bound" : 2
  182. },
  183. ...
  184. ]
  185. }
  186. }
  187. }
  188. --------------------------------------------------
  189. These errors can only be calculated in this way when the terms are ordered by descending document count. When the aggregation is
  190. ordered by the terms values themselves (either ascending or descending) there is no error in the document count since if a shard
  191. does not return a particular term which appears in the results from another shard, it must not have that term in its index. When the
  192. aggregation is either sorted by a sub aggregation or in order of ascending document count, the error in the document counts cannot be
  193. determined and is given a value of -1 to indicate this.
  194. [[search-aggregations-bucket-terms-aggregation-order]]
  195. ==== Order
  196. The order of the buckets can be customized by setting the `order` parameter. By default, the buckets are ordered by
  197. their `doc_count` descending. It is also possible to change this behaviour as follows:
  198. Ordering the buckets by their `doc_count` in an ascending manner:
  199. [source,js]
  200. --------------------------------------------------
  201. {
  202. "aggs" : {
  203. "genders" : {
  204. "terms" : {
  205. "field" : "gender",
  206. "order" : { "_count" : "asc" }
  207. }
  208. }
  209. }
  210. }
  211. --------------------------------------------------
  212. Ordering the buckets alphabetically by their terms in an ascending manner:
  213. [source,js]
  214. --------------------------------------------------
  215. {
  216. "aggs" : {
  217. "genders" : {
  218. "terms" : {
  219. "field" : "gender",
  220. "order" : { "_term" : "asc" }
  221. }
  222. }
  223. }
  224. }
  225. --------------------------------------------------
  226. Ordering the buckets by single value metrics sub-aggregation (identified by the aggregation name):
  227. [source,js]
  228. --------------------------------------------------
  229. {
  230. "aggs" : {
  231. "genders" : {
  232. "terms" : {
  233. "field" : "gender",
  234. "order" : { "avg_height" : "desc" }
  235. },
  236. "aggs" : {
  237. "avg_height" : { "avg" : { "field" : "height" } }
  238. }
  239. }
  240. }
  241. }
  242. --------------------------------------------------
  243. Ordering the buckets by multi value metrics sub-aggregation (identified by the aggregation name):
  244. [source,js]
  245. --------------------------------------------------
  246. {
  247. "aggs" : {
  248. "genders" : {
  249. "terms" : {
  250. "field" : "gender",
  251. "order" : { "height_stats.avg" : "desc" }
  252. },
  253. "aggs" : {
  254. "height_stats" : { "stats" : { "field" : "height" } }
  255. }
  256. }
  257. }
  258. }
  259. --------------------------------------------------
  260. It is also possible to order the buckets based on a "deeper" aggregation in the hierarchy. This is supported as long
  261. as the aggregations path are of a single-bucket type, where the last aggregation in the path may either be a single-bucket
  262. one or a metrics one. If it's a single-bucket type, the order will be defined by the number of docs in the bucket (i.e. `doc_count`),
  263. in case it's a metrics one, the same rules as above apply (where the path must indicate the metric name to sort by in case of
  264. a multi-value metrics aggregation, and in case of a single-value metrics aggregation the sort will be applied on that value).
  265. The path must be defined in the following form:
  266. --------------------------------------------------
  267. AGG_SEPARATOR := '>'
  268. METRIC_SEPARATOR := '.'
  269. AGG_NAME := <the name of the aggregation>
  270. METRIC := <the name of the metric (in case of multi-value metrics aggregation)>
  271. PATH := <AGG_NAME>[<AGG_SEPARATOR><AGG_NAME>]*[<METRIC_SEPARATOR><METRIC>]
  272. --------------------------------------------------
  273. [source,js]
  274. --------------------------------------------------
  275. {
  276. "aggs" : {
  277. "countries" : {
  278. "terms" : {
  279. "field" : "address.country",
  280. "order" : { "females>height_stats.avg" : "desc" }
  281. },
  282. "aggs" : {
  283. "females" : {
  284. "filter" : { "term" : { "gender" : "female" }},
  285. "aggs" : {
  286. "height_stats" : { "stats" : { "field" : "height" }}
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. --------------------------------------------------
  294. The above will sort the countries buckets based on the average height among the female population.
  295. Multiple criteria can be used to order the buckets by providing an array of order criteria such as the following:
  296. [source,js]
  297. --------------------------------------------------
  298. {
  299. "aggs" : {
  300. "countries" : {
  301. "terms" : {
  302. "field" : "address.country",
  303. "order" : [ { "females>height_stats.avg" : "desc" }, { "_count" : "desc" } ]
  304. },
  305. "aggs" : {
  306. "females" : {
  307. "filter" : { "term" : { "gender" : { "female" }}},
  308. "aggs" : {
  309. "height_stats" : { "stats" : { "field" : "height" }}
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. --------------------------------------------------
  317. The above will sort the countries buckets based on the average height among the female population and then by
  318. their `doc_count` in descending order.
  319. NOTE: In the event that two buckets share the same values for all order criteria the bucket's term value is used as a
  320. tie-breaker in ascending alphabetical order to prevent non-deterministic ordering of buckets.
  321. ==== Minimum document count
  322. It is possible to only return terms that match more than a configured number of hits using the `min_doc_count` option:
  323. [source,js]
  324. --------------------------------------------------
  325. {
  326. "aggs" : {
  327. "tags" : {
  328. "terms" : {
  329. "field" : "tags",
  330. "min_doc_count": 10
  331. }
  332. }
  333. }
  334. }
  335. --------------------------------------------------
  336. The above aggregation would only return tags which have been found in 10 hits or more. Default value is `1`.
  337. Terms are collected and ordered on a shard level and merged with the terms collected from other shards in a second step. However, the shard does not have the information about the global document count available. The decision if a term is added to a candidate list depends only on the order computed on the shard using local shard frequencies. The `min_doc_count` criterion is only applied after merging local terms statistics of all shards. In a way the decision to add the term as a candidate is made without being very _certain_ about if the term will actually reach the required `min_doc_count`. This might cause many (globally) high frequent terms to be missing in the final result if low frequent terms populated the candidate lists. To avoid this, the `shard_size` parameter can be increased to allow more candidate terms on the shards. However, this increases memory consumption and network traffic.
  338. `shard_min_doc_count` parameter
  339. The parameter `shard_min_doc_count` regulates the _certainty_ a shard has if the term should actually be added to the candidate list or not with respect to the `min_doc_count`. Terms will only be considered if their local shard frequency within the set is higher than the `shard_min_doc_count`. If your dictionary contains many low frequent terms and you are not interested in those (for example misspellings), then you can set the `shard_min_doc_count` parameter to filter out candidate terms on a shard level that will with a reasonable certainty not reach the required `min_doc_count` even after merging the local counts. `shard_min_doc_count` is set to `0` per default and has no effect unless you explicitly set it.
  340. NOTE: Setting `min_doc_count`=`0` will also return buckets for terms that didn't match any hit. However, some of
  341. the returned terms which have a document count of zero might only belong to deleted documents or documents
  342. from other types, so there is no warranty that a `match_all` query would find a positive document count for
  343. those terms.
  344. WARNING: When NOT sorting on `doc_count` descending, high values of `min_doc_count` may return a number of buckets
  345. which is less than `size` because not enough data was gathered from the shards. Missing buckets can be
  346. back by increasing `shard_size`.
  347. Setting `shard_min_doc_count` too high will cause terms to be filtered out on a shard level. This value should be set much lower than `min_doc_count/#shards`.
  348. [[search-aggregations-bucket-terms-aggregation-script]]
  349. ==== Script
  350. Generating the terms using a script:
  351. [source,js]
  352. --------------------------------------------------
  353. {
  354. "aggs" : {
  355. "genders" : {
  356. "terms" : {
  357. "script" : "doc['gender'].value"
  358. }
  359. }
  360. }
  361. }
  362. --------------------------------------------------
  363. This will interpret the `script` parameter as an `inline` script with the default script language and no script parameters. To use a file script use the following syntax:
  364. [source,js]
  365. --------------------------------------------------
  366. {
  367. "aggs" : {
  368. "genders" : {
  369. "terms" : {
  370. "script" : {
  371. "file": "my_script",
  372. "params": {
  373. "field": "gender"
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. --------------------------------------------------
  381. TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
  382. ==== Value Script
  383. [source,js]
  384. --------------------------------------------------
  385. {
  386. "aggs" : {
  387. "genders" : {
  388. "terms" : {
  389. "field" : "gender",
  390. "script" : "'Gender: ' +_value"
  391. }
  392. }
  393. }
  394. }
  395. --------------------------------------------------
  396. ==== Filtering Values
  397. It is possible to filter the values for which buckets will be created. This can be done using the `include` and
  398. `exclude` parameters which are based on regular expression strings or arrays of exact values.
  399. [source,js]
  400. --------------------------------------------------
  401. {
  402. "aggs" : {
  403. "tags" : {
  404. "terms" : {
  405. "field" : "tags",
  406. "include" : ".*sport.*",
  407. "exclude" : "water_.*"
  408. }
  409. }
  410. }
  411. }
  412. --------------------------------------------------
  413. In the above example, buckets will be created for all the tags that has the word `sport` in them, except those starting
  414. with `water_` (so the tag `water_sports` will no be aggregated). The `include` regular expression will determine what
  415. values are "allowed" to be aggregated, while the `exclude` determines the values that should not be aggregated. When
  416. both are defined, the `exclude` has precedence, meaning, the `include` is evaluated first and only then the `exclude`.
  417. The syntax is the same as <<regexp-syntax,regexp queries>>.
  418. For matching based on exact values the `include` and `exclude` parameters can simply take an array of
  419. strings that represent the terms as they are found in the index:
  420. [source,js]
  421. --------------------------------------------------
  422. {
  423. "aggs" : {
  424. "JapaneseCars" : {
  425. "terms" : {
  426. "field" : "make",
  427. "include" : ["mazda", "honda"]
  428. }
  429. },
  430. "ActiveCarManufacturers" : {
  431. "terms" : {
  432. "field" : "make",
  433. "exclude" : ["rover", "jensen"]
  434. }
  435. }
  436. }
  437. }
  438. --------------------------------------------------
  439. ==== Multi-field terms aggregation
  440. The `terms` aggregation does not support collecting terms from multiple fields
  441. in the same document. The reason is that the `terms` agg doesn't collect the
  442. string term values themselves, but rather uses
  443. <<search-aggregations-bucket-terms-aggregation-execution-hint,global ordinals>>
  444. to produce a list of all of the unique values in the field. Global ordinals
  445. results in an important performance boost which would not be possible across
  446. multiple fields.
  447. There are two approaches that you can use to perform a `terms` agg across
  448. multiple fields:
  449. <<search-aggregations-bucket-terms-aggregation-script,Script>>::
  450. Use a script to retrieve terms from multiple fields. This disables the global
  451. ordinals optimization and will be slower than collecting terms from a single
  452. field, but it gives you the flexibility to implement this option at search
  453. time.
  454. <<copy-to,`copy_to` field>>::
  455. If you know ahead of time that you want to collect the terms from two or more
  456. fields, then use `copy_to` in your mapping to create a new dedicated field at
  457. index time which contains the values from both fields. You can aggregate on
  458. this single field, which will benefit from the global ordinals optimization.
  459. ==== Collect mode
  460. Deferring calculation of child aggregations
  461. For fields with many unique terms and a small number of required results it can be more efficient to delay the calculation
  462. of child aggregations until the top parent-level aggs have been pruned. Ordinarily, all branches of the aggregation tree
  463. are expanded in one depth-first pass and only then any pruning occurs. In some rare scenarios this can be very wasteful and can hit memory constraints.
  464. An example problem scenario is querying a movie database for the 10 most popular actors and their 5 most common co-stars:
  465. [source,js]
  466. --------------------------------------------------
  467. {
  468. "aggs" : {
  469. "actors" : {
  470. "terms" : {
  471. "field" : "actors",
  472. "size" : 10
  473. },
  474. "aggs" : {
  475. "costars" : {
  476. "terms" : {
  477. "field" : "actors",
  478. "size" : 5
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }
  485. --------------------------------------------------
  486. Even though the number of movies may be comparatively small and we want only 50 result buckets there is a combinatorial explosion of buckets
  487. during calculation - a single movie will produce n² buckets where n is the number of actors. The sane option would be to first determine
  488. the 10 most popular actors and only then examine the top co-stars for these 10 actors. This alternative strategy is what we call the `breadth_first` collection
  489. mode as opposed to the default `depth_first` mode:
  490. [source,js]
  491. --------------------------------------------------
  492. {
  493. "aggs" : {
  494. "actors" : {
  495. "terms" : {
  496. "field" : "actors",
  497. "size" : 10,
  498. "collect_mode" : "breadth_first"
  499. },
  500. "aggs" : {
  501. "costars" : {
  502. "terms" : {
  503. "field" : "actors",
  504. "size" : 5
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. --------------------------------------------------
  512. When using `breadth_first` mode the set of documents that fall into the uppermost buckets are
  513. cached for subsequent replay so there is a memory overhead in doing this which is linear with the number of matching documents.
  514. In most requests the volume of buckets generated is smaller than the number of documents that fall into them so the default `depth_first`
  515. collection mode is normally the best bet but occasionally the `breadth_first` strategy can be significantly more efficient. Currently
  516. elasticsearch will always use the `depth_first` collect_mode unless explicitly instructed to use `breadth_first` as in the above example.
  517. Note that the `order` parameter can still be used to refer to data from a child aggregation when using the `breadth_first` setting - the parent
  518. aggregation understands that this child aggregation will need to be called first before any of the other child aggregations.
  519. WARNING: It is not possible to nest aggregations such as `top_hits` which require access to match score information under an aggregation that uses
  520. the `breadth_first` collection mode. This is because this would require a RAM buffer to hold the float score value for every document and
  521. this would typically be too costly in terms of RAM.
  522. [[search-aggregations-bucket-terms-aggregation-execution-hint]]
  523. ==== Execution hint
  524. experimental[The automated execution optimization is experimental, so this parameter is provided temporarily as a way to override the default behaviour]
  525. There are different mechanisms by which terms aggregations can be executed:
  526. - by using field values directly in order to aggregate data per-bucket (`map`)
  527. - by using ordinals of the field and preemptively allocating one bucket per ordinal value (`global_ordinals`)
  528. - by using ordinals of the field and dynamically allocating one bucket per ordinal value (`global_ordinals_hash`)
  529. - by using per-segment ordinals to compute counts and remap these counts to global counts using global ordinals (`global_ordinals_low_cardinality`)
  530. Elasticsearch tries to have sensible defaults so this is something that generally doesn't need to be configured.
  531. `map` should only be considered when very few documents match a query. Otherwise the ordinals-based execution modes
  532. are significantly faster. By default, `map` is only used when running an aggregation on scripts, since they don't have
  533. ordinals.
  534. `global_ordinals_low_cardinality` only works for leaf terms aggregations but is usually the fastest execution mode. Memory
  535. usage is linear with the number of unique values in the field, so it is only enabled by default on low-cardinality fields.
  536. `global_ordinals` is the second fastest option, but the fact that it preemptively allocates buckets can be memory-intensive,
  537. especially if you have one or more sub aggregations. It is used by default on top-level terms aggregations.
  538. `global_ordinals_hash` on the contrary to `global_ordinals` and `global_ordinals_low_cardinality` allocates buckets dynamically
  539. so memory usage is linear to the number of values of the documents that are part of the aggregation scope. It is used by default
  540. in inner aggregations.
  541. [source,js]
  542. --------------------------------------------------
  543. {
  544. "aggs" : {
  545. "tags" : {
  546. "terms" : {
  547. "field" : "tags",
  548. "execution_hint": "map" <1>
  549. }
  550. }
  551. }
  552. }
  553. --------------------------------------------------
  554. <1> experimental[] the possible values are `map`, `global_ordinals`, `global_ordinals_hash` and `global_ordinals_low_cardinality`
  555. Please note that Elasticsearch will ignore this execution hint if it is not applicable and that there is no backward compatibility guarantee on these hints.
  556. ==== Missing value
  557. The `missing` parameter defines how documents that are missing a value should be treated.
  558. By default they will be ignored but it is also possible to treat them as if they
  559. had a value.
  560. [source,js]
  561. --------------------------------------------------
  562. {
  563. "aggs" : {
  564. "tags" : {
  565. "terms" : {
  566. "field" : "tags",
  567. "missing": "N/A" <1>
  568. }
  569. }
  570. }
  571. }
  572. --------------------------------------------------
  573. <1> Documents without a value in the `tags` field will fall into the same bucket as documents that have the value `N/A`.