2013-03-26 66 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#:和如本文所述,使用仍然优选过滤的查询_范围)。

相关问题