elasticsearch - Elasticsearch:搜索分组结果
问题描述
我在 elasticsearch 索引中加载了来自不同类别的产品目录 - 衬衫、裤子、鞋子等。
我想支持一个用例,允许按名称搜索产品并获得按类别分组的结果。例如,当我搜索“红色”时,我想查看前 5 件红色衬衫、前 5 件红色裤子、前 5 件红色鞋子
我在这里找到了一种使用 top hits 聚合的方法。但我不确定这是否是正确的进步方式。这是一个 2 级聚合,我担心这会使查询变慢。
有没有更好(更快?)的方法来实现这一目标?
编辑:添加示例数据
样本目录
[
{
"title": "Red Shirt",
"category": "Shirt",
"id": 1
},
{
"title": "Blue Shirt",
"category": "Shirt",
"id": 2
},
{
"title": "Green Shirt",
"category": "Shirt",
"id": 3
},
{
"title": "Black Shoes",
"category": "Shoes",
"id": 4
},
{
"title": "White Shoes",
"category": "Shoes",
"id": 5
},
{
"title": "Red Shoes",
"category": "Shoes",
"id": 6
},
{
"title": "Red and White Shoes",
"category": "Shoes",
"id": 7
},
{
"title": "Red Bag",
"category": "Bag",
"id": 8
},
{
"title": "Red and Blue Bag",
"category": "Bag",
"id": 9
},
{
"title": "Yello Bag",
"category": "Bag",
"id": 10
}
]
搜索“红色”时的预期响应
[
{
"category": "Shirt",
"results": [
{
"title": "Red Shirt",
"category": "Shirt",
"id": 1
}
]
},
{
"category": "Shoes",
"results": [
{
"title": "Red Shoes",
"category": "Shoes",
"id": 6
},
{
"title": "Red and White Shoes",
"category": "Shoes",
"id": 7
}
]
},
{
"category": "Bag",
"results": [
{
"title": "Red Bag",
"category": "Bag",
"id": 8
},
{
"title": "Red and Blue Bag",
"category": "Bag",
"id": 9
}
]
}
]
解决方案
推荐阅读
- java - 在java中打印2个日期之间的所有日期,特殊情况
- hyperledger - 运行超级账本 iroha 的快速启动版本和构建 iroha 有什么区别?
- azure-api-management - Azure - API 管理 - 管理 API SAS 令牌到期
- linear-programming - In Scipy Linprog, is there a setting that makes the results "nicer"?
- c++ - 我可以将 CFMutableDictionaryRef 转换为 CFDictionaryRef 吗?
- javascript - 使用 jquery 或 js 刷新所有浏览器选项卡
- android - 如何以编程方式从 Firebase 获取无崩溃用户的百分比
- django - 如何在 Azure Kubernetes 服务上向公众隐藏 Django 管理员,同时通过后门保持访问
- c# - 在 Asp.net 核心 Web API 中使用 Continuation 令牌的正确方法是什么
- javascript - React Material-UI:单击后如何禁用工具提示