This operation will show you my problem.
1、Create Index
PUT car
{
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"company": {
"type": "keyword"
},
"price":{
"type": "double"
}
}
}
}
2、Import data
POST car/_doc
{
"color":"red",
"company":"byd",
"price":120000
}
POST car/_doc
{
"color":"blue",
"company":"byd",
"price":130000
}
POST car/_doc
{
"color":"blue",
"company":"bmw",
"price":230000
}
3、Do Search
GET car/_search
{
"size": 0,
"aggs": {
"company": {
"terms": {
"field": "company"
},
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
},
"price_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"price": "price"
},
"script": "params.price > 200000"
}
}
}
}
}
}
}
}
The search query will return
{
"took" : 189,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"company" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "byd",
"doc_count" : 2,
"color" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ] # how to remove the empty result?
}
},
{
"key" : "bmw",
"doc_count" : 1,
"color" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "blue",
"doc_count" : 1,
"price" : {
"value" : 230000.0
}
}
]
}
}
]
}
}
}
What should I do if I wish to get the company and color of a car that sells for $200,000 or more? Now the "byd" in this query is invalid and I don't want him to appear.
I tried to add a second 'bucket_selector', but he doesn't filter the array length.
GET car/_search
{
"size": 0,
"aggs": {
"company": {
"terms": {
"field": "company"
},
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"price": {
"avg": {
"field": "price"
}
},
"price_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"price": "price"
},
"script": "params.price > 200000"
}
}
}
},
"empty_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"len": "color.buckets.length"
},
"script": "params.len>0"
}
}
}
}
}
}