gpt4 book ai didi

elasticsearch - 如何在嵌套文档上聚合每个存储桶的子存储桶

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

完整的示例代码:

https://gist.github.com/anonymous/329eaaf5654096c529da

对于标准的电子商务网站,我有一个简单的标准产品/选项映射,如下所示:

"mappings": {
"product": {
"properties" : {
"name":
{
"type": "string",
"fields": {
"raw": { "type": "string", "analyzer": "lowercase" }
},
"analyzer": "default"

},
"options" : {
"type": "nested",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"values": {"type": "nested"}
}
},
"price":{"type": "integer"},
"createdAt": {
"type": "date",
"format": "basic_date_time"
}
}
}
}

请注意,一个产品有多个选项,每个选项可以有多个值(即:衬衫的颜色包括蓝色,红色;选项尺寸包括M,XL)

当前,在使用多种条件搜索产品的查询之后,我汇总了结果以获取结果集中所有选项和选项值的列表:
"aggregations": {
"options": {
"nested": {
"path": "options"
},
"aggs": {
"options_ids": {
"terms": {
"field": "id"
}
},
"aggs": {
"nested": {
"path": "options.values"
},
"aggs": {
"options_values_ids": {
"terms": {
"field": "options.values.id"
}
}
}
}
}
}
}

一切正常,除了我得到这样的东西
"aggregations": {     
"options": {
"doc_count": 4,
"options_ids": {
"buckets": [
{
"key": 1,
"doc_count": 2
},
{
"key": 2,
"doc_count": 2
}
]
},
"aggs": {
"doc_count": 7,
"options_values_ids": {
"buckets": [
{
"key": 1,
"doc_count": 2
},
{
"key": 5,
"doc_count": 2
},
{
"key": 2,
"doc_count": 1
},
{
"key": 3,
"doc_count": 1
},
{
"key": 6,
"doc_count": 1
}
]
}
}
}
}

如您所见,我无法知道结果中的哪些选项值属于哪些选项。如果可以在每个选项下列出可用选项值,那就更好了。那有可能吗?

最佳答案

您将需要嵌套聚合:

"aggregations": {
"options" : {
"aggs" : {
"options_ids" : {
"aggs" : {
"aggs" : {
"options_values_ids" : {
"terms" : {
"field" : "options.values.id"
}
}
},
"nested" : {
"path" : "options.values"
}
},
"terms" : {
"field" : "id"
}
}
},
"nested" : {
"path" : "options"
}
}
}

关于elasticsearch - 如何在嵌套文档上聚合每个存储桶的子存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627105/

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