我目前有以下结构的索引文件:
"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/