全文检索-Elasticsearch系列

ES搜索(三)aggs聚合与post_filter

2017-09-21  本文已影响0人  echo_ye4

对搜索结果进行聚合
对 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" }
  }
}
上一篇下一篇

猜你喜欢

热点阅读