轮胎弹性搜索过滤器由关联ID

时间:2013-07-27 19:29:40

标签: ruby-on-rails ruby elasticsearch tire

我有一个有选民的邮政模型。在弹性搜索中,我已将Post编入索引,以便我有以下示例数据:name: 'My first post', voter_ids: [1,2,3]。我正在尝试搜索帖子,以便我只获得给定voter_ids的结果。例如,如果搜索显示1,2,我想在结果中获得My first post,因为它有voter_ids 1和2,但如果搜索是1,5,我不应该在我的结果中看到该帖子选民5从未在该职位上投票。 以下代码返回所有指定了任何voter_ids的帖子。所以它就像OR-ing。我在寻找的是和。

@posts = Post.search(load: true) do |tire|
  tire.filter :terms, :voter_ids => voter_ids
end

1 个答案:

答案 0 :(得分:3)

创建相当于:

"filter" : {
    "terms" : {
        "voter_ids" : ["1", "2"],
        "execution" : "and"
    }
}

From reading the elasticsearch docs execution过滤器上有一个terms参数,您可以将其设置为and

祝你好运!

相关问题