# Elasticsearch aggregation query ignores parent filter when setting min\_doc\_count to 0 in child terms aggregation

**URL:** https://discuss.elastic.co/t/elasticsearch-aggregation-query-ignores-parent-filter-when-setting-min-doc-count-to-0-in-child-terms-aggregation/174434
**Category:** Elasticsearch
**Created:** [March 28, 2019, 9:14pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-query-ignores-parent-filter-when-setting-min-doc-count-to-0-in-child-terms-aggregation/174434 "2019-03-28T21:14:11Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![m5tt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m5tt/32/43044_2.png) [@m5tt](https://discuss.elastic.co/u/m5tt)
#### Post date: [March 28, 2019, 9:14pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-query-ignores-parent-filter-when-setting-min-doc-count-to-0-in-child-terms-aggregation/174434/1 "2019-03-28T21:14:12Z")

</div>

If i run this query in Elasticsearch, it returns a bucket set aggregating on field `name`. Not returning any document that has a `type` field of **1** This is correct.

```auto
{:size=>0,
 :aggs=>
  {:agg=>
    {:filter=>{:term=>{"type"=>1}},
     :aggs=>{:agg=>{:terms=>{:field=>"name", :min_doc_count=>1}}}}}}

```

Now if i run this exact same query, changing _only_ `min_doc_count` to **0**. Then it _ignores_ the parent filter aggregation and returns _all_ documents, regardless of type.

```auto
{:size=>0,
 :aggs=>
  {:agg=>
    {:filter=>{:term=>{"type"=>1}},
     :aggs=>{:agg=>{:terms=>{:field=>"name", :min_doc_count=>0}}}}}}

```

If i reverse the query, ie the filter aggregation nested inside the terms, then it doesnt work regardless of the `min_doc_count` setting.

I understand this is documented [Here:](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

> Setting `min_doc_count` = `0` will also return buckets for terms that didn’t match any hit. However, some of the returned terms which have a document count of zero might only belong to deleted documents or documents from other types, so there is no warranty that a `match_all` query would find a positive document count for those terms.

**However there _must_ be a way to build a aggregation query that returns documents with a doc\_count 0 which are _also_ filtered by a filter aggregation.**

I am using Elasticsearch 6.x, and using it through the Elasticsearch Rails gem.

---

<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: [April 25, 2019, 9:14pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-query-ignores-parent-filter-when-setting-min-doc-count-to-0-in-child-terms-aggregation/174434/2 "2019-04-25T21:14:23Z")

</div>

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