# Aggregation for count values on boolean

**URL:** <https://discuss.elastic.co/t/aggregation-for-count-values-on-boolean/104762>\
**Category:** Elasticsearch\
**Created:** [October 21, 2017, 12:25pm UTC](https://discuss.elastic.co/t/aggregation-for-count-values-on-boolean/104762 "2017-10-21T12:25:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanGl](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@StefanGl](https://discuss.elastic.co/u/StefanGl)\
**Post date:** [October 21, 2017, 12:25pm UTC](https://discuss.elastic.co/t/aggregation-for-count-values-on-boolean/104762/1 "2017-10-21T12:25:37Z")

</div>

Hello,

I have 100 documents with an boolean field, 80 docs are true, 20 are false.

How can I return only the "true" ones in an aggregation?

I'm looking for an aggregation similar to this sql:

```
SELECT count(*) FROM docs WHERE field = 'true';

```

I've tried an Term-Aggregation, but that returns also the false-values:

```
"myField": {

    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
        {
            "key": 0,
            "key_as_string": "false",
            "doc_count": 20
        }
        ,
        {
            "key": 1,
            "key_as_string": "true",
            "doc_count": 80
        }
    ]
}

```

Is there a shorter aggregation, which count's only me "true"-fields?

Best regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 21, 2017, 3:07pm UTC](https://discuss.elastic.co/t/aggregation-for-count-values-on-boolean/104762/2 "2017-10-21T15:07:59Z")

</div>

Why doing an aggregation for that?

You can just run a normal query which filters on that field with size=0. And just get the total hits.

If you really want an aggregation, use the filter agg.

---

<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:** [November 18, 2017, 3:08pm UTC](https://discuss.elastic.co/t/aggregation-for-count-values-on-boolean/104762/3 "2017-11-18T15:08:00Z")

</div>

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