ruby-on-rails - 通过关联ID的轮胎 Elasticsearch 过滤器

标签 ruby-on-rails ruby elasticsearch tire

我有一个有选民的邮政模型。在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 docsexecution过滤器上有一个terms参数,您可以将其设置为and

祝好运!

关于ruby-on-rails - 通过关联ID的轮胎 Elasticsearch 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901661/

相关文章:

ruby-on-rails - 如何恢复我的 Rails 4 binstubs?

ruby-on-rails - 如何在我的代码库中忽略/抑制特定类型的异常(ActiveRecord::ReadOnlyRecord)

ruby - 如何在 ruby​​ 中使用 ssh 隧道连接 postgres 数据库

elasticsearch - Elasticsearch:使用filter和constant_score优化查询吗?

ruby-on-rails - Capistrano Rails 正在跳过任务 `deploy:updating',因为它之前已被调用

ruby-on-rails - 在 ubuntu 启动时运行 rake 任务

javascript - Bootstrap JS 不与中间人一起工作

ruby - Ruby 中 STDIN 的最佳实践?

elasticsearch - Couchbase Elasticsearch Connector保存检查点相同的存储桶

elasticsearch - Elasticsearch位置数组的非法映射