# Terms aggregation with filters inside

**URL:** <https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600>\
**Category:** Elasticsearch\
**Created:** [June 23, 2017, 9:04am UTC](https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600 "2017-06-23T09:04:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogauchard](https://avatars.discourse-cdn.com/v4/letter/o/3da27b/32.png) [@ogauchard](https://discuss.elastic.co/u/ogauchard)\
**Post date:** [June 23, 2017, 9:04am UTC](https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600/1 "2017-06-23T09:04:17Z")

</div>

Hi all,

I am trying to write a query to achieve this :

sample data :

```
{"code":"AAA", "bool1":true, "bool2":true, "bool3": false}
{"code":"AAA", "bool1":false, "bool2":true, "bool3": false}
{"code":"BBB", "bool1":true, "bool2":false, "bool3": true}
{"code":"BBB", "bool1":true, "bool2":true, "bool3": true}

```

expected result:

```
{
    "AAA": {
        "count_bool1": 1,
        "count_bool2": 2,
        "count_bool3": 0
    },
    "BBB": {
        "count_bool1": 2,
        "count_bool2": 1,
        "count_bool3": 2
    },
}

```

Is it possible using only one query ?

Thank you

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [June 23, 2017, 6:40pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600/2 "2017-06-23T18:40:20Z")

</div>

Yep. The easiest way is if you do a terms aggregation on field `"code"`, and then embed a terms agg for field `"bool1"` ,`"bool2"` and `"bool3"` inside that first agg, you'll get a count of true/false for each field, per code.

Something like:

```auto
{
  "aggregations": {
    "codes": {
      "terms": {
         "field": "code"
       },
       "aggs": {
         "bool1": {
            "terms": {
              "field": "bool1"
            }
          },
         "bool2": {
            "terms": {
              "field": "bool2"
            }
          },
         "bool3": {
            "terms": {
              "field": "bool3"
            }
          }
       }
    }
  }
}

```

You could also use a script to sum up true values:

```auto
{
  "aggregations": {
    "codes": {
      "terms": {
         "field": "code"
       },
       "aggs": {
         "bool1": {
            "sum": {
              "script": {
                 "inline": "doc.bool1.value.equals(true) ? 1 : 0;"
               }
            }
          },
         "bool2": {
            "sum": {
              "script": {
                 "inline": "doc.bool2.value.equals(true) ? 1 : 0;"
               }
            }
          },
         "bool3": {
            "sum": {
              "script": {
                 "inline": "doc.bool3.value.equals(true) ? 1 : 0;"
               }
            }
          }
       }
    }
  }
}

```

Note: I didn't test either of those queries, so I may have messed up the syntax slightly on accident 🙂

---

<div class="post-metadata">

**Author:** ![ogauchard](https://avatars.discourse-cdn.com/v4/letter/o/3da27b/32.png) [@ogauchard](https://discuss.elastic.co/u/ogauchard)\
**Post date:** [June 26, 2017, 3:48pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600/3 "2017-06-26T15:48:45Z")

</div>

Thank you !

The script query works like a charm.

---

<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:** [July 24, 2017, 3:49pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-filters-inside/90600/4 "2017-07-24T15:49:02Z")

</div>

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