0

我有一个有选民的 Post 模型。在 Elastic Search 中,我已将 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。我正在寻找的是AND。

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

1 回答 1

3

创建等效的:

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

通过阅读 elasticsearch 文档,您可以将过滤器上的execution参数terms设置为and.

祝你好运!

于 2013-07-27T20:35:39.973 回答