【问题标题】:Can ElasticSearch perform multiple aggregations with different query conditions in a single request?ElasticSearch 可以在单个请求中执行具有不同查询条件的多个聚合吗?
【发布时间】:2017-05-18 19:04:05
【问题描述】:

我正在寻找一种解决方案来获取聚合,每个字段一个,但在不同的聚合中应用不同的查询条件。

我有一个产品集合,它具有以下属性:typecolorbrand。 用户选择:brand=Gapcolor=白色type=凉鞋。要显示每个聚合中各种相似产品的计数:

  • brand 聚合的查询条件:color=Whitetype=Sandal
  • color 聚合的查询条件:brand=差距,以及 type=凉鞋
  • type 聚合的查询条件:brand=Gapcolor=White

这可以在单个 ElasticSearch 查询中完成吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    您将创建三个聚合,每个聚合都有一个 filter agg,并在其中添加您想要的查询。我使用了最简单的一个 - boolterm - 只是为了展示高级方法:

      "aggs": {
        "brand_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "color": "white"
                  }
                },
                {
                  "term": {
                    "type": "sandal"
                  }
                }
              ]
            }
          }
        },
        "color_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "brand": "gap"
                  }
                },
                {
                  "term": {
                    "type": "sandal"
                  }
                }
              ]
            }
          }
        },
        "type_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "color": "white"
                  }
                },
                {
                  "term": {
                    "brand": "gap"
                  }
                }
              ]
            }
          }
        }
      }
    

    【讨论】:

    • 谢谢,这正是我想要的。 :)
    • 如果此答案对您有所帮助,将其标记为已接受应该有助于其他人在搜索类似查询时。
    猜你喜欢
    • 2020-09-21
    • 2014-05-12
    • 2021-12-12
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多