Elasticsearch aggregation query ignores parent filter when setting min_doc_count to 0 in child terms aggregation

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.

{: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.

{: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:

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.

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