ElasticSearch---es之Post Filter,聚合后过滤

使用场景

平常我们使用es,都会先查询、过滤后再进行聚合,但有时也需要在聚合后再过滤,

这时可以使用"后置过滤器",也就是PostFilter。

实践理解

阅读了官方文档后,感觉学习还是要多动手,才会理解更透彻。

参考官方文档,列举了以下例子。可以跟着动手玩一下。

  • 新建索引:
PUT /shirts
{
    "mappings": {
        "item": {
            "properties": {
                "brand": { "type": "keyword"},
                "color": { "type": "keyword"}
            }
        }
    }
}
  • 新增数据:
    第一条数据:
PUT /shirts/item/1?refresh
{
    "brand": "gucci",
    "color": "blue"
}

第二条数据:

PUT /shirts/item/2?refresh
{
    "brand": "gucci",
    "color": "red"
}

第三条数据:

PUT /shirts/item/3?refresh
{
    "brand": "dior",
    "color": "red"
}
  • PostFilter,聚合后过滤
GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "brand": "gucci" }}   
      ]
    }
  },
  "size": 20,
  "aggs": {
    "agg_color": {
        "terms": { "field": "color" }  
    }
  }, 
  "post_filter": {
    "term": { "color": "red" } 
  }
}

在以上的DSL上,
(1)用filter过滤出gucci品牌的衬衫;
(2)用aggs对color进行聚合;
(3)用post_filter过滤出color为红色的衬衫;

  • 查询结果 :
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "shirts",
        "_type": "item",
        "_id": "2",
        "_score": 0,
        "_source": {
          "brand": "gucci",
          "color": "red"
        }
      }
    ]
  },
  "aggregations": {
    "agg_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "blue",
          "doc_count": 1
        },
        {
          "key": "red",
          "doc_count": 1
        }
      ]
    }
  }
}

用filter过滤出gucci品牌的衬衫,用aggs对color进行聚合。
我们看一下"aggregations"的结果,可以看到color聚合结果中,是存在blue的,
而且"key"为red的doc_count只有一条,而不是两条,品牌为dior的red衬衫已经去掉了。
用post_filter过滤出color为红色的衬衫,所以"hits"的结果是没有blue的。

说明是先filter,再agg,最后再postFilter的。

参考资料

http://doc.codingdict.com/elasticsearch/105/
https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html

posted on   乐之者v  阅读(3937)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2017-03-30 Error running : Address localhost:1099 is already in use
2017-03-30 Element div is not closed
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示