composite-aggregation.asciidoc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. [[search-aggregations-bucket-composite-aggregation]]
  2. === Composite aggregation
  3. ++++
  4. <titleabbrev>Composite</titleabbrev>
  5. ++++
  6. A multi-bucket aggregation that creates composite buckets from different sources.
  7. Unlike the other `multi-bucket` aggregations, you can use the `composite`
  8. aggregation to paginate **all** buckets from a multi-level aggregation
  9. efficiently. This aggregation provides a way to stream **all** buckets of a
  10. specific aggregation, similar to what
  11. <<scroll-search-results, scroll>> does for documents.
  12. The composite buckets are built from the combinations of the
  13. values extracted/created for each document and each combination is considered as
  14. a composite bucket.
  15. //////////////////////////
  16. [source,js]
  17. --------------------------------------------------
  18. PUT /sales
  19. {
  20. "mappings": {
  21. "properties": {
  22. "product": {
  23. "type": "keyword"
  24. },
  25. "timestamp": {
  26. "type": "date"
  27. },
  28. "price": {
  29. "type": "long"
  30. },
  31. "shop": {
  32. "type": "keyword"
  33. },
  34. "location": {
  35. "type": "geo_point"
  36. },
  37. "nested": {
  38. "type": "nested",
  39. "properties": {
  40. "product": {
  41. "type": "keyword"
  42. },
  43. "timestamp": {
  44. "type": "date"
  45. },
  46. "price": {
  47. "type": "long"
  48. },
  49. "shop": {
  50. "type": "keyword"
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. POST /sales/_bulk?refresh
  58. {"index":{"_id":0}}
  59. {"product": "mad max", "price": "20", "timestamp": "2017-05-09T14:35"}
  60. {"index":{"_id":1}}
  61. {"product": "mad max", "price": "25", "timestamp": "2017-05-09T12:35"}
  62. {"index":{"_id":2}}
  63. {"product": "rocky", "price": "10", "timestamp": "2017-05-08T09:10"}
  64. {"index":{"_id":3}}
  65. {"product": "mad max", "price": "27", "timestamp": "2017-05-10T07:07"}
  66. {"index":{"_id":4}}
  67. {"product": "apocalypse now", "price": "10", "timestamp": "2017-05-11T08:35"}
  68. -------------------------------------------------
  69. // NOTCONSOLE
  70. // TESTSETUP
  71. //////////////////////////
  72. For example, consider the following document:
  73. [source,js]
  74. --------------------------------------------------
  75. {
  76. "keyword": ["foo", "bar"],
  77. "number": [23, 65, 76]
  78. }
  79. --------------------------------------------------
  80. // NOTCONSOLE
  81. Using `keyword` and `number` as source fields for the aggregation results in
  82. the following composite buckets:
  83. [source,js]
  84. --------------------------------------------------
  85. { "keyword": "foo", "number": 23 }
  86. { "keyword": "foo", "number": 65 }
  87. { "keyword": "foo", "number": 76 }
  88. { "keyword": "bar", "number": 23 }
  89. { "keyword": "bar", "number": 65 }
  90. { "keyword": "bar", "number": 76 }
  91. --------------------------------------------------
  92. // NOTCONSOLE
  93. ==== Value sources
  94. The `sources` parameter defines the source fields to use when building
  95. composite buckets. The order that the `sources` are defined controls the order
  96. that the keys are returned.
  97. NOTE: You must use a unique name when defining `sources`.
  98. The `sources` parameter can be any of the following types:
  99. * <<_terms,Terms>>
  100. * <<_histogram,Histogram>>
  101. * <<_date_histogram,Date histogram>>
  102. * <<_geotile_grid,GeoTile grid>>
  103. [[_terms]]
  104. ===== Terms
  105. The `terms` value source is equivalent to a simple `terms` aggregation.
  106. The values are extracted from a field or a script exactly like the `terms` aggregation.
  107. Example:
  108. [source,console,id=composite-aggregation-terms-field-example]
  109. --------------------------------------------------
  110. GET /_search
  111. {
  112. "size": 0,
  113. "aggs": {
  114. "my_buckets": {
  115. "composite": {
  116. "sources": [
  117. { "product": { "terms": { "field": "product" } } }
  118. ]
  119. }
  120. }
  121. }
  122. }
  123. --------------------------------------------------
  124. Like the `terms` aggregation it is also possible to use a script to create the values for the composite buckets:
  125. [source,console,id=composite-aggregation-terms-script-example]
  126. --------------------------------------------------
  127. GET /_search
  128. {
  129. "size": 0,
  130. "aggs": {
  131. "my_buckets": {
  132. "composite": {
  133. "sources": [
  134. {
  135. "product": {
  136. "terms": {
  137. "script": {
  138. "source": "doc['product'].value",
  139. "lang": "painless"
  140. }
  141. }
  142. }
  143. }
  144. ]
  145. }
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. [[_histogram]]
  151. ===== Histogram
  152. The `histogram` value source can be applied on numeric values to build fixed size
  153. interval over the values. The `interval` parameter defines how the numeric values should be
  154. transformed. For instance an `interval` set to 5 will translate any numeric values to its closest interval,
  155. a value of `101` would be translated to `100` which is the key for the interval between 100 and 105.
  156. Example:
  157. [source,console,id=composite-aggregation-histogram-field-example]
  158. --------------------------------------------------
  159. GET /_search
  160. {
  161. "size": 0,
  162. "aggs": {
  163. "my_buckets": {
  164. "composite": {
  165. "sources": [
  166. { "histo": { "histogram": { "field": "price", "interval": 5 } } }
  167. ]
  168. }
  169. }
  170. }
  171. }
  172. --------------------------------------------------
  173. The values are built from a numeric field or a script that return numerical values:
  174. [source,console,id=composite-aggregation-histogram-script-example]
  175. --------------------------------------------------
  176. GET /_search
  177. {
  178. "size": 0,
  179. "aggs": {
  180. "my_buckets": {
  181. "composite": {
  182. "sources": [
  183. {
  184. "histo": {
  185. "histogram": {
  186. "interval": 5,
  187. "script": {
  188. "source": "doc['price'].value",
  189. "lang": "painless"
  190. }
  191. }
  192. }
  193. }
  194. ]
  195. }
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. [[_date_histogram]]
  201. ===== Date histogram
  202. The `date_histogram` is similar to the `histogram` value source except that the interval
  203. is specified by date/time expression:
  204. [source,console,id=composite-aggregation-datehistogram-example]
  205. --------------------------------------------------
  206. GET /_search
  207. {
  208. "size": 0,
  209. "aggs": {
  210. "my_buckets": {
  211. "composite": {
  212. "sources": [
  213. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } }
  214. ]
  215. }
  216. }
  217. }
  218. }
  219. --------------------------------------------------
  220. The example above creates an interval per day and translates all `timestamp` values to the start of its closest intervals.
  221. Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
  222. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  223. Note that fractional time values are not supported, but you can address this by shifting to another
  224. time unit (e.g., `1.5h` could instead be specified as `90m`).
  225. *Format*
  226. Internally, a date is represented as a 64 bit number representing a timestamp in milliseconds-since-the-epoch.
  227. These timestamps are returned as the bucket keys. It is possible to return a formatted date string instead using
  228. the format specified with the format parameter:
  229. [source,console,id=composite-aggregation-datehistogram-format-example]
  230. --------------------------------------------------
  231. GET /_search
  232. {
  233. "size": 0,
  234. "aggs": {
  235. "my_buckets": {
  236. "composite": {
  237. "sources": [
  238. {
  239. "date": {
  240. "date_histogram": {
  241. "field": "timestamp",
  242. "calendar_interval": "1d",
  243. "format": "yyyy-MM-dd" <1>
  244. }
  245. }
  246. }
  247. ]
  248. }
  249. }
  250. }
  251. }
  252. --------------------------------------------------
  253. <1> Supports expressive date <<date-format-pattern,format pattern>>
  254. *Time Zone*
  255. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  256. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  257. that bucketing should use a different time zone.
  258. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  259. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  260. `America/Los_Angeles`.
  261. *Offset*
  262. include::datehistogram-aggregation.asciidoc[tag=offset-explanation]
  263. [source,console,id=composite-aggregation-datehistogram-offset-example]
  264. ----
  265. PUT my-index-000001/_doc/1?refresh
  266. {
  267. "date": "2015-10-01T05:30:00Z"
  268. }
  269. PUT my-index-000001/_doc/2?refresh
  270. {
  271. "date": "2015-10-01T06:30:00Z"
  272. }
  273. GET my-index-000001/_search?size=0
  274. {
  275. "aggs": {
  276. "my_buckets": {
  277. "composite" : {
  278. "sources" : [
  279. {
  280. "date": {
  281. "date_histogram" : {
  282. "field": "date",
  283. "calendar_interval": "day",
  284. "offset": "+6h",
  285. "format": "iso8601"
  286. }
  287. }
  288. }
  289. ]
  290. }
  291. }
  292. }
  293. }
  294. ----
  295. include::datehistogram-aggregation.asciidoc[tag=offset-result-intro]
  296. [source,console-result]
  297. ----
  298. {
  299. ...
  300. "aggregations": {
  301. "my_buckets": {
  302. "after_key": { "date": "2015-10-01T06:00:00.000Z" },
  303. "buckets": [
  304. {
  305. "key": { "date": "2015-09-30T06:00:00.000Z" },
  306. "doc_count": 1
  307. },
  308. {
  309. "key": { "date": "2015-10-01T06:00:00.000Z" },
  310. "doc_count": 1
  311. }
  312. ]
  313. }
  314. }
  315. }
  316. ----
  317. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  318. include::datehistogram-aggregation.asciidoc[tag=offset-note]
  319. [[_geotile_grid]]
  320. ===== GeoTile grid
  321. The `geotile_grid` value source works on `geo_point` fields and groups points into buckets that represent
  322. cells in a grid. The resulting grid can be sparse and only contains cells
  323. that have matching data. Each cell corresponds to a
  324. {wikipedia}/Tiled_web_map[map tile] as used by many online map
  325. sites. Each cell is labeled using a "{zoom}/{x}/{y}" format, where zoom is equal
  326. to the user-specified precision.
  327. [source,console,id=composite-aggregation-geotilegrid-example]
  328. --------------------------------------------------
  329. GET /_search
  330. {
  331. "size": 0,
  332. "aggs": {
  333. "my_buckets": {
  334. "composite": {
  335. "sources": [
  336. { "tile": { "geotile_grid": { "field": "location", "precision": 8 } } }
  337. ]
  338. }
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. *Precision*
  344. The highest-precision geotile of length 29 produces cells that cover
  345. less than 10cm by 10cm of land. This precision is uniquely suited for composite aggregations as each
  346. tile does not have to be generated and loaded in memory.
  347. See https://wiki.openstreetmap.org/wiki/Zoom_levels[Zoom level documentation]
  348. on how precision (zoom) correlates to size on the ground. Precision for this
  349. aggregation can be between 0 and 29, inclusive.
  350. *Bounding box filtering*
  351. The geotile source can optionally be constrained to a specific geo bounding box, which reduces
  352. the range of tiles used. These bounds are useful when only a specific part of a geographical area needs high
  353. precision tiling.
  354. [source,console,id=composite-aggregation-geotilegrid-boundingbox-example]
  355. --------------------------------------------------
  356. GET /_search
  357. {
  358. "size": 0,
  359. "aggs": {
  360. "my_buckets": {
  361. "composite": {
  362. "sources": [
  363. {
  364. "tile": {
  365. "geotile_grid": {
  366. "field": "location",
  367. "precision": 22,
  368. "bounds": {
  369. "top_left": "52.4, 4.9",
  370. "bottom_right": "52.3, 5.0"
  371. }
  372. }
  373. }
  374. }
  375. ]
  376. }
  377. }
  378. }
  379. }
  380. --------------------------------------------------
  381. ===== Mixing different value sources
  382. The `sources` parameter accepts an array of value sources.
  383. It is possible to mix different value sources to create composite buckets.
  384. For example:
  385. [source,console,id=composite-aggregation-mixing-sources-example]
  386. --------------------------------------------------
  387. GET /_search
  388. {
  389. "size": 0,
  390. "aggs": {
  391. "my_buckets": {
  392. "composite": {
  393. "sources": [
  394. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } },
  395. { "product": { "terms": { "field": "product" } } }
  396. ]
  397. }
  398. }
  399. }
  400. }
  401. --------------------------------------------------
  402. This will create composite buckets from the values created by two value sources, a `date_histogram` and a `terms`.
  403. Each bucket is composed of two values, one for each value source defined in the aggregation.
  404. Any type of combinations is allowed and the order in the array is preserved
  405. in the composite buckets.
  406. [source,console,id=composite-aggregation-mixing-three-sources-example]
  407. --------------------------------------------------
  408. GET /_search
  409. {
  410. "size": 0,
  411. "aggs": {
  412. "my_buckets": {
  413. "composite": {
  414. "sources": [
  415. { "shop": { "terms": { "field": "shop" } } },
  416. { "product": { "terms": { "field": "product" } } },
  417. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } }
  418. ]
  419. }
  420. }
  421. }
  422. }
  423. --------------------------------------------------
  424. ==== Order
  425. By default the composite buckets are sorted by their natural ordering. Values are sorted
  426. in ascending order of their values. When multiple value sources are requested, the ordering is done per value
  427. 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
  428. next values in the composite bucket are used for tie-breaking. This means that the composite bucket
  429. `[foo, 100]` is considered smaller than `[foobar, 0]` because `foo` is considered smaller than `foobar`.
  430. It is possible to define the direction of the sort for each value source by setting `order` to `asc` (default value)
  431. or `desc` (descending order) directly in the value source definition.
  432. For example:
  433. [source,console,id=composite-aggregation-order-example]
  434. --------------------------------------------------
  435. GET /_search
  436. {
  437. "size": 0,
  438. "aggs": {
  439. "my_buckets": {
  440. "composite": {
  441. "sources": [
  442. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  443. { "product": { "terms": { "field": "product", "order": "asc" } } }
  444. ]
  445. }
  446. }
  447. }
  448. }
  449. --------------------------------------------------
  450. \... will sort the composite bucket in descending order when comparing values from the `date_histogram` source
  451. and in ascending order when comparing values from the `terms` source.
  452. ==== Missing bucket
  453. By default documents without a value for a given source are ignored.
  454. It is possible to include them in the response by setting `missing_bucket` to
  455. `true` (defaults to `false`):
  456. [source,console,id=composite-aggregation-missing-bucket-example]
  457. --------------------------------------------------
  458. GET /_search
  459. {
  460. "size": 0,
  461. "aggs": {
  462. "my_buckets": {
  463. "composite": {
  464. "sources": [
  465. { "product_name": { "terms": { "field": "product", "missing_bucket": true } } }
  466. ]
  467. }
  468. }
  469. }
  470. }
  471. --------------------------------------------------
  472. In the example above the source `product_name` will emit an explicit `null` value
  473. for documents without a value for the field `product`.
  474. The `order` specified in the source dictates whether the `null` values should rank
  475. first (ascending order, `asc`) or last (descending order, `desc`).
  476. ==== Size
  477. The `size` parameter can be set to define how many composite buckets should be returned.
  478. Each composite bucket is considered as a single bucket, so setting a size of 10 will return the
  479. first 10 composite buckets created from the value sources.
  480. The response contains the values for each composite bucket in an array containing the values extracted
  481. from each value source. Defaults to `10`.
  482. ==== Pagination
  483. If the number of composite buckets is too high (or unknown) to be returned in a single response
  484. it is possible to split the retrieval in multiple requests.
  485. Since the composite buckets are flat by nature, the requested `size` is exactly the number of composite buckets
  486. that will be returned in the response (assuming that they are at least `size` composite buckets to return).
  487. If all composite buckets should be retrieved it is preferable to use a small size (`100` or `1000` for instance)
  488. and then use the `after` parameter to retrieve the next results.
  489. For example:
  490. [source,console,id=composite-aggregation-after-key-example]
  491. --------------------------------------------------
  492. GET /_search
  493. {
  494. "size": 0,
  495. "aggs": {
  496. "my_buckets": {
  497. "composite": {
  498. "size": 2,
  499. "sources": [
  500. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } },
  501. { "product": { "terms": { "field": "product" } } }
  502. ]
  503. }
  504. }
  505. }
  506. }
  507. --------------------------------------------------
  508. // TEST[s/_search/_search\?filter_path=aggregations/]
  509. \... returns:
  510. [source,console-result]
  511. --------------------------------------------------
  512. {
  513. ...
  514. "aggregations": {
  515. "my_buckets": {
  516. "after_key": {
  517. "date": 1494288000000,
  518. "product": "mad max"
  519. },
  520. "buckets": [
  521. {
  522. "key": {
  523. "date": 1494201600000,
  524. "product": "rocky"
  525. },
  526. "doc_count": 1
  527. },
  528. {
  529. "key": {
  530. "date": 1494288000000,
  531. "product": "mad max"
  532. },
  533. "doc_count": 2
  534. }
  535. ]
  536. }
  537. }
  538. }
  539. --------------------------------------------------
  540. // TESTRESPONSE[s/\.\.\.//]
  541. To get the next set of buckets, resend the same aggregation with the `after`
  542. parameter set to the `after_key` value returned in the response.
  543. For example, this request uses the `after_key` value provided in the previous response:
  544. [source,console,id=composite-aggregation-after-example]
  545. --------------------------------------------------
  546. GET /_search
  547. {
  548. "size": 0,
  549. "aggs": {
  550. "my_buckets": {
  551. "composite": {
  552. "size": 2,
  553. "sources": [
  554. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  555. { "product": { "terms": { "field": "product", "order": "asc" } } }
  556. ],
  557. "after": { "date": 1494288000000, "product": "mad max" } <1>
  558. }
  559. }
  560. }
  561. }
  562. --------------------------------------------------
  563. <1> Should restrict the aggregation to buckets that sort **after** the provided values.
  564. NOTE: The `after_key` is *usually* the key to the last bucket returned in
  565. the response, but that isn't guaranteed. Always use the returned `after_key` instead
  566. of derriving it from the buckets.
  567. ==== Early termination
  568. For optimal performance the <<index-modules-index-sorting,index sort>> should be set on the index so that it matches
  569. parts or fully the source order in the composite aggregation.
  570. For instance the following index sort:
  571. [source,console]
  572. --------------------------------------------------
  573. PUT my-index-000001
  574. {
  575. "settings": {
  576. "index": {
  577. "sort.field": [ "username", "timestamp" ], <1>
  578. "sort.order": [ "asc", "desc" ] <2>
  579. }
  580. },
  581. "mappings": {
  582. "properties": {
  583. "username": {
  584. "type": "keyword",
  585. "doc_values": true
  586. },
  587. "timestamp": {
  588. "type": "date"
  589. }
  590. }
  591. }
  592. }
  593. --------------------------------------------------
  594. <1> This index is sorted by `username` first then by `timestamp`.
  595. <2> ... in ascending order for the `username` field and in descending order for the `timestamp` field.
  596. .. could be used to optimize these composite aggregations:
  597. [source,console]
  598. --------------------------------------------------
  599. GET /_search
  600. {
  601. "size": 0,
  602. "aggs": {
  603. "my_buckets": {
  604. "composite": {
  605. "sources": [
  606. { "user_name": { "terms": { "field": "user_name" } } } <1>
  607. ]
  608. }
  609. }
  610. }
  611. }
  612. --------------------------------------------------
  613. <1> `user_name` is a prefix of the index sort and the order matches (`asc`).
  614. [source,console]
  615. --------------------------------------------------
  616. GET /_search
  617. {
  618. "size": 0,
  619. "aggs": {
  620. "my_buckets": {
  621. "composite": {
  622. "sources": [
  623. { "user_name": { "terms": { "field": "user_name" } } }, <1>
  624. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } } <2>
  625. ]
  626. }
  627. }
  628. }
  629. }
  630. --------------------------------------------------
  631. <1> `user_name` is a prefix of the index sort and the order matches (`asc`).
  632. <2> `timestamp` matches also the prefix and the order matches (`desc`).
  633. In order to optimize the early termination it is advised to set `track_total_hits` in the request
  634. to `false`. The number of total hits that match the request can be retrieved on the first request
  635. and it would be costly to compute this number on every page:
  636. [source,console]
  637. --------------------------------------------------
  638. GET /_search
  639. {
  640. "size": 0,
  641. "track_total_hits": false,
  642. "aggs": {
  643. "my_buckets": {
  644. "composite": {
  645. "sources": [
  646. { "user_name": { "terms": { "field": "user_name" } } },
  647. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } }
  648. ]
  649. }
  650. }
  651. }
  652. }
  653. --------------------------------------------------
  654. Note that the order of the source is important, in the example below switching the `user_name` with the `timestamp`
  655. would deactivate the sort optimization since this configuration wouldn't match the index sort specification.
  656. If the order of sources do not matter for your use case you can follow these simple guidelines:
  657. * Put the fields with the highest cardinality first.
  658. * Make sure that the order of the field matches the order of the index sort.
  659. * Put multi-valued fields last since they cannot be used for early termination.
  660. WARNING: <<index-modules-index-sorting,index sort>> can slowdown indexing, it is very important to test index sorting
  661. with your specific use case and dataset to ensure that it matches your requirement. If it doesn't note that `composite`
  662. aggregations will also try to early terminate on non-sorted indices if the query matches all document (`match_all` query).
  663. ==== Sub-aggregations
  664. Like any `multi-bucket` aggregations the `composite` aggregation can hold sub-aggregations.
  665. These sub-aggregations can be used to compute other buckets or statistics on each composite bucket created by this
  666. parent aggregation.
  667. For instance the following example computes the average value of a field
  668. per composite bucket:
  669. [source,console,id=composite-aggregation-subaggregations-example]
  670. --------------------------------------------------
  671. GET /_search
  672. {
  673. "size": 0,
  674. "aggs": {
  675. "my_buckets": {
  676. "composite": {
  677. "sources": [
  678. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  679. { "product": { "terms": { "field": "product" } } }
  680. ]
  681. },
  682. "aggregations": {
  683. "the_avg": {
  684. "avg": { "field": "price" }
  685. }
  686. }
  687. }
  688. }
  689. }
  690. --------------------------------------------------
  691. // TEST[s/_search/_search\?filter_path=aggregations/]
  692. \... returns:
  693. [source,console-result]
  694. --------------------------------------------------
  695. {
  696. ...
  697. "aggregations": {
  698. "my_buckets": {
  699. "after_key": {
  700. "date": 1494201600000,
  701. "product": "rocky"
  702. },
  703. "buckets": [
  704. {
  705. "key": {
  706. "date": 1494460800000,
  707. "product": "apocalypse now"
  708. },
  709. "doc_count": 1,
  710. "the_avg": {
  711. "value": 10.0
  712. }
  713. },
  714. {
  715. "key": {
  716. "date": 1494374400000,
  717. "product": "mad max"
  718. },
  719. "doc_count": 1,
  720. "the_avg": {
  721. "value": 27.0
  722. }
  723. },
  724. {
  725. "key": {
  726. "date": 1494288000000,
  727. "product": "mad max"
  728. },
  729. "doc_count": 2,
  730. "the_avg": {
  731. "value": 22.5
  732. }
  733. },
  734. {
  735. "key": {
  736. "date": 1494201600000,
  737. "product": "rocky"
  738. },
  739. "doc_count": 1,
  740. "the_avg": {
  741. "value": 10.0
  742. }
  743. }
  744. ]
  745. }
  746. }
  747. }
  748. --------------------------------------------------
  749. // TESTRESPONSE[s/\.\.\.//]
  750. ==== Pipeline aggregations
  751. The composite agg is not currently compatible with pipeline aggregations, nor does it make sense in most cases.
  752. E.g. due to the paging nature of composite aggs, a single logical partition (one day for example) might be spread
  753. over multiple pages. Since pipeline aggregations are purely post-processing on the final list of buckets,
  754. running something like a derivative on a composite page could lead to inaccurate results as it is only taking into
  755. account a "partial" result on that page.
  756. Pipeline aggs that are self contained to a single bucket (such as `bucket_selector`) might be supported in the future.