elasticsearch - 如何从 Elasticsearch 中获取过滤选项
问题描述
我在弹性搜索中有数据。我想在多个字段上生成过滤器选项以显示在 UI 上。
我在弹性搜索中有衣服数据,在弹性搜索中有属性"Brand", "Color", "Price"
。
我想从适用的品牌、颜色和价格范围的弹性搜索列表中获取。
另外,我希望根据过滤器选择来修改列表,例如,如果用户在品牌中选择,则不应获取 "nike"
不存在的颜色。"nike"
我的 ES 映射
{
"mappings":{
"products": {
"properties": {
"title": { "type": "text"},
"description": { "type": "text"},
"productCode": { "type": "keyword"},
"brand" : {"type":"keyword","fields": {"raw": {"type": "text"}}},
"color" : {"type":"keyword","fields": {"raw": {"type": "text"}}},
"category" : {"type":"keyword","fields": {"raw": {"type": "text"}}},
"price" : {"type":"double"},
"imageLink" : {"type":"keyword"}
}
}
}
}
我也愿意更改我的 ES 映射,因为我正在从头开始构建它。
解决方案
您正在寻找的是ES 中的聚合。我将从它开始,Terms Aggregations
这是一种桶聚合。
聚合的一个简单示例"brand"
如下所示:
GET <index_name>/products/_search
{
"aggs" : {
"brands" : {
"terms" : { "field" : "brand" }
}
}
}
推荐阅读
- python - 在 Jupyter 上进行 Pandas 分析时出错
- sql - 关于 SQL 查询中派生列的执行问题
- heroku - 将应用程序上传到heroku reactjs-nodejs后,api调用不起作用
- python - 从 Python Popen().communicate() 获取标准错误
- php - 组合 2 个查询以防止必须在 foreach 循环中插入查询
- python - 编码时输入字母时,spyder ide 会冻结片刻
- flutter - 如何在颤动中从 Firebase 存储中获取下载 URL
- ruby - 脚本“主”第 14 行:发生运行时错误,回溯太大。traceback.log 中的输出 Xenoverse PokeGame 如何修复?
- api - 在颤振/飞镖的 api 调用中获取数据的问题
- java - 有没有办法使用 MPJ 发送地图数据结构