# Is it possible to calculate the number of results by grouping them by several criteria?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497
**Category:** Elasticsearch
**Created:** [April 28, 2022, 10:10am UTC](https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497 "2022-04-28T10:10:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Denys\_Lukash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/denys_lukash/32/102216_2.png) [@Denys\_Lukash](https://discuss.elastic.co/u/Denys_Lukash)
#### Post date: [April 28, 2022, 10:10am UTC](https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497/1 "2022-04-28T10:10:49Z")

</div>

Hi dear community.

I'm a new user of elastic and I'm having trouble building a query, I don't know if it's possible to do what I want. My task is to get the number of products in each category. The problem is that the grouping is done not by a single category id, but by a set of id's that belong to the one category.

Elastic document **Product** has indexed fields { /.../ "genericId": 123 /.../ }

From the database I get a list of categories, **Category\_1** , **Category\_2**. There are several **genericId** belong to each category.  
**Category\_1** has generics 123, 124, 125  
**Category\_2** has generics 126, 127, 128

As a result, I need to get the number of products in each category using a set of generics from each category to search.

I can't figure out how to do this with one query, I would be grateful for any help or advice. Thanks.

---

<div class="post-metadata">

### Author: ![Irina\_Truong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/irina_truong/32/112040_2.png) [@Irina\_Truong](https://discuss.elastic.co/u/Irina_Truong)
#### Post date: [April 28, 2022, 11:04pm UTC](https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497/2 "2022-04-28T23:04:17Z")

</div>

Hi @Denys_Lukash , welcome!

You would use a filter aggregations for this.

For example, if you have an index as follows:

```auto
PUT my-products
{
  "mappings": {
    "properties": {
      "name": {"type": "keyword"},
      "genericid": {"type": "short"}
    }
  }
}

```

with 3 documents:

```auto
POST my-products/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "Product 1", "genericid": [1, 2, 3] }
{ "index" : { "_id" : "2" } }
{ "name" : "Product 2", "genericid": [2, 3, 4] }
{ "index" : { "_id" : "3" } }
{ "name" : "Product 3", "genericid": [3, 4, 5] }

```

Here is how you could query that:

```auto
GET my-products/_search
{
  "size": 0, 
  "aggs": {
    "Category 1": {
      "filter": {
        "terms": {
          "genericid": [1, 2]
        }
      }
    },
    "Category 2": {
      "filter": {
        "terms": {
          "genericid": [3, 4, 5]
        }
      }
    }
  }
}

```

The response would be something like this:

```auto
{
  [...]
  "aggregations": {
    "Category 1": {
      "doc_count": 2
    },
    "Category 2": {
      "doc_count": 3
    }
  }
}

```

Documentation:

> **[Filter aggregation | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html)**

I hope this helps.

---

<div class="post-metadata">

### Author: ![Denys\_Lukash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/denys_lukash/32/102216_2.png) [@Denys\_Lukash](https://discuss.elastic.co/u/Denys_Lukash)
#### Post date: [May 2, 2022, 11:53am UTC](https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497/3 "2022-05-02T11:53:24Z")

</div>

Hi Irina, looks pretty good and simple. just tried it for my case and it seems that all works as expected.

Thank you a lot

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 30, 2022, 11:54am UTC](https://discuss.elastic.co/t/is-it-possible-to-calculate-the-number-of-results-by-grouping-them-by-several-criteria/303497/4 "2022-05-30T11:54:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
