composite-aggregation.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. [[search-aggregations-bucket-composite-aggregation]]
  2. === Composite Aggregation
  3. experimental[]
  4. A multi-bucket aggregation that creates composite buckets from different sources.
  5. Unlike the other `multi-bucket` aggregation the `composite` aggregation can be used
  6. to paginate **all** buckets from a multi-level aggregation efficiently. This aggregation
  7. provides a way to stream **all** buckets of a specific aggregation similarly to what
  8. <<search-request-scroll, scroll>> does for documents.
  9. The composite buckets are built from the combinations of the
  10. values extracted/created for each document and each combination is considered as
  11. a composite bucket.
  12. //////////////////////////
  13. [source,js]
  14. --------------------------------------------------
  15. PUT /sales
  16. {
  17. "mappings": {
  18. "docs": {
  19. "properties": {
  20. "product": {
  21. "type": "keyword"
  22. },
  23. "timestamp": {
  24. "type": "date"
  25. },
  26. "price": {
  27. "type": "long"
  28. },
  29. "shop": {
  30. "type": "keyword"
  31. }
  32. }
  33. }
  34. }
  35. }
  36. POST /sales/docs/_bulk?refresh
  37. {"index":{"_id":0}}
  38. {"product": "mad max", "price": "20", "timestamp": "2017-05-09T14:35"}
  39. {"index":{"_id":1}}
  40. {"product": "mad max", "price": "25", "timestamp": "2017-05-09T12:35"}
  41. {"index":{"_id":2}}
  42. {"product": "rocky", "price": "10", "timestamp": "2017-05-08T09:10"}
  43. {"index":{"_id":3}}
  44. {"product": "mad max", "price": "27", "timestamp": "2017-05-10T07:07"}
  45. {"index":{"_id":4}}
  46. {"product": "apocalypse now", "price": "10", "timestamp": "2017-05-11T08:35"}
  47. -------------------------------------------------
  48. // NOTCONSOLE
  49. // TESTSETUP
  50. //////////////////////////
  51. For instance the following document:
  52. ```
  53. {
  54. "keyword": ["foo", "bar"],
  55. "number": [23, 65, 76]
  56. }
  57. ```
  58. \... creates the following composite buckets when `keyword` and `number` are used as values source
  59. for the aggregation:
  60. ```
  61. { "keyword": "foo", "number": 23 }
  62. { "keyword": "foo", "number": 65 }
  63. { "keyword": "foo", "number": 76 }
  64. { "keyword": "bar", "number": 23 }
  65. { "keyword": "bar", "number": 65 }
  66. { "keyword": "bar", "number": 76 }
  67. ```
  68. ==== Values source
  69. The `values` parameter controls the sources that should be used to build the composite buckets.
  70. There are three different types of values source:
  71. ===== Terms
  72. The `terms` value source is equivalent to a simple `terms` aggregation.
  73. The values are extracted from a field or a script exactly like the `terms` aggregation.
  74. Example:
  75. [source,js]
  76. --------------------------------------------------
  77. GET /_search
  78. {
  79. "aggs" : {
  80. "my_buckets": {
  81. "composite" : {
  82. "sources" : [
  83. { "product": { "terms" : { "field": "product" } } }
  84. ]
  85. }
  86. }
  87. }
  88. }
  89. --------------------------------------------------
  90. // CONSOLE
  91. Like the `terms` aggregation it is also possible to use a script to create the values for the composite buckets:
  92. [source,js]
  93. --------------------------------------------------
  94. GET /_search
  95. {
  96. "aggs" : {
  97. "my_buckets": {
  98. "composite" : {
  99. "sources" : [
  100. {
  101. "product": {
  102. "terms" : {
  103. "script" : {
  104. "source": "doc['product'].value",
  105. "lang": "painless"
  106. }
  107. }
  108. }
  109. }
  110. ]
  111. }
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // CONSOLE
  117. ===== Histogram
  118. The `histogram` value source can be applied on numeric values to build fixed size
  119. interval over the values. The `interval` parameter defines how the numeric values should be
  120. transformed. For instance an `interval` set to 5 will translate any numeric values to its closest interval,
  121. a value of `101` would be translated to `100` which is the key for the interval between 100 and 105.
  122. Example:
  123. [source,js]
  124. --------------------------------------------------
  125. GET /_search
  126. {
  127. "aggs" : {
  128. "my_buckets": {
  129. "composite" : {
  130. "sources" : [
  131. { "histo": { "histogram" : { "field": "price", "interval": 5 } } }
  132. ]
  133. }
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. // CONSOLE
  139. The values are built from a numeric field or a script that return numerical values:
  140. [source,js]
  141. --------------------------------------------------
  142. GET /_search
  143. {
  144. "aggs" : {
  145. "my_buckets": {
  146. "composite" : {
  147. "sources" : [
  148. {
  149. "histo": {
  150. "histogram" : {
  151. "interval": 5,
  152. "script" : {
  153. "source": "doc['price'].value",
  154. "lang": "painless"
  155. }
  156. }
  157. }
  158. }
  159. ]
  160. }
  161. }
  162. }
  163. }
  164. --------------------------------------------------
  165. // CONSOLE
  166. ===== Date Histogram
  167. The `date_histogram` is similar to the `histogram` value source except that the interval
  168. is specified by date/time expression:
  169. [source,js]
  170. --------------------------------------------------
  171. GET /_search
  172. {
  173. "aggs" : {
  174. "my_buckets": {
  175. "composite" : {
  176. "sources" : [
  177. { "date": { "date_histogram" : { "field": "timestamp", "interval": "1d" } } }
  178. ]
  179. }
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // CONSOLE
  185. The example above creates an interval per day and translates all `timestamp` values to the start of its closest intervals.
  186. Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
  187. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  188. Note that fractional time values are not supported, but you can address this by shifting to another
  189. time unit (e.g., `1.5h` could instead be specified as `90m`).
  190. ====== Time Zone
  191. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  192. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  193. that bucketing should use a different time zone.
  194. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  195. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  196. `America/Los_Angeles`.
  197. ===== Mixing different values source
  198. The `sources` parameter accepts an array of values source.
  199. It is possible to mix different values source to create composite buckets.
  200. For example:
  201. [source,js]
  202. --------------------------------------------------
  203. GET /_search
  204. {
  205. "aggs" : {
  206. "my_buckets": {
  207. "composite" : {
  208. "sources" : [
  209. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
  210. { "product": { "terms": {"field": "product" } } }
  211. ]
  212. }
  213. }
  214. }
  215. }
  216. --------------------------------------------------
  217. // CONSOLE
  218. This will create composite buckets from the values created by two values source, a `date_histogram` and a `terms`.
  219. Each bucket is composed of two values, one for each value source defined in the aggregation.
  220. Any type of combinations is allowed and the order in the array is preserved
  221. in the composite buckets.
  222. [source,js]
  223. --------------------------------------------------
  224. GET /_search
  225. {
  226. "aggs" : {
  227. "my_buckets": {
  228. "composite" : {
  229. "sources" : [
  230. { "shop": { "terms": {"field": "shop" } } },
  231. { "product": { "terms": { "field": "product" } } },
  232. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } }
  233. ]
  234. }
  235. }
  236. }
  237. }
  238. --------------------------------------------------
  239. // CONSOLE
  240. ==== Order
  241. By default the composite buckets are sorted by their natural ordering. Values are sorted
  242. in ascending order of their values. When multiple value sources are requested, the ordering is done per value
  243. source, the first value of the composite bucket is compared to the first value of the other composite bucket and if they are equals the
  244. next values in the composite bucket are used for tie-breaking. This means that the composite bucket
  245. `[foo, 100]` is considered smaller than `[foobar, 0]` because `foo` is considered smaller than `foobar`.
  246. It is possible to define the direction of the sort for each value source by setting `order` to `asc` (default value)
  247. or `desc` (descending order) directly in the value source definition.
  248. For example:
  249. [source,js]
  250. --------------------------------------------------
  251. GET /_search
  252. {
  253. "aggs" : {
  254. "my_buckets": {
  255. "composite" : {
  256. "sources" : [
  257. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  258. { "product": { "terms": {"field": "product", "order": "asc" } } }
  259. ]
  260. }
  261. }
  262. }
  263. }
  264. --------------------------------------------------
  265. // CONSOLE
  266. \... will sort the composite bucket in descending order when comparing values from the `date_histogram` source
  267. and in ascending order when comparing values from the `terms` source.
  268. ==== Size
  269. The `size` parameter can be set to define how many composite buckets should be returned.
  270. Each composite bucket is considered as a single bucket so setting a size of 10 will return the
  271. first 1O composite buckets created from the values source.
  272. The response contains the values for each composite bucket in an array containing the values extracted
  273. from each value source.
  274. ==== After
  275. If the number of composite buckets is too high (or unknown) to be returned in a single response
  276. it is possible to split the retrieval in multiple requests.
  277. Since the composite buckets are flat by nature, the requested `size` is exactly the number of composite buckets
  278. that will be returned in the response (assuming that they are at least `size` composite buckets to return).
  279. If all composite buckets should be retrieved it is preferable to use a small size (`100` or `1000` for instance)
  280. and then use the `after` parameter to retrieve the next results.
  281. For example:
  282. [source,js]
  283. --------------------------------------------------
  284. GET /_search
  285. {
  286. "aggs" : {
  287. "my_buckets": {
  288. "composite" : {
  289. "size": 2,
  290. "sources" : [
  291. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
  292. { "product": { "terms": {"field": "product" } } }
  293. ]
  294. }
  295. }
  296. }
  297. }
  298. --------------------------------------------------
  299. // CONSOLE
  300. // TEST[s/_search/_search\?filter_path=aggregations/]
  301. \... returns:
  302. [source,js]
  303. --------------------------------------------------
  304. {
  305. ...
  306. "aggregations": {
  307. "my_buckets": {
  308. "buckets": [
  309. {
  310. "key": {
  311. "date": 1494201600000,
  312. "product": "rocky"
  313. },
  314. "doc_count": 1
  315. },
  316. {
  317. "key": { <1>
  318. "date": 1494288000000,
  319. "product": "mad max"
  320. },
  321. "doc_count": 2
  322. }
  323. ]
  324. }
  325. }
  326. }
  327. --------------------------------------------------
  328. // TESTRESPONSE[s/\.\.\.//]
  329. <1> The last composite bucket returned by the query.
  330. The `after` parameter can be used to retrieve the composite buckets that are **after**
  331. the last composite buckets returned in a previous round.
  332. For the example below the last bucket is `"key": [1494288000000, "mad max"]` so the next
  333. round of result can be retrieved with:
  334. [source,js]
  335. --------------------------------------------------
  336. GET /_search
  337. {
  338. "aggs" : {
  339. "my_buckets": {
  340. "composite" : {
  341. "size": 2,
  342. "sources" : [
  343. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  344. { "product": { "terms": {"field": "product", "order": "asc" } } }
  345. ],
  346. "after": { "date": 1494288000000, "product": "mad max" } <1>
  347. }
  348. }
  349. }
  350. }
  351. --------------------------------------------------
  352. // CONSOLE
  353. <1> Should restrict the aggregation to buckets that sort **after** the provided values.
  354. ==== Sub-aggregations
  355. Like any `multi-bucket` aggregations the `composite` aggregation can hold sub-aggregations.
  356. These sub-aggregations can be used to compute other buckets or statistics on each composite bucket created by this
  357. parent aggregation.
  358. For instance the following example computes the average value of a field
  359. per composite bucket:
  360. [source,js]
  361. --------------------------------------------------
  362. GET /_search
  363. {
  364. "aggs" : {
  365. "my_buckets": {
  366. "composite" : {
  367. "sources" : [
  368. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  369. { "product": { "terms": {"field": "product" } } }
  370. ]
  371. },
  372. "aggregations": {
  373. "the_avg": {
  374. "avg": { "field": "price" }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. --------------------------------------------------
  381. // CONSOLE
  382. // TEST[s/_search/_search\?filter_path=aggregations/]
  383. \... returns:
  384. [source,js]
  385. --------------------------------------------------
  386. {
  387. ...
  388. "aggregations": {
  389. "my_buckets": {
  390. "buckets": [
  391. {
  392. "key": {
  393. "date": 1494460800000,
  394. "product": "apocalypse now"
  395. },
  396. "doc_count": 1,
  397. "the_avg": {
  398. "value": 10.0
  399. }
  400. },
  401. {
  402. "key": {
  403. "date": 1494374400000,
  404. "product": "mad max"
  405. },
  406. "doc_count": 1,
  407. "the_avg": {
  408. "value": 27.0
  409. }
  410. },
  411. {
  412. "key": {
  413. "date": 1494288000000,
  414. "product" : "mad max"
  415. },
  416. "doc_count": 2,
  417. "the_avg": {
  418. "value": 22.5
  419. }
  420. },
  421. {
  422. "key": {
  423. "date": 1494201600000,
  424. "product": "rocky"
  425. },
  426. "doc_count": 1,
  427. "the_avg": {
  428. "value": 10.0
  429. }
  430. }
  431. ]
  432. }
  433. }
  434. }
  435. --------------------------------------------------
  436. // TESTRESPONSE[s/\.\.\.//]
  437. ==== Index sorting
  438. By default this aggregation runs on every document that match the query.
  439. Though if the index sort matches the composite sort this aggregation can optimize
  440. the execution and can skip documents that contain composite buckets that would not
  441. be part of the response.
  442. For instance the following aggregations:
  443. [source,js]
  444. --------------------------------------------------
  445. GET /_search
  446. {
  447. "aggs" : {
  448. "my_buckets": {
  449. "composite" : {
  450. "size": 2,
  451. "sources" : [
  452. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "asc" } } },
  453. { "product": { "terms": { "field": "product", "order": "asc" } } }
  454. ]
  455. }
  456. }
  457. }
  458. }
  459. --------------------------------------------------
  460. // CONSOLE
  461. \... is much faster on an index that uses the following sort:
  462. [source,js]
  463. --------------------------------------------------
  464. PUT twitter
  465. {
  466. "settings" : {
  467. "index" : {
  468. "sort.field" : ["timestamp", "product"],
  469. "sort.order" : ["asc", "asc"]
  470. }
  471. },
  472. "mappings": {
  473. "sales": {
  474. "properties": {
  475. "timestamp": {
  476. "type": "date"
  477. },
  478. "product": {
  479. "type": "keyword"
  480. }
  481. }
  482. }
  483. }
  484. }
  485. --------------------------------------------------
  486. // CONSOLE
  487. WARNING: The optimization takes effect only if the fields used for sorting are single-valued and follow
  488. the same order as the aggregation (`desc` or `asc`).
  489. If only the aggregation results are needed it is also better to set the size of the query to 0
  490. and `track_total_hits` to false in order to remove other slowing factors:
  491. [source,js]
  492. --------------------------------------------------
  493. GET /_search
  494. {
  495. "size": 0,
  496. "track_total_hits": false,
  497. "aggs" : {
  498. "my_buckets": {
  499. "composite" : {
  500. "size": 2,
  501. "sources" : [
  502. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
  503. { "product": { "terms": { "field": "product" } } }
  504. ]
  505. }
  506. }
  507. }
  508. }
  509. --------------------------------------------------
  510. // CONSOLE
  511. See <<index-modules-index-sorting, index sorting>> for more details.