Post filter


Post filter

  在已經計算了聚合之后,post_filter應用於搜索請求最后的搜索匹配。其目的最好的例子如下:

  想像一下,您正在銷售具有以下屬性的襯衫:

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

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

  想象一下,用戶已經指定了兩個過濾器:

  color:red 和 brand:gucci.。您只想在搜索結果中顯示Gucci制造的紅色襯衫。通常你會用一個bool查詢

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "color": "red"   }},
        { "term": { "brand": "gucci" }}
      ]
    }
  }
}

  但是,您也可以使用多面導航來顯示用戶可以點擊的其他選項的列表。也許你有一個model字段,允許用戶將他們的搜索結果限制在紅色的Gucci t-shirts或dress-shirts上。

  這可以用術語聚合來完成:

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

  【1】返回Gucci最受歡迎的紅色襯衫款式。

  但也許您也想告訴用戶Gucci襯衫有多少其他顏色。如果您只是在顏色字段中添加術語聚合,則只會返回顏色為紅色,因為您的查詢只返回Gucci的紅色襯衫。

  相反,您要在聚合期間包括所有顏色的襯衫,然后將顏色過濾器應用於搜索結果。這是post_filter的目的:

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } 【1】
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } 【2】
    },
    "color_red": {【3】
      "filter": {
        "term": { "color": "red" } 
      },
      "aggs": {
        "models": {
          "terms": { "field": "model" } 
        }
      }
    }
  },
  "post_filter": { 【5】
    "term": { "color": "red" }
  }
}

  【1】主要查詢現在找到Gucci的所有襯衫,不管顏色如何。

  【2】color聚合返回Gucci襯衫流行的顏色。

  【3】color_red agg將模型子聚合限制為紅色Gucci襯衫。

  【4】最后,post_filter從搜索匹配中除去紅色以外的顏色。

 

原文地址:https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-post-filter.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM