Filter records having count =0 in aggregation

Hi,

I have a requirement to fetch categories having zero count

Query :

GET /category-sale-*/_search?size=100&filter_path=aggregations
{
"query": {
"bool": {
"filter": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"match_phrase": {
"region_cd": "6"
}
},
{
"match_phrase": {
"region_cd": "65"
}
},
{
"match_phrase": {
"region_cd": "62"
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}
},
"aggs": {
"Data_val": {
"filter": {
"range": {
"@timestamp": {
"gte":"now-2h",
"lte":"now"
}
}
},
"aggs": {
"NAME": {
"terms": {
"field": "catgy_id",
"size": 10000,
"min_doc_count": 0,
"order": {
"_count": "asc"
}
}

  }
  }
}

}
}

Output:-

{
"aggregations": {
"Data_val": {
"doc_count": 21027297,
"NAME": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1302",
"doc_count": 0
},
{
"key": "353",
"doc_count": 0
},
{
"key": "301",
"doc_count": 0
},
{
"key": "221",
"doc_count": 0
},
....
}

The output has both records having doc_count 0 and non-zero values. Requirement is to fetch the category having only zero

Used the below query

GET /category-sale-*/_search?size=100&filter_path=aggregations
{
"query": {
"bool": {
"filter": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"match_phrase": {
"region_cd": "6"
}
},
{
"match_phrase": {
"region_cd": "65"
}
},
{
"match_phrase": {
"region_cd": "62"
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}
},
"aggs": {
"Data_val": {
"filter": {
"range": {
"@timestamp": {
"gte":"now-2h",
"lte":"now"
}
}
},
"aggs": {
"NAME": {
"terms": {
"field": "catgy_id",
"size": 10000,
"min_doc_count": 0,
"order": {
"_count": "asc"
}
} ,
"aggs":{
"filter_zero":{
"bucket_selector": {
"buckets_path": {
"val":"NAME.buckets"
},
"script": "val._count==0"
}
}
}
}
}
}
}
}

but getting error ""type": "action_request_validation_exception",
"reason": "Validation Failed: 1: No aggregation found for path [NAME.buckets];"

How to achieve this?

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