# What to do to show aggregations doc\_count 0, for graphics, when result dont exists

**URL:** <https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410>\
**Category:** Elasticsearch\
**Created:** [July 13, 2016, 1:41pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410 "2016-07-13T13:41:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuelr](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@samuelr](https://discuss.elastic.co/u/samuelr)\
**Post date:** [July 13, 2016, 1:41pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/1 "2016-07-13T13:41:53Z")

</div>

hello, I have this sample search where the result I want to use in a highchart chart.

```
 `GET _search
{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                    {"terms": {"sexo": ["M","F"]}},
                     {"terms": {"doenca":
                     ["Dengue","Intox Alcoolica","DST","Intox Alimentar","Animal Peconhento"
                     ]}}]
                }
            }
        }
    },  
    "size": 0,
        "aggs" : {
            "municipios" : {
                "terms" : { 
                    "field" : "bairro",
                    "size":15
                },
                
                "aggs": {
    "doencas": {
      "terms": { 
        "field": "doenca", "size" : 5,"order" : { "_term" : "asc" }
      }
    }}
        }
        }
  }`

```

As you can see, before the aggregations, it goes through some queries and terms.  
The problem is that when it does not find the result to the Aggregation, he's not returning any data, and I need it to return 0 in the case.

a return Example:

```
{
   "took": 69,
    "timed_out": false,
    "_shards": {
       "total": 5,
  "successful": 5,
   "failed": 0
 },
 "hits": {
   "total": 5260,
   "max_score": 0,
   "hits": []
},
"aggregations": {
  "municipios": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 2234,
     "buckets": [
        { {
           "key": "Ouro Verde",
           "doc_count": 248,
           "doencas": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                 {
                    "key": "Animal Peconhento",
                    "doc_count": 38
                 },
                 {
                    "key": "DST",
                    "doc_count": 68
                 },
                 {
                    "key": "Dengue",
                    "doc_count": 1
                 },
                 {
                    "key": "Intox Alcoolica",
                    "doc_count": 101
                 },
                 {
                    "key": "Intox Alimentar",
                    "doc_count": 40
                 }
              ]
           }
        },
        {
           "key": "Municipal",
           "doc_count": 244,
           "doencas": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                 {
                    "key": "Animal Peconhento",
                    "doc_count": 34
                 },
                 {
                    "key": "DST",
                    "doc_count": 40
                 },
                 {
                    "key": "Intox Alcoolica",
                    "doc_count": 145
                 },
                 {
                    "key": "Intox Alimentar",
                    "doc_count": 25
                 }
              ]
           }
        },

```

The second bucket, did not return the key "Dengue", because in this case there was no record in the database, but I need to return the result, even if it is 0. For example:  
{  
"Key": "Dengue"  
"Doc\_count": 0  
},

How can I make it returns 0 when there are no cases?

Note: I tried using min\_doc\_count, like this:

```
  "aggs": {
    "doencas": {
      "terms": { 
        "field": "doenca","min_doc_count": 0, "size" : 5,"order" : { "_term" : "asc" }
      }
    }}

```

but using this way, it violates the terms of the query, and brings totally erroneous results.  
thank you for attention

---

<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:** [July 13, 2016, 3:04pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/2 "2016-07-13T15:04:57Z")

</div>

You can set the `min_doc_count` option on the Terms Aggregation. If you set it to zero (`min_doc_count: 0`), it'll return all buckets, even if they have zero documents

Here are the docs for it: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#\_minimum\_document\_count\_3](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_minimum_document_count_3)

Note: this could result in very large / expensive terms aggregations and responses.

---

<div class="post-metadata">

**Author:** ![samuelr](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@samuelr](https://discuss.elastic.co/u/samuelr)\
**Post date:** [July 13, 2016, 4:55pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/3 "2016-07-13T16:55:37Z")

</div>

yes it returns all but I needed him to return only those that added in the query

```
{"terms": {"doenca": ["Dengue","Intox Alcoolica","DST","Intox Alimentar","Animal Peconhento"

```

instead it returns all, restricted to 5 for the site, but not the field I need.  
Thank you so much

---

<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:** [July 13, 2016, 5:04pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/4 "2016-07-13T17:04:18Z")

</div>

Oops, sorry, I totally skipped over the last part of your message where you mentioned that you tried `min_doc_count`. 😮 Sorry!

There are two ways you could tackle this, neither are the most pleasant. The root problem is that the query just finds documents that match the query, while the aggregation just summarizes details about the documents that are found. They aren't very closely coupled.

**Option A**

The first option is to use `min_doc_count: 0` as suggested, and also [filter the terms that are aggregated](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_2). So your agg would look something like this:

```auto
"aggs": {
    "doencas": {
      "terms": { 
        "field": "doenca",
        "min_doc_count": 0,
        "include": ["Dengue","Intox Alcoolica","DST","Intox Alimentar","Animal Peconhento"]
      }
    }
}

```

This will only include those explicit terms in the aggregation.

**Option B**

The second option is to construct a set of [filter aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html) for each term you're interested in:

```auto
"aggs": {
  "dengue": {
    "filter": {
      "term": "Dengue"
    }
  },
  "intox": {
    "filter": {
      "term": "Intox Alcoolica"
    }
  },
  ... etc ...
}

```

Which will give you a bucket-per-filter that you construct, regardless of if it matches documents or not.

Option A will likely give better performance, but B may be more flexible depending on what you want to do.

---

<div class="post-metadata">

**Author:** ![samuelr](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@samuelr](https://discuss.elastic.co/u/samuelr)\
**Post date:** [July 13, 2016, 5:18pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/5 "2016-07-13T17:18:26Z")

</div>

oh, option A, it worked perfectly.  
How I will use this return to mount a chart, it must always return the same order and without missing fields, because manage the return without the fields is hell.

Thank you very very much , I hope that this answer can help others.

---

<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:** [July 14, 2016, 12:55pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/6 "2016-07-14T12:55:51Z")

</div>

If you add back in the sort order that you had in your original post (`"order" : { "_term" : "asc" }`) you should be able to get a consistent order each time.

Goodluck!

---

<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 5, 2017, 10:35pm UTC](https://discuss.elastic.co/t/what-to-do-to-show-aggregations-doc-count-0-for-graphics-when-result-dont-exists/55410/7 "2017-07-05T22:35:33Z")

</div>


