gpt4 book ai didi

Elasticsearch:脚本化指标聚合返回奇怪的分组

转载 作者:行者123 更新时间:2023-12-02 22:11:21 25 4
gpt4 key购买 nike

我得到了一个意想不到的结果,我有点失落。

已添加 这些文档 :

POST es_test/_doc/_bulk?pretty
{ "index": {}}
{ "firstname": "John", "lastname": "Doe", "age": 22, "birthdate": "1980-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "May", "lastname": "Greenwood", "age": 19, "birthdate": "1980-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "Marry", "lastname": "Hilake", "age": 32, "birthdate": "1970-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "Mister", "lastname": "X", "age": 20, "birthdate": "1990-11-23T12:30:00Z" }

它是 一切顺利当我这样要求他们时:
GET es_test/_doc/_search

出现问题当我添加 无痛脚本 :
GET es_test/_doc/_search    
{
"size": 0,
"aggs": {
"user": {
"scripted_metric": {
"init_script" : "params._agg.transactions = []",
"map_script" : "params._agg.transactions.add(params._source)"
}
}
}
}

输出 它看起来像这样:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"user": {
"value": [
{
"transactions": [
{
"firstname": "John",
"birthdate": "1980-01-20T12:30:00Z",
"age": 22,
"lastname": "Doe"
},
{
"firstname": "John",
"birthdate": "1980-01-20T12:30:00Z",
"age": 22,
"lastname": "Doe"
}
]
},
{
"transactions": []
},
{
"transactions": [
{
"firstname": "May",
"birthdate": "1980-01-20T12:30:00Z",
"age": 19,
"lastname": "Greenwood"
}
]
},
{
"transactions": []
},
{
"transactions": [
{
"firstname": "Marry",
"birthdate": "1970-01-20T12:30:00Z",
"age": 32,
"lastname": "Hilake"
}
]
}
]
}
}
}

第一 transactions数组包括两次约翰,第二次是空的,然后是五月,又是空的,最后是玛丽。 我不知道为什么它的分组如此奇怪 .

所需输出 将是 一个包含所有用户的数组 (约翰,梅,玛丽,先生)。

感谢您的帮助, 谢谢你!
游戏

最佳答案

为此,您必须使用 reduce_script。这有效

GET es_test/_doc/_search
{
"size": 0,
"aggs": {
"user": {
"scripted_metric": {
"init_script": "params._agg.transactions = []",
"map_script": "params._agg.transactions.add(params._source)",
"reduce_script": """
ArrayList transactions = [];
for (a in params._aggs) {
transactions.addAll(a.transactions)
}
return transactions
"""
}
}
}
}

结果
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"user": {
"value": [
{
"firstname": "John",
"birthdate": "1980-01-20T12:30:00Z",
"age": 22,
"lastname": "Doe"
},
{
"firstname": "Marry",
"birthdate": "1970-01-20T12:30:00Z",
"age": 32,
"lastname": "Hilake"
},
{
"firstname": "Mister",
"birthdate": "1990-11-23T12:30:00Z",
"age": 20,
"lastname": "X"
},
{
"firstname": "Mister",
"birthdate": "1990-11-23T12:30:00Z",
"age": 20,
"lastname": "X"
}
]
}
}
}

更新,下面的作品,这是相同的聚合,但它只使用了部分来源,这很奇怪。
GET es_test/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"user": {
"scripted_metric": {
"params": {
"_agg": {}
},
"init_script": "params._agg.transactions = []",
"map_script": "params._agg.transactions.add(params._source.firstname + ' ' + params._source.lastname)",
"reduce_script": """
ArrayList transactions = [];
for (a in params._aggs) {
transactions.addAll(a.transactions)
}
return transactions
"""
}
}
}
}

结果
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"user": {
"value": [
"John Doe",
"May Greenwood",
"Marry Hilake",
"Mister X"
]
}
}
}

关于Elasticsearch:脚本化指标聚合返回奇怪的分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50836784/

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