Aggregation with 0 count Elastic search
I have a set of documents in the elasticity index (id, name, dept, status) as {1, pone, d1, m2} {2, ptwo, d1, m2}, {3, ptwo, d2, m1} I want to get a query to get a group of results by status 'm2'. Also, the result set must include records with a zero number like {d1: 2}, {d2: 0}. How can we achieve this with Aggregation to find elastic objects?
{
"query": {
"match": {
"status": "m2"
}
},
"aggs" : {
"results" : {
"terms" : {
"field" : "dept"
}
}
}
}
This query returns "dept" with no null count as {d1: 2}. In addition, I also want records with 0 to be read as {d1: 2}, {d2: 0}. Thanks to
+3
source to share
1 answer
What you are looking for is a parameter min_doc_count
. Try the following:
{
"query": {
"match": {
"status": "m2"
}
},
"aggs" : {
"results" : {
"terms" : {
"field" : "dept",
"min_doc_count" : 0 <------ add this setting
}
}
}
}
+5
source to share