multi-terms-aggregation.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. [role="xpack"]
  2. [[search-aggregations-bucket-multi-terms-aggregation]]
  3. === Multi Terms aggregation
  4. ++++
  5. <titleabbrev>Multi Terms</titleabbrev>
  6. ++++
  7. A multi-bucket value source based aggregation where buckets are dynamically built - one per unique set of values. The multi terms
  8. aggregation is very similar to the <<search-aggregations-bucket-terms-aggregation-order,`terms aggregation`>>, however in most cases
  9. it will be slower than the terms aggregation and will consume more memory. Therefore, if the same set of fields is constantly used,
  10. it would be more efficient to index a combined key for this fields as a separate field and use the terms aggregation on this field.
  11. The multi_term aggregations are the most useful when you need to sort by a number of document or a metric aggregation on a composite
  12. key and get top N results. If sorting is not required and all values are expected to be retrieved using nested terms aggregation or
  13. <<search-aggregations-bucket-composite-aggregation, `composite aggregations`>> will be a faster and more memory efficient solution.
  14. //////////////////////////
  15. [source,js]
  16. --------------------------------------------------
  17. PUT /products
  18. {
  19. "mappings": {
  20. "properties": {
  21. "genre": {
  22. "type": "keyword"
  23. },
  24. "product": {
  25. "type": "keyword"
  26. },
  27. "quantity": {
  28. "type": "integer"
  29. }
  30. }
  31. }
  32. }
  33. POST /products/_bulk?refresh
  34. {"index":{"_id":0}}
  35. {"genre": "rock", "product": "Product A", "quantity": 4}
  36. {"index":{"_id":1}}
  37. {"genre": "rock", "product": "Product A", "quantity": 5}
  38. {"index":{"_id":2}}
  39. {"genre": "rock", "product": "Product B", "quantity": 1}
  40. {"index":{"_id":3}}
  41. {"genre": "jazz", "product": "Product B", "quantity": 10}
  42. {"index":{"_id":4}}
  43. {"genre": "electronic", "product": "Product B", "quantity": 3}
  44. {"index":{"_id":5}}
  45. {"genre": "electronic"}
  46. -------------------------------------------------
  47. // NOTCONSOLE
  48. // TESTSETUP
  49. //////////////////////////
  50. Example:
  51. [source,console,id=multi-terms-aggregation-example]
  52. --------------------------------------------------
  53. GET /products/_search
  54. {
  55. "aggs": {
  56. "genres_and_products": {
  57. "multi_terms": {
  58. "terms": [{
  59. "field": "genre" <1>
  60. }, {
  61. "field": "product"
  62. }]
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // TEST[s/_search/_search\?filter_path=aggregations/]
  69. <1> `multi_terms` aggregation can work with the same field types as a
  70. <<search-aggregations-bucket-terms-aggregation-order,`terms aggregation`>> and supports most of the terms aggregation parameters.
  71. Response:
  72. [source,console-result]
  73. --------------------------------------------------
  74. {
  75. ...
  76. "aggregations" : {
  77. "genres_and_products" : {
  78. "doc_count_error_upper_bound" : 0, <1>
  79. "sum_other_doc_count" : 0, <2>
  80. "buckets" : [ <3>
  81. {
  82. "key" : [ <4>
  83. "rock",
  84. "Product A"
  85. ],
  86. "key_as_string" : "rock|Product A",
  87. "doc_count" : 2
  88. },
  89. {
  90. "key" : [
  91. "electronic",
  92. "Product B"
  93. ],
  94. "key_as_string" : "electronic|Product B",
  95. "doc_count" : 1
  96. },
  97. {
  98. "key" : [
  99. "jazz",
  100. "Product B"
  101. ],
  102. "key_as_string" : "jazz|Product B",
  103. "doc_count" : 1
  104. },
  105. {
  106. "key" : [
  107. "rock",
  108. "Product B"
  109. ],
  110. "key_as_string" : "rock|Product B",
  111. "doc_count" : 1
  112. }
  113. ]
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // TESTRESPONSE[s/\.\.\.//]
  119. <1> an upper bound of the error on the document counts for each term, see <<search-aggregations-bucket-multi-terms-aggregation-approximate-counts,below>
  120. <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
  121. <3> the list of the top buckets.
  122. <4> the keys are arrays of values ordered the same ways as expression in the `terms` parameter of the aggregation
  123. By default, the `multi_terms` aggregation will return the buckets for the top ten terms ordered by the `doc_count`. One can
  124. change this default behaviour by setting the `size` parameter.
  125. [[search-aggregations-bucket-multi-terms-aggregation-parameters]]
  126. ==== Aggregation Parameters
  127. The following parameters are supported. See <<search-aggregations-bucket-terms-aggregation-order,`terms aggregation`>> for more detailed
  128. explanation of these parameters.
  129. [horizontal]
  130. size:: Optional. Defines how many term buckets should be returned out of the overall terms list. Defaults to 10.
  131. shard_size:: Optional. The higher the requested `size` is, the more accurate the results will be, but also, the more
  132. expensive it will be to compute the final results. The default `shard_size` is `(size * 1.5 + 10)`.
  133. show_term_doc_count_error:: Optional. Calculates the doc count error on per term basis. Defaults to `false`
  134. order:: Optional. Specifies the order of the buckets. Defaults to the number of documents per bucket. The bucket terms
  135. value is used as a tiebreaker for buckets with the same document count.
  136. min_doc_count:: Optional. The minimal number of documents in a bucket for it to be returned. Defaults to 1.
  137. shard_min_doc_count:: Optional. The minimal number of documents in a bucket on each shard for it to be returned. Defaults to
  138. `min_doc_count`.
  139. collect_mode:: Optional. Specifies the strategy for data collection. The `depth_first` or `breadth_first` modes are
  140. supported. Defaults to `breadth_first`.
  141. [[search-aggregations-bucket-multi-terms-aggregation-script]]
  142. ==== Script
  143. Generating the terms using a script:
  144. [source,console,id=multi-terms-aggregation-runtime-field-example]
  145. ----
  146. GET /products/_search
  147. {
  148. "runtime_mappings": {
  149. "genre.length": {
  150. "type": "long",
  151. "script": "emit(doc['genre'].value.length())"
  152. }
  153. },
  154. "aggs": {
  155. "genres_and_products": {
  156. "multi_terms": {
  157. "terms": [
  158. {
  159. "field": "genre.length"
  160. },
  161. {
  162. "field": "product"
  163. }
  164. ]
  165. }
  166. }
  167. }
  168. }
  169. ----
  170. // TEST[s/_search/_search\?filter_path=aggregations/]
  171. Response:
  172. [source,console-result]
  173. --------------------------------------------------
  174. {
  175. ...
  176. "aggregations" : {
  177. "genres_and_products" : {
  178. "doc_count_error_upper_bound" : 0,
  179. "sum_other_doc_count" : 0,
  180. "buckets" : [
  181. {
  182. "key" : [
  183. 4,
  184. "Product A"
  185. ],
  186. "key_as_string" : "4|Product A",
  187. "doc_count" : 2
  188. },
  189. {
  190. "key" : [
  191. 4,
  192. "Product B"
  193. ],
  194. "key_as_string" : "4|Product B",
  195. "doc_count" : 2
  196. },
  197. {
  198. "key" : [
  199. 10,
  200. "Product B"
  201. ],
  202. "key_as_string" : "10|Product B",
  203. "doc_count" : 1
  204. }
  205. ]
  206. }
  207. }
  208. }
  209. --------------------------------------------------
  210. // TESTRESPONSE[s/\.\.\.//]
  211. ==== Missing value
  212. The `missing` parameter defines how documents that are missing a value should be treated.
  213. By default if any of the key components are missing the entire document will be ignored
  214. but it is also possible to treat them as if they had a value by using the `missing` parameter.
  215. [source,console,id=multi-terms-aggregation-missing-example]
  216. --------------------------------------------------
  217. GET /products/_search
  218. {
  219. "aggs": {
  220. "genres_and_products": {
  221. "multi_terms": {
  222. "terms": [
  223. {
  224. "field": "genre"
  225. },
  226. {
  227. "field": "product",
  228. "missing": "Product Z"
  229. }
  230. ]
  231. }
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. // TEST[s/_search/_search\?filter_path=aggregations/]
  237. Response:
  238. [source,console-result]
  239. --------------------------------------------------
  240. {
  241. ...
  242. "aggregations" : {
  243. "genres_and_products" : {
  244. "doc_count_error_upper_bound" : 0,
  245. "sum_other_doc_count" : 0,
  246. "buckets" : [
  247. {
  248. "key" : [
  249. "rock",
  250. "Product A"
  251. ],
  252. "key_as_string" : "rock|Product A",
  253. "doc_count" : 2
  254. },
  255. {
  256. "key" : [
  257. "electronic",
  258. "Product B"
  259. ],
  260. "key_as_string" : "electronic|Product B",
  261. "doc_count" : 1
  262. },
  263. {
  264. "key" : [
  265. "electronic",
  266. "Product Z"
  267. ],
  268. "key_as_string" : "electronic|Product Z", <1>
  269. "doc_count" : 1
  270. },
  271. {
  272. "key" : [
  273. "jazz",
  274. "Product B"
  275. ],
  276. "key_as_string" : "jazz|Product B",
  277. "doc_count" : 1
  278. },
  279. {
  280. "key" : [
  281. "rock",
  282. "Product B"
  283. ],
  284. "key_as_string" : "rock|Product B",
  285. "doc_count" : 1
  286. }
  287. ]
  288. }
  289. }
  290. }
  291. --------------------------------------------------
  292. // TESTRESPONSE[s/\.\.\.//]
  293. <1> Documents without a value in the `product` field will fall into the same bucket as documents that have the value `Product Z`.
  294. ==== Mixing field types
  295. WARNING: When aggregating on multiple indices the type of the aggregated field may not be the same in all indices.
  296. Some types are compatible with each other (`integer` and `long` or `float` and `double`) but when the types are a mix
  297. of decimal and non-decimal number the terms aggregation will promote the non-decimal numbers to decimal numbers.
  298. This can result in a loss of precision in the bucket values.
  299. ==== Sub aggregation and sorting examples
  300. As most bucket aggregations the `multi_term` supports sub aggregations and ordering the buckets by metrics sub-aggregation:
  301. [source,console,id=multi-terms-aggregation-subaggregation-example]
  302. --------------------------------------------------
  303. GET /products/_search
  304. {
  305. "aggs": {
  306. "genres_and_products": {
  307. "multi_terms": {
  308. "terms": [
  309. {
  310. "field": "genre"
  311. },
  312. {
  313. "field": "product"
  314. }
  315. ],
  316. "order": {
  317. "total_quantity": "desc"
  318. }
  319. },
  320. "aggs": {
  321. "total_quantity": {
  322. "sum": {
  323. "field": "quantity"
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. --------------------------------------------------
  331. // TEST[s/_search/_search\?filter_path=aggregations/]
  332. [source,console-result]
  333. --------------------------------------------------
  334. {
  335. ...
  336. "aggregations" : {
  337. "genres_and_products" : {
  338. "doc_count_error_upper_bound" : 0,
  339. "sum_other_doc_count" : 0,
  340. "buckets" : [
  341. {
  342. "key" : [
  343. "jazz",
  344. "Product B"
  345. ],
  346. "key_as_string" : "jazz|Product B",
  347. "doc_count" : 1,
  348. "total_quantity" : {
  349. "value" : 10.0
  350. }
  351. },
  352. {
  353. "key" : [
  354. "rock",
  355. "Product A"
  356. ],
  357. "key_as_string" : "rock|Product A",
  358. "doc_count" : 2,
  359. "total_quantity" : {
  360. "value" : 9.0
  361. }
  362. },
  363. {
  364. "key" : [
  365. "electronic",
  366. "Product B"
  367. ],
  368. "key_as_string" : "electronic|Product B",
  369. "doc_count" : 1,
  370. "total_quantity" : {
  371. "value" : 3.0
  372. }
  373. },
  374. {
  375. "key" : [
  376. "rock",
  377. "Product B"
  378. ],
  379. "key_as_string" : "rock|Product B",
  380. "doc_count" : 1,
  381. "total_quantity" : {
  382. "value" : 1.0
  383. }
  384. }
  385. ]
  386. }
  387. }
  388. }
  389. --------------------------------------------------
  390. // TESTRESPONSE[s/\.\.\.//]