Elasticsearch Elasticsearch Aggregation on Multiple Fields: A Comprehensive Guide

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Before you dig into the details of this technical guide, have you tried asking OpsGPT?

You'll receive concise answers that will help streamline your Elasticsearch/OpenSearch operations.


Try OpsGPT now for step-by-step guidance and tailored insights into your Elasticsearch/ OpenSearch operation.

Before you dig into the details of this guide, have you tried asking OpsGPT? You’ll receive concise answers that will help streamline your Elasticsearch/OpenSearch operations.

Try OpsGPT now for step-by-step guidance and tailored insights into your search operation.

You can also try for free our full platform: AutoOps for Elasticsearch. It will prevent issues automatically and perform advanced optimizations to keep your search operation running smoothly. Try AutoOps for free.

Introduction

Aggregations in Elasticsearch provide a powerful mechanism to group and summarize data based on specific criteria. They allow you to extract valuable insights from your data and can be used for various purposes, such as analytics, reporting, and visualization. In this article, we will explore how to perform aggregations on multiple fields in Elasticsearch, including different types of aggregations and how to combine them to achieve the desired results. If you want to learn about Elasticsearch aggregation, check out this guide.

1. Bucketing Aggregations on Multiple Fields

Bucketing aggregations are used to group documents into buckets based on certain criteria. To perform bucketing aggregations on multiple fields, you can use the following techniques:

a. Nested Terms Aggregations

You can nest multiple aggregations to create a hierarchical structure of buckets. For example, if you want to group documents by the “category” field and then by the “brand” field, you can use the following request:

GET /_search
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": {
        "field": "category"
      },
      "aggs": {
        "by_brand": {
          "terms": {
            "field": "brand"
          }
        }
      }
    }
  }
}

b. Multi Terms Aggregations

You can leverage the multi_terms bucket aggregation in order to create one buckets per combination of fields value. For example, if you want to group documents by the “category” and “brand” fields, you can use the following request:

GET /_search
{
  "size": 0,
  "aggs": {
    "category_and_brand": {
      "multi_terms": {
        "terms": [
          {“field”: “category”},
          {“field”: “brand”}
        ]
      }
    }
  }
}

c. Composite Aggregations

Composite aggregations allow you to group documents by multiple fields in a single request. This is useful when you need to paginate through large result sets. Here’s an example of using composite aggregations to group documents by the “category” and “brand” fields:

GET /_search
{
  "size": 0,
  "aggs": {
    "by_category_and_brand": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "category": {
              "terms": {
                "field": "category"
              }
            }
          },
          {
            "brand": {
              "terms": {
                "field": "brand"
              }
            }
          }
        ]
      }
    }
  }
}

2. Metric Aggregations on Multiple Fields

Metric aggregations are used to compute summary statistics for numeric fields. To perform metric aggregations on multiple fields, you can use the following techniques:

a. Multi-Field Metric Aggregations

You can compute multiple metric aggregations in a single request by specifying them in the “aggs” section. For example, if you want to calculate the average price and the total quantity of products in each category, you can use the following request:

GET /_search
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": {
        "field": "category"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        },
        "total_quantity": {
          "sum": {
            "field": "quantity"
          }
        }
      }
    }
  }
}

b. Extended Stats Aggregations

Extended stats aggregations allow you to compute multiple summary statistics for a single field in one request. For example, if you want to calculate the average, minimum, maximum, and standard deviation of the “price” field, you can use the following request:

GET /_search
{
  "size": 0,
  "aggs": {
    "price_stats": {
      "extended_stats": {
        "field": "price"
      }
    }
  }
}

Conclusion

In conclusion, Elasticsearch aggregations provide a flexible and powerful way to perform complex data analysis on multiple fields. By combining different types of aggregations and nesting them, you can extract valuable insights from your data and use them for various purposes, such as analytics, reporting, and visualization.

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?


Get expert answers on Elasticsearch/OpenSearch