您可以将Scripted Metric Aggregation 与reduce_script 一起使用。
设置一些测试数据:
curl -XPUT http://localhost:9200/testing/foo/1 -d '{ "foo" : [1, 2, 3] }'
curl -XPUT http://localhost:9200/testing/foo/2 -d '{ "foo" : [4, 5, 6] }'
现在试试这个聚合:
curl -XGET "http://localhost:9200/testing/foo/_search" -d'
{
"size": 0,
"aggs": {
"fooreduced": {
"scripted_metric": {
"init_script": "_agg[\"result\"] = []",
"map_script": "_agg.result.add(doc[\"foo\"].values)",
"reduce_script": "reduced = []; for (a in _aggs) { for (entry in a) { word = entry.key; reduced += entry.value } }; return reduced.flatten().sort()"
}
}
}
}'
调用将返回:
{
"took": 50,
"timed_out": false,
"_shards": {
"total": 6,
"successful": 6,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"fooreduced": {
"value": [
1,
2,
3,
4,
5,
6
]
}
}
}
可能有一个没有.flatten() 的解决方案,但我不太喜欢 groovy(还)找到这样的解决方案。而且这个聚合的性能有多好我也说不准,你自己去测试吧。