首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElasticSearch能否在单个请求中执行具有不同查询条件的多个聚合?

ElasticSearch能否在单个请求中执行具有不同查询条件的多个聚合?
EN

Stack Overflow用户
提问于 2017-05-18 19:04:05
回答 1查看 1.5K关注 0票数 4

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

我有一个产品集合,它具有属性:typecolorbrand。用户选择:brand=Gap,color=White,type=Sandal。若要在每次聚合时显示各种类似产品的计数:

  • brand聚合的查询条件:color=White和type=Sandal
  • color聚合的查询条件:brand=Gap和type=Sandal
  • type聚合的查询条件:brand=Gap和color=White

这能在一个ElasticSearch查询中完成吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-19 01:40:49

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

代码语言:javascript
复制
  "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"
              }
            }
          ]
        }
      }
    }
  }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44055935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档