gpt4 book ai didi

elasticsearch - ElasticSearch高级聚合

转载 作者:行者123 更新时间:2023-12-03 01:47:31 24 4
gpt4 key购买 nike

我目前有以下结构的索引文件:

"ProductInteractions": {
"properties": {
"SKU": {
"type": "string"
},
"Name": {
"type": "string"
},
"Sources": {
"properties": {
"Source": {
"type": "string"
},
"Type": {
"type": "string"
},
}
}
}
}

在此类型上搜索时,我想汇总结果。我最初只是想要 Source字段中的术语,这很容易。我只是对 terms字段使用了 Source聚合。

现在,我也想聚合 Type字段。但是,类型与源有关。例如,我可以有两个像这样的 Sources:
{
"Source": "The Store",
"Type": "Purchase"
}


{
"Source": "The Store",
"Type": "Return"
}

我想显示每个不同来源的不同类型及其数量。换句话说,我希望我的回应是这样的:
{
"aggs": {
"Sources": [
{
"Key": "The Store",
"DocCount": 2,
"Aggregations": {
"Types": [
{
"Key": "Purchase",
"DocCount": 1
},
{
"Key": "Return",
"DocCount": 1
}
]
}
}
]
}
}

有没有办法获得这些子集合?

最佳答案

是的,有,但是您需要稍微更改映射以使字段“not_analyzed”

"ProductInteractions": {
"properties": {
"SKU": {
"type": "string"
},
"Name": {
"type": "string"
},
"Sources": {
"properties": {
"Source": {
"type": "string",
"index": "not_analyzed"
},
"Type": {
"type": "string",
"index": "not_analyzed"
},
}
}
}
}

然后,您可以使用以下聚合来获得所需的内容:
{
"aggs": {
"sources": {
"terms": {
"field": "Sources.Source"
},
"aggs": {
"types": {
"terms": {
"field": "Sources.Type"
}
}
}
}
}
}

关于elasticsearch - ElasticSearch高级聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704092/

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