post_filter

  post_filter  后置过滤:在查询命中文档、完成聚合后,再对命中的文档进行过滤。

  要在一次查询中查询品牌为gucci且颜色为红色的shirts,同时还要得到gucci品牌各颜色的shirts的分面统计

PUT /shirts
{
  "mappings":{
    "properties": {
      "brand":{"type":"keyword"},
      "color":{"type":"keyword"},
      "model":{"type":"keyword"}
    }
  }
}

PUT /shirts/_doc/1?refresh
{
  "brand":"gucci",
  "color":"red",
  "model":"slim"
}

PUT /shirts/_doc/2?refresh
{
  "brand":"gucci",
  "color":"green",
  "model":"seec"
}

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

  查询结果

{
  "took" : 22,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "shirts",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "red",
          "model" : "slim"
        }
      }
    ]
  },
  "aggregations" : {
    "colors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "green",
          "doc_count" : 1
        },
        {
          "key" : "red",
          "doc_count" : 1
        }
      ]
    }
  }
}

  

 

posted on 2021-10-12 08:21  溪水静幽  阅读(93)  评论(0)    收藏  举报