Chapter 26. Aggregation Test-Drive
We could spend the next few pages defining the various aggregations and their syntax, but aggregations are truly best learned by example. Once you learn how to think about aggregations, and how to nest them appropriately, the syntax is fairly trivial.
Note
A complete list of aggregation buckets and metrics can be found at the online reference documentation. We’ll cover many of them in this chapter, but glance over it after finishing so you are familiar with the full range of capabilities.
So let’s just dive in and start with an example. We are going to build some aggregations that might be useful to a car dealer. Our data will be about car transactions: the car model, manufacturer, sale price, when it sold, and more.
First we will bulk-index some data to work with:
POST/cars/transactions/_bulk{"index":{}}{"price":10000,"color":"red","make":"honda","sold":"2014-10-28"}{"index":{}}{"price":20000,"color":"red","make":"honda","sold":"2014-11-05"}{"index":{}}{"price":30000,"color":"green","make":"ford","sold":"2014-05-18"}{"index":{}}{"price":15000,"color":"blue","make":"toyota","sold":"2014-07-02"}{"index":{}}{"price":12000,"color":"green","make":"toyota","sold":"2014-08-19"}{"index":{}}{"price":20000,"color":"red","make":"honda","sold":"2014-11-05"}{"index":{}}{"price":80000,"color":"red","make":"bmw","sold":"2014-01-01"}{
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access