What to do to show aggregations doc_count 0, for graphics, when result dont exists

Oops, sorry, I totally skipped over the last part of your message where you mentioned that you tried min_doc_count. :open_mouth: 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. So your agg would look something like this:

"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 for each term you're interested in:

"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.

1 Like