ruby-on-rails - 通过has_many关联搜索Rails轮胎(ElasticSearch)

标签 ruby-on-rails elasticsearch tire

我有2个协会

belongs_to :author
has_many :favorites

我想知道为什么这个例子有效:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) do
  query { string params[:query], default_operator: "AND" } if params[:query].present?
  filter :term, author_id: ['111']
  sort { by :created_at, 'desc' }
end

而这个没有:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) do
  query { string params[:query], default_operator: "AND" } if params[:query].present?
  filter :term, favorite_ids: ['567']
  sort { by :created_at, 'desc' }
end

谁能帮我?

最佳答案

外键存储在子表中,Rails为您完成两个表的连接。

因此,在此模型中,存在一个author_id属性,因为此模型属于作者。收藏夹关系的外键存储在收藏夹表中。尽管可以执行Model.first.favorites并获取相应的favorites,但这是因为后者表中存储了一个值。
Model.first.author_id存在。 Model.first.favorite_ids没有。

如果要在favorites_ids上运行搜索,则需要在to_indexed_json方法中显式定义它。

此外,tire也已退休。您应该考虑迁移到elasticsearch-rails或我的首选 gem searchkick!

关于ruby-on-rails - 通过has_many关联搜索Rails轮胎(ElasticSearch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883582/

相关文章:

java - Spring Data Elasticsearch 只返回精确匹配

elasticsearch - 如何更新 ElasticSearch 中的多个项目?

ruby-on-rails - Elasticsearch + 轮胎 : good strategy to mock ES

ruby-on-rails - Bootstrap 和 collection_select

ruby-on-rails - 使用 Warbler 制作 .war 时获取正确的应用程序结构

ruby-on-rails - Rails - 带有 'attribute=' 方法的 alias_method_chain

ruby-on-rails - 如何为Mysql中的所有表创建模型rb文件

elasticsearch - @timestamp以外的时间字段名称不起作用

ruby-on-rails - 使用 Tire 和 ElasticSearch 搜索多个模型

elasticsearch - 涉及嵌套模型的复杂轮胎查询