What is a composite aggregation in Elasticsearch?

A composite aggregation in Elasticsearch is a way to paginate and aggregate large result sets efficiently. It allows you to group documents based on one or more fields, and then paginate through the groups to retrieve the complete set of results.

When you perform a composite aggregation, you specify one or more fields to group the documents by, and a size parameter that determines the maximum number of groups returned in each page of results. Elasticsearch then returns the first page of results, along with a composite key that represents the last group in the page. You can use this key to request the next page of results, and continue until all groups have been returned.

In addition to grouping and paginating the results, composite aggregations also allow you to perform sub-aggregations on each group. This allows you to calculate aggregate statistics, histograms, or other metrics within each group, and obtain a complete set of results with a single request.

For example, let’s say you have an index of customer orders, and each document has a “product” field that represents the product ordered, and a “timestamp” field that represents the date and time the order was placed. You could perform a composite aggregation on the “product” field and a date histogram aggregation on the “timestamp” field to group the orders by product and day. Elasticsearch would then return the first page of results, along with a composite key representing the last group in the page. You could use this key to request the next page of results, and continue until all groups have been returned.

Composite aggregations can be useful for a wide range of applications, such as analyzing large datasets, monitoring system performance, or tracking user activity. They allow you to efficiently paginate through large result sets, while also performing sub-aggregations to obtain detailed metrics within each group.