Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 301
CompuDynasty Avatar asked Jul 21 '15 08:07

CompuDynasty


People also ask

What is Elasticsearch aggregations?

Elasticsearch - Aggregations. The aggregations framework collects all the data selected by the search query and consists of many building blocks, which help in building complex summaries of the data.

What is the default shard_size and Doc_count in Elasticsearch?

shard_size cannot be smaller than size (as it doesn’t make much sense). When it is, Elasticsearch will override it and reset it to be equal to size. The default shard_size is (size * 1.5 + 10). doc_count values for a terms aggregation may be approximate. As a result, any sub-aggregations on the terms aggregation may also be approximate.

What is a single-value metrics aggregator?

A single-value metrics aggregation that counts the number of values that are extracted from the aggregated documents. These values can be extracted either from specific fields in the documents, or be generated by a provided script. Typically, this aggregator will be used in conjunction with other single-value aggregations.

Why does Elasticsearch only return the top terms of a bucket?

when there are lots of unique terms, Elasticsearch only returns the top terms; this number is the sum of the document counts for all buckets that are not part of the response the list of the top buckets, the meaning of top being defined by the order


1 Answers

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
            }
        }
    }
 }
like image 199
Val Avatar answered Oct 12 '22 12:10

Val