composite-aggregation.asciidoc 19 KB

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