Search⌘ K
AI Features

Solution: Aggregation

Explore how to perform and combine various Elasticsearch aggregations to analyze data efficiently. Learn to create terms, average, histogram, and nested aggregations to extract insights from datasets. Understand how to structure queries for diverse aggregation needs like runtime distribution, rating breakdowns, and budget analysis.

Aggregation 1: Average lifetime gross by studio

To solve this challenge, we can use the terms aggregation on the studio attribute combined with the avg aggregation lifetime_gross.

To implement this aggregation, we will initiate a _search request targeting the movie index, and within the request body, we will define the aggregation structure. Initially, we will specify aggs as terms and designate the field as studio. Nested within this aggregation, we will include another aggs section labeled as avg, where we will specify the field as lifetime_gross.

Dart
GET /movie/_search
{
"size": 0,
"aggs": {
"average_lifetime_gross_by_studio": {
"terms": {
"field": "studio"
},
"aggs": {
"avg_lifetime_gross": {
"avg": {
"field": "lifetime_gross"
}
}
}
}
}
}

Aggregation 2: Number of movies released each year

To solve this challenge, we can employ the terms aggregation on the Year field to count the number of movies released each year.

To implement this aggregation, we will initiate a _search request targeting the movie index. Within the request ...