![]() |
Elasticsearch is a robust tool not only for full-text search but also for data analytics. One of the core features that make Elasticsearch powerful is its aggregation framework, particularly bucket aggregations. Bucket aggregations allow you to group documents into buckets based on certain criteria, making it easier to analyze and summarize your data. This article will explain what bucket aggregations are, how they work, and provide detailed examples to help you understand their usage. What are Bucket Aggregations?Bucket aggregations in Elasticsearch are used to group documents into different buckets based on specified criteria. Each bucket can contain multiple documents that match the criteria. Unlike metric aggregations, which calculate metrics on numeric fields, bucket aggregations focus on grouping data. Bucket aggregations can be combined with metric aggregations to perform complex analytics. For instance, you can group documents by a field (like category) and then calculate the average price within each group. Types of Bucket AggregationsElasticsearch provides several types of bucket aggregations, each suited for different grouping scenarios:
Example DatasetTo illustrate bucket aggregations, let’s consider an Elasticsearch index called products with documents like this: { Terms AggregationThe term aggregation groups documents based on the unique values in a field. Let’s group products by their category. Query GET /products/_search Output { In this example, products are grouped by category, and the number of products in each category is counted. Histogram AggregationThe histogram aggregation groups numeric values into buckets of a specified interval. Let’s group products by price ranges with an interval of $100. Query: GET /products/_search Output: { In this example, products are grouped into price ranges with an interval of $100, and the number of products in each range is counted. Date Histogram AggregationThe date histogram aggregation groups date values into buckets of a fixed time interval. Let’s group products by the month they were sold. Query: GET /products/_search Output: { In this example, products are grouped by the month they were sold, and the number of products sold each month is counted. Range AggregationThe range aggregation groups numeric values into custom ranges. Let’s group products by custom price ranges. Query: GET /products/_search Output: { In this example, products are grouped into custom price ranges, and the number of products in each range is counted. Date Range AggregationThe date range aggregation groups date values into custom ranges. Let’s group products by custom-sold date ranges. Query GET /products/_search Output { In this example, products are grouped into custom-sold date ranges, and the number of products sold in each range is counted. Filter AggregationThe filter aggregation groups documents that match a specific filter. Let’s group products that have a rating of 4 or higher. Query GET /products/_search Output { In this example, we group products with a rating of 4 or higher, and the number of such products is counted. Filters AggregationThe filter aggregation groups documents based on multiple filters. Let’s group products based on multiple rating ranges. Query GET /products/_search Output { In this example, products are grouped based on two rating ranges, and the number of products in each range is counted. Significant Terms AggregationThe significant terms aggregation finds unusual terms in a set of documents. Let’s find significant terms in product names in the electronics category. Query GET /products/_search Output { In this example, significant terms in product names in the electronics category are identified, with their document counts and significance scores. Geohash Grid AggregationThe geohash grid aggregation groups geo-point data into geohash cells. Let’s group products by their location. Query GET /products/_search Output { In this example, products are grouped by their location into geohash cells with a precision of 5. ConclusionBucket aggregations in Elasticsearch are a powerful tool for grouping and analyzing data based on various criteria. By understanding and using different types of bucket aggregations, you can perform complex analytics and gain valuable insights into your data. Whether you’re analyzing sales data, user behavior, or any other type of information, bucket aggregations provide a flexible and efficient way to summarize and explore your data in Elasticsearch. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |