我有一个有选民的邮政模型。在Elastic Search中,我已经为Post编制了索引,因此我有以下示例数据:name: 'My first post', voter_ids: [1,2,3]
。我正在尝试搜索帖子,以便仅获得给定的表决者ID的结果。例如,如果搜索说1,2
,我想在结果中获取My first post
,因为它具有选民1和2,但是如果搜索是1,5
,我就不会在结果中看到该帖子,因为投票者5从未对该帖子投票。
以下代码将返回所有指定了任何选民名称的帖子。所以就像OR-ing我正在寻找的是AND。
@posts = Post.search(load: true) do |tire|
tire.filter :terms, :voter_ids => voter_ids
end
最佳答案
创建等效项:
"filter" : {
"terms" : {
"voter_ids" : ["1", "2"],
"execution" : "and"
}
}
From reading the elasticsearch docs在
execution
过滤器上有一个terms
参数,您可以将其设置为and
。祝好运!
关于ruby-on-rails - 通过关联ID的轮胎 Elasticsearch 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901661/