ES搜索(三)aggs聚合与post_filter

对搜索结果进行聚合
对 gucci 品牌的 color 进行聚合

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "brand": "gucci" }}
      ]
    }
  },
  "aggs": {
    "models": {
      "terms": { "field": "color" } 
    }
  }
}

对聚合结果进行过滤
对 gucci 品牌的 color 进行聚合,同时对 gucci 品牌的 red 颜色的 model 进行聚合

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } 
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } 
    },
    "color_red": {
      "filter": {
        "term": { "color": "red" } 
      },
      "aggs": {
        "models": {
          "terms": { "field": "model" } 
        }
      }
    }
  }
}

使用post_filter,可以使聚合不受过滤影响,适用于聚合条件与过滤条件不一致的情况
对 gucci 品牌的 color 进行聚合,并且返回过滤 red 颜色的数据

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } 
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } 
    }
  },
  "post_filter": { 
    "term": { "color": "red" }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • title:乐优商城学习笔记十一-Elasticsearch学习(三)date: 2019-04-18 09:49...
    smallmartial阅读 5,008评论 2 0
  • 1.1基本概念 Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL...
    _麻辣香锅不要辣阅读 12,158评论 2 10
  • 什么是聚合 每个聚合都是一个或者多个桶和零个或者多个指标的组合。 桶(Buckets) 满足特定条件的文档的集合。...
    壹点零阅读 7,835评论 0 2
  • 抱怨,除了让自己和别人心烦,什么都做不了……可是我又忍不住不抱怨,不去喷…… 我还是太嫩了,太渴望又太急躁。很多人...
    安鑫鑫森阅读 1,693评论 0 1
  • 意想不到,今天晚上打做的时候,跟随音乐很快进入了状态,听着音乐想着音乐中的画面,又美又清净想着在千水之间,鸟语花香...
    云少nn阅读 1,508评论 0 0

友情链接更多精彩内容