CompuDynasty
CompuDynasty

Reputation: 154

Aggregation with 0 count Elastic Search

I have set of documents in elastic index(id,name,dept,status) as {1,pone , d1,m2} {2,ptwo,d1,m2},{3,ptwo,d2,m1} I want query to get the result group by dept for 'm2' status.Also result set should include the records with zero count as {d1:2}, {d2:0}. How can we achieve it using Elastic Search aggs?

 {
   "query": {
        "match": {
           "status": "m2"
        }
    },
    "aggs" : {
        "results" : {
            "terms" : {
               "field" : "dept"
            }
        }
    }
 }

This Query returns the 'dept' without zero count as {d1:2}.In addition I also want records with 0 count as {d1:2}, {d2:0}. Thanks

Upvotes: 9

Views: 3866

Answers (1)

Val
Val

Reputation: 217304

What you're looking for is the min_doc_count setting. Try this:

{
   "query": {
        "match": {
           "status": "m2"
        }
    },
    "aggs" : {
        "results" : {
            "terms" : {
               "field" : "dept",
               "min_doc_count" : 0      <------ add this setting
            }
        }
    }
 }

Upvotes: 23

Related Questions