使用Elastic在网上商店中进行分面搜索

时间:2017-10-18 09:53:06

标签: elasticsearch elasticsearch-5 elasticsearch-aggregation

我已经在Elastic中看到了一些关于分面搜索的例子,但他们都提前知道你想要创建存储桶的字段。

当我拥有一个包含多个类别的网上商店时,我应该如何工作?每个类别的值的属性都不同?

有没有办法描述运行查询时文档的属性(例如按类别过滤)?

我现在有这个问题:

{
   "from" : 0, "size" : 10,
   "query": {
        "bool" : {
            "must" : [
              { "terms": {"color": ["red", "green", "purple"]} },
              { "terms": {"make": ["honda", "toyota", "bmw"]} }
            ]
        }
   },
   "aggregations": {
     "all_cars": {
         "global": {},
         "aggs": {
              "colors": {
                "filter" : { "terms": {"make": ["honda", "toyota", "bmw"]} },
                "aggregations": {
                    "filtered_colors": { "terms": {"field": "color.keyword"} }
                }
              },
              "makes": {
                "filter" : { "terms": {"color": ["red", "green"]} },
                "aggregations": {
                    "filtered_makes": { "terms": {"field": "make.keyword"} }
                }
              }
          }
      }
   }
}

我如何知道我可以在哪些字段进行聚合。有没有办法在运行查询后描述文档的属性?所以我可以知道聚合的可能字段是什么。

2 个答案:

答案 0 :(得分:1)

现在我将我文章的所有属性存储在一个数组中,我可以像这样快速聚合它们:

{
  "size": 0,
  "aggregations": {
    "array_aggregation": {
      "terms": {
        "field": "properties.keyword",
        "size": 10
      }
    }
  }
}

这是向正确方向迈出的一步,但这样我不知道属性的类型是什么。

这是一个示例对象

            "price": 10000,
            "color": "red",
            "make": "honda",
            "sold": "2014-10-28",
            "properties": [
                "price",
                "color",
                "make",
                "sold"
            ]

答案 1 :(得分:0)

您可以使用过滤器聚合来过滤,然后在里面创建一个术语聚合吗?

相关问题