14.ES|Post Filter

Post Filterとは、検索結果とAggregations(集計結果)の両方に対して、Filterをかける機能。

  • サンプルクエリ
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": { 
    "term": { "color": "red" }
  }
}

.The main query now finds all shirts by Gucci, regardless of color.

.The colors agg returns popular colors for shirts by Gucci.

.The color_red agg limits the models sub-aggregation to red Gucci shirts.

.Finally, the post_filter removes colors other than red from the search hits.