首页 文章

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

提问于
浏览
0

我有一个有选民的邮政模型 . 在弹性搜索中,我已将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 回答

  • 3

    创建相当于:

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

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

    祝好运!

相关问题