Remove Empty Bucket from Sub Aggregation


#1

We are using multi level Aggregation:
`{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": []
}
}
},

"aggs": {
	"FIELD1": {
		"terms": {
			"field": "City"
		},
		"aggs": {
			"FIELD2": {
				"terms": {
					"field": "Class"
				}
			}
		}
	}
}

}`

We have buckets of "City" and each bucket of City has buckets of "Class". Now for some, Class Bucket is empty as the value of the field is NULL. How can we remove Empty Bucket from the Aggregation.


(Geunmoon Oh) #2

i made script below.

{
  "size": 0,
  "aggregations": {
    "in_product": {
      "filter": {
        "exists": {
          "field": "Class"
        }
      },
      "aggs": {
        "FIELD1": {
          "terms": {
            "field": "City"
          },
          "aggs": {
            "FIELD2": {
              "terms": {
                "field": "Class"
              }
            }
          }
        }
      }
    }
  }
}

#3

Thanks this worked :slightly_smiling:


#4

Another question would be how can we get back the documents even if the Class field is null. Currently we are getting empty buckets when Class is Null for some keys in City.


(system) #5