我想对elasticsearch中的列表字段进行聚合。但是,在执行相同操作时,由于这是列表字段,如果列表中存在其他值以及搜索值,那么聚合也会在结果中给出这些值。有什么方法可以过滤此聚合结果以仅获取匹配的记录。 也希望这也能与分页一起使用。
示例:
医生:
{
id: 1,
field: [foo, bar, foobar]
},
{
id: 2,
field: [foo]
},
{
id: 3,
field: [foo, foobar]
}
ES查询:
{
"aggFilter": {
"filter": {
"terms": {
"field": [
"foo"
],
"boost": 1
}
},
"aggregations": {
"fieldAggregation0": {
"terms": {
"field": "field",
"size": 1000,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
输出:
{
"_key" : "foo",
"doc_count" : 3
},
{
"_key" : "foobar",
"doc_count" : 2,
},
{
"_key" : "bar",
"doc_count" : 1
}
期待:
{
"_key" : "foo",
"doc_count" : 3
}
尝试使用bucket_selector,但它给出错误,要过滤的值(bucket_path 处的值)应该是数字,在我的情况下,我想过滤 _key,它是字符串。
terms
聚合具有include
参数来过滤术语(也exclude
)
文件
POST /filter_array_items/_bulk
{"create":{"_id":1}}
{"id":"1","field":["foo","bar","foobar"]}
{"create":{"_id":2}}
{"id":"2","field":["foo"]}
{"create":{"_id":3}}
{"id":"3","field":["foo","foobar"]}
修改查询
GET /filter_array_items/_search?filter_path=aggregations
{
"query": {
"terms": {
"field": [
"foo"
]
}
},
"aggs": {
"fieldAggregation0": {
"terms": {
"field": "field.keyword",
"size": 1000,
"include": "foo",
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
回应
{
"aggregations" : {
"fieldAggregation0" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "foo",
"doc_count" : 3
}
]
}
}
}
我使用
terms
查询来过滤文档以进行聚合
使用
bucket_sort
聚合对结果进行分页