我在索引中有下列文件 products
```
{ "product_name": "prod-1", "meta": [ { "tag": "tag1", "score": "12" }, { "tag": "tag2", "score": "24" } ] }
{ "product_name": "prod-2", "meta": [ { "tag": "tag1", "score": "36" } ] }
{ "product_name": "prod-2", "meta": [ { "tag": "tag2", "score": "44" } ] }
{ "product_name": "prod-3", "meta": [ { "tag": "tag3", "score": "54" } ] }
我知道如何分组 `product_name` 在es中
POST /products/_search
{
"size": 0,
"aggs": {
"by_product": {
"terms": {
"field": "product_name"
}
}
}
}
按分组后 `product_name` ,我希望每个bucket中都有一个名为meta的字段,该字段包含来自该bucket中所有文档的meta的并集,如下所示
[
{
"key": "prod-1",
"meta": [{ "tag": "tag1", "score": "12" }, { "tag": "tag2", "score": "24" }]
},
{
"key": "prod-2",
"meta": [{ "tag": "tag1", "score": "36" }, { "tag": "tag2", "score": "44" }]
},
{
"key": "prod-3",
"meta": [ { "tag": "tag3", "score": "54" } ]
}
]
如何在elaticsearch中实现这一点?
1条答案
按热度按时间sg2wtvxw1#
显示预期搜索结果的最佳方法是使用top hits聚合,您可以使用它向术语聚合添加其他字段
搜索查询:
搜索结果: