首页 > 解决方案 > 如何从 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 映射,因为我正在从头开始构建它。

标签: elasticsearch

解决方案


您正在寻找的是ES 中的聚合。我将从它开始,Terms Aggregations这是一种桶聚合。

聚合的一个简单示例"brand"如下所示:

GET <index_name>/products/_search
{
    "aggs" : {
        "brands" : {
            "terms" : { "field" : "brand" }
        }
    }
}

推荐阅读