Tire 中对 ActiveRecord 关联的支持正在发挥作用,但需要在应用程序内部进行一些调整。毫无疑问,图书馆应该在这方面做得更好,将来也一定会的。
也就是说,这里是 Tire 配置的完整示例,可与 Elasticsearch 中的 Rails 关联一起使用:active_record_associations.rb https://gist.github.com/3200212
让我在这里强调几件事。
触摸父母
首先,您必须确保将关联的更改通知关联的父模型。
鉴于我们有一个Chapter
模型,它“属于”Book
,我们需要做:
class Chapter < ActiveRecord::Base
belongs_to :book, touch: true
end
这样,当我们做类似的事情时:
book.chapters.create text: "Lorem ipsum...."
The book
实例收到有关添加的章节的通知。
对触摸做出反应
整理好这部分后,我们需要通知Tire关于更改,并相应地更新elasticsearch索引:
class Book < ActiveRecord::Base
has_many :chapters
after_touch() { tire.update_index }
end
(毫无疑问Tire应该拦截after_touch
通知本身,而不是强迫您这样做。另一方面,它证明了以不伤害眼睛的方式绕过库的限制是多么容易。)
Rails
尽管自述文件提到您必须在 Rails
self.include_root_in_json = false
Elasticsearch 的正确映射
现在是我们工作的重点——为我们的文档(模型)定义正确的映射:
mapping do
indexes :title, type: 'string', boost: 10, analyzer: 'snowball'
indexes :created_at, type: 'date'
indexes :chapters do
indexes :text, analyzer: 'snowball'
end
end
注意我们索引title
随着提升,created_at
作为“日期”,以及关联模型中的章节文本。所有数据都被有效地“非规范化”为elasticsearch中的单个文档(如果这个术语有点意义的话)。
正确的文档 JSON 序列化
作为最后一步,我们必须在 elasticsearch 索引中正确序列化文档。请注意我们如何利用便利的to_json
方法来自活动记录:
def to_indexed_json
to_json( include: { chapters: { only: [:text] } } )
end
完成所有这些设置后,我们可以在两个属性中搜索属性Book
和Chapter
我们文档的一部分。
请运行active_record_associations.rb https://gist.github.com/3200212Ruby 文件链接在开头,可以看到完整的图片。
如需了解更多信息,请参阅以下资源:
- https://github.com/karmi/railscasts-episodes/commit/ee1f6f3 https://github.com/karmi/railscasts-episodes/commit/ee1f6f3
- https://github.com/karmi/railscasts-episodes/commit/03c45c3 https://github.com/karmi/railscasts-episodes/commit/03c45c3
- https://github.com/karmi/tire/blob/master/test/models/active_record_models.rb#L10-20 https://github.com/karmi/tire/blob/master/test/models/active_record_models.rb#L10-20
请参阅 StackOverflow 的回答:ElasticSearch 和 Tire:使用映射和 to_indexed_json https://stackoverflow.com/questions/11672072/elasticsearch-tire-using-mapping-and-to-indexed-json/11700251#11700251欲了解更多信息mapping
/ to_indexed_json
相互作用。
请参阅 StackOverflow 的回答:在 ElasticSearch 中索引方法的结果(Tire + ActiveRecord) https://stackoverflow.com/questions/13600086/index-the-results-of-a-method-in-elasticsearch-tire-activerecord/13847929#13847929了解在为具有关联的模型建立索引时如何应对 n+1 查询。