简体   繁体   English

通过has_many关联搜索Rails轮胎(ElasticSearch)

[英]Rails tire(elasticsearch) search through has_many association

I have 2 associations 我有2个协会

belongs_to :author
has_many :favorites

I'm wondering why this example works: 我想知道为什么这个例子有效:

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

And this one doesnt: 而这个没有:

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

Can anyone help me? 谁能帮我?

Foreign keys are stored in the child table, Rails does the joining of the two tables for you. 外键存储在子表中,Rails为您完成两个表的连接。

So in this model, there is an attribute author_id because this model belongs to an author. 因此,在此模型中,存在属性author_id因为该模型属于作者。 The foreign keys for the favorites relationship are stored in the favorites table. 收藏夹关系的外键存储在收藏夹表中。 While you can do Model.first.favorites and get the corresponding favorites , it is because of a value stored in the latter table. 尽管可以执行Model.first.favorites并获取相应的favorites ,但这是因为后者表中存储了一个值。

Model.first.author_id exists. 存在Model.first.author_id Model.first.favorite_ids does not. Model.first.favorite_ids没有。

If you want to run a search on favorites_ids , you're going to need to explicitly define that in the to_indexed_json method. 如果您要在favorites_ids上进行搜索,则需要在to_indexed_json方法中明确定义该to_indexed_json

Also, tire has been retired. 另外, 轮胎已经退役。 You should look into migrating to elasticsearch-rails , or my preferred gem, searchkick ! 您应该考虑迁移到elasticsearch -rails或我首选的宝石searchkick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM