2013-03-26 12 views
2

我來自Solr背景,試圖在Elasticsearch中找到相當於"tagging" and "excluding"的地方。如何從一個方面排除過濾器?

在以下示例中,如何從prices構面的計算中排除price過濾器?換言之,prices方面應考慮除price之外的所有過濾器。

{ 
    query : { 
    "filtered" : { 
     "query" : { 
     "match_all" : {} 
     }, 
     "filter" : { 
     "and" : [ 
      { 
      "term" : { 
       "colour" : "Red" 
      } 
      }, 
      { 
      "term" : { 
       "feature" : "Square" 
      } 
      }, 
      { 
      "term" : { 
       "feature" : "Shiny" 
      } 
      }, 
      { 
      "range" : { 
       "price" : { 
       "from" : "10", 
       "to" : "20" 
       } 
      } 
      } 
     ] 
     } 
    } 
    }, 
    "facets" : { 
    "colours" : { 
     "terms" : { 
     "field" : "colour" 
     } 
    }, 
    "features" : { 
     "terms" : { 
     "field" : "feature" 
     } 
    }, 
    "prices" : { 
     "statistical" : { 
     "field" : "price" 
     } 
    } 
    } 
} 

回答

5

您可以將價格過濾器作爲頂級過濾器到您的查詢,並將其添加到各個方面的預期價格爲facet_filter:

{ 
    query : { 
    "filtered" : { 
     "query" : { 
     "match_all" : {} 
     }, 
     "filter" : { 
     "and" : [ 
      { 
      "term" : { 
       "colour" : "Red" 
      } 
      }, 
      { 
      "term" : { 
       "feature" : "Square" 
      } 
      }, 
      { 
      "term" : { 
       "feature" : "Shiny" 
      } 
      } 
     ] 
     } 
    } 
    }, 
    "facets" : { 
    "colours" : { 
     "terms" : { 
     "field" : "colour" 
     }, 
     "facet_filter" : { 
     "range" : { "price" : { "from" : "10", "to" : "20" } } 
     } 
    }, 
    "features" : { 
     "terms" : { 
     "field" : "feature" 
     }, 
     "facet_filter" : { 
     "range" : { "price" : { "from" : "10", "to" : "20" } } 
     } 
    }, 
    "prices" : { 
     "statistical" : { 
     "field" : "price" 
     } 
    } 
    }, 
    "filter": { 
    "range" : { "price" : { "from" : "10", "to" : "20" } } 
    } 
} 
+0

恐怕這似乎不起作用。 「價格」過濾器不適用於搜索結果。此外,價格過濾器仍然包含在「價格」面的計算中。 – gjb 2013-03-27 00:27:02

+1

它似乎在爲我工作 - https://gist.github.com/imotov/5250599 – imotov 2013-03-27 00:35:12

+0

謝謝,這完美的作品。我嵌套查詢的方式存在問題。只有一個最後的想法:有沒有辦法實現這一點,而沒有重複?我有很多方面,因此在每個其他方面添加'facet_filter'遠非理想。 – gjb 2013-03-27 00:58:42

0

順便說一句,重要的變化,因爲ES 1.0.0。頂級過濾器已重命名爲post_filter(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_search_requests.html#_search_requests)。 http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html

和有global選項刻面,以避免由查詢過濾器過濾(elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html#:和如本文所述,使用仍然優選過濾的查詢_範圍)。

相關問題