Elasticsearch Elasticsearch Group By Field: Aggregations and Bucketing Techniques

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction 

Aggregations in Elasticsearch provide a powerful way to analyze and summarize your data. One common use case is to group documents by a specific field, similar to the SQL “GROUP BY” clause. In this article, we will explore different techniques to group documents by a field using Elasticsearch aggregations and bucketing. If you want to learn about aggregations in general, check out this guide.

Techniques to group documents by a field using Elasticsearch aggregations and bucketing

1. Terms Aggregation

Terms aggregation is the most common way to group documents by a field. It creates a bucket for each unique value of the specified field and calculates the document count for each bucket.

Example: Group documents by the “category” field.

GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "group_by_category": {
      "terms": {
        "field": "category.keyword"
      }
    }
  }
}

In this example, we use the “terms” aggregation to group documents by the “category” field. Note that we use the “category.keyword” field, which is a non-analyzed version of the “category” field. This is important because the “terms” aggregation requires a non-analyzed or keyword field.

2. Histogram Aggregation

Histogram aggregation is useful when you want to group documents by a numeric field with fixed interval buckets. It creates buckets based on the specified interval and calculates the document count for each bucket.

Example: Group documents by the “price” field with an interval of 10.

GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "group_by_price": {
      "histogram": {
        "field": "price",
        "interval": 10
      }
    }
  }
}

In this example, we use the “histogram” aggregation to group documents by the “price” field with an interval of 10. The result will show the document count for each price range, such as 0-10, 10-20, 20-30, and so on.

3. Date Histogram Aggregation

Date histogram aggregation is similar to the histogram aggregation but specifically designed for date fields. It groups documents by a specified date interval, such as hourly, daily, or monthly.

Example: Group documents by the “timestamp” field with a daily interval.

  "size": 0,
  "aggs": {
    "group_by_date": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "day"
      }
    }
  }
}

In this example, we use the “date_histogram” aggregation to group documents by the “timestamp” field with a daily interval. The result will show the document count for each day.

4. Composite Aggregation

Composite aggregation allows you to group documents by multiple fields or create a multi-level grouping. It creates buckets based on the specified sources, which can be a combination of terms, histograms, or date histograms among others.

Example: Group documents by the “category” field and the “price” field with an interval of 10.

GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "group_by_category_and_price": {
      "composite": {
        "sources": [
          {
            "category": {
              "terms": {
                "field": "category.keyword"
              }
            }
          },
          {
            "price": {
              "histogram": {
                "field": "price",
                "interval": 10
              }
            }
          }
        ]
      }
    }
  }
}

In this example, we use the “composite” aggregation to group documents by the “category” field and the “price” field with an interval of 10. The result will show the document count for each combination of category and price range.

Conclusion 

In conclusion, Elasticsearch provides various aggregation and bucketing techniques to group documents by a specific field or multiple fields. By using terms, histograms, date histograms, and composite aggregations, you can analyze and summarize your data effectively. Remember to choose the appropriate aggregation type based on your use case and the nature of the field you want to group by.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?