composite-aggregation.asciidoc 19 KB

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