gpt4 book ai didi

elasticsearch - Elasticsearch聚合和最大计数类型的操作

转载 作者:行者123 更新时间:2023-12-03 02:14:21 27 4
gpt4 key购买 nike

我在Elasticsearch中存储了一些文档,如下所示:

{
"date" : 1,
"field1" : 0.2,
"field2" : 0.5,
"field3" : 0.3
},
{
"date" : 1,
"field1" : 0.9,
"field2" : 0.5,
"field3" : 0.1
},
{
"date" : 2,
"field1" : 0.2,
"field2" : 0.6,
"field3" : 0.7
}
我想得到的是对每个文档中field1,field2或field3中每个字段最大的次数进行计数,即按日期分组。期望结果是这样的:
{
"date" : 1,
"field1-greatest" : 1,
"field2-greatest" : 1,
"field3-greatest" : 0
},
{
"date" : 2,
"field1-greatest" : 0,
"field2-greatest" : 0,
"field3-greatest" : 1
}
我在日期上使用术语聚合,但不确定如何使用Elasticsearch聚合比较不同字段以执行此最大和计数类型操作。有什么建议么?

最佳答案

您可以执行以下操作:

{
"size": 0,
"aggs": {
"by_date": {
"terms": {
"field": "date"
},
"aggs": {
"field1_greatest": {
"max": {
"field": "field1"
}
},
"field2_greatest": {
"max": {
"field": "field2"
}
},
"field3_greatest": {
"max": {
"field": "field3"
}
}
}
}
}
}
提示:确保将 field*属性映射为 double类型,而不是 float类型,因为 field1的最大agg可能会生成 0.8999999761581421而不是 0.9

更正
这是一个不平凡的用例,因此您可能需要 use a script。以下是一些入门知识:
{
"size": 0,
"aggs": {
"by_date": {
"terms": {
"field": "date"
},
"aggs": {
"by_greatest": {
"scripted_metric": {
"init_script": """
state.field1_greatest = 0;
state.field2_greatest = 0;
state.field3_greatest = 0;
""",
"map_script": """
def v1 = doc['field1'].value;
def v2 = doc['field2'].value;
def v3 = doc['field3'].value;

// your comparison logic
""",
"combine_script": "state",
"reduce_script": "states"
}
}
}
}
}
}

关于elasticsearch - Elasticsearch聚合和最大计数类型的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63911238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com