今天在ES上做了一个聚合,先过滤一个嵌套对象,再对另一个域做聚合,但是过滤似乎没有起作用
{
"size":0,
"filter":{
"nested":{
"path":"nna_risks",
"filter":{
"exists":{
"field":"nna_risks.ina_id"
}
}
}
},
"aggs":{
"level0":{
"terms":{
"script":"doc['inp_type'].value"
}
}
}
}
结果如下:
took: 47
timed_out: false
▾ _shards{}
total: 25
successful: 25
failed: 0
▾ hits{}
total: 31470
max_score: 0
▾ hits[]
▾ aggregations{}
▾ level0{}
doc_count_error_upper_bound: 0
sum_other_doc_count: 0
▾ buckets[]
key: ""
doc_count: 147617
key: ""
doc_count: 139434
key: ""
doc_count: 47220
key: ""
doc_count: 24580
key: ""
doc_count: 10148
从结果上看,聚合到的数目相加与Hits不相等;分析后发现,聚合的数据不是来源于过滤后,而是整个数据集;
修改方法, 不用过滤,而是用查询过滤
{
"size":0,
"query":{
"filtered":{
"filter":{
"nested":{
"path":"nna_risks",
"filter":{
"exists":{
"field":"nna_risks.ina_id"
}
}
}
}
}
},
"aggs":{
"level0":{
"terms":{
"script":"doc['inp_type'].value"
}
}
}
}
结果集:
took: 14
timed_out: false
▾ _shards{}
total: 25
successful: 25
failed: 0
▾ hits{}
total: 31617
max_score: 0
▾ hits[]
▾ aggregations{}
▾ level0{}
doc_count_error_upper_bound: 0
sum_other_doc_count: 0
▾ buckets[]
key: ""
doc_count: 18634
key: ""
doc_count: 7464
key: ""
doc_count: 2845
key: ""
doc_count: 1738
key: ""
doc_count: 936
或者在聚合体中过滤:
{
"size":0,
"aggs":{
"level0":{
"filter":{
"nested":{
"path":"nna_risks",
"filter":{
"exists":{
"field":"nna_risks.ina_id"
}
}
}
},
"aggs":{
"level1":{
"terms":{
"script":"doc['inp_type'].value"
}
}
}
}
}
}
结果为:
took: 36
timed_out: false
▾ _shards{}
total: 25
successful: 25
failed: 0
▾ hits{}
total: 375035
max_score: 0
▾ hits[]
▾ aggregations{}
▾ level0{}
doc_count: 31836
▾ level1{}
doc_count_error_upper_bound: 0
sum_other_doc_count: 0
▾ buckets[]
key: ""
doc_count: 18727
key: ""
doc_count: 7525
key: ""
doc_count: 2878
key: ""
doc_count: 1743
key: ""
doc_count: 963