我正在尝试在 elasticsearch 中运行此查询。我正在尝试在我的存储桶上运行自定义 scripted_metric 聚合。在指标脚本中,我想访问聚合它的存储桶 key 。
我在 ES 中的文档是这样的。
{
user_id: 5,
data: {
5: 200,
8: 300
}
},
{
user_id: 8,
data: {
5: 889,
8: 22
}
}
我的聚合查询如下所示:
aggs = {
approvers: {
terms: {
field: 'user_id'
},
aggs: {
new_metric: {
scripted_metric: {
map_script: `
// IS IT POSSIBLE TO GET THE BUCKET KEY HERE?
// The bucket key here would be the user_id
// so i can do stuff like
doc['data'][**_term**]....
`
}
}
}
}
最佳答案
我不得不进行一些挖掘,并且在寻找如何检索父值的解决方案时可能遇到了同样的困难......我唯一能找到的是关于一个特殊的“_count”值子 agg,但与其父存储桶名称/键无关。
如果使用带有 scripted_metric 的子聚合不是严格要求,我能够找到一种方法,让您至少可以访问父级中的存储桶键。也许这可以让您开始朝着解决方案的方向前进:
aggs = {
approvers: {
terms: {
field: 'user_id',
script: '"There seems to be a magic value here: " + _value'
}
}
样本改编自 this
关于ElasticSearch:获取 bucket scripted_metric 中的 bucket key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678164/