【发布时间】:2012-11-16 00:14:06
【问题描述】:
我正在使用 Tire 和 ActiveRecord 为弹性搜索的数据集编制索引。我有一个 Artist 模型,它有_many :images。如何索引返回特定图像的 Artist 模型的方法?或者引用关联模型的方法?我想要的艺术家结果将包括与艺术家关联的主要图像的路径(原始图像和缩略图)。
我试过这个映射:
mapping do
indexes :id, :index => :not_analyzed
indexes :name
indexes :url
indexes :primary_image_original
indexes :primary_image_thumbnail
end
引用这些 Artist 方法:
def primary_image_original
return images.where(:priority => 'primary').first.original
end
def primary_image_thumbnail
return images.where(:priority => 'primary').first.thumbnail_150
end
这只是忽略了索引方法。根据Elasticsearch, Tire, and Nested queries / associations with ActiveRecord 等其他答案,我尝试了这个:
mapping do
indexes :id, :index => :not_analyzed
indexes :name
indexes :url
indexes :images do
indexes :original
indexes :thumbnail_150
indexes :priority
end
end
def to_indexed_json
to_json(include: { images: { only: [:original, :thumbnail_150, :priority] } } )
end
但这也不会返回我所追求的。我花了几个小时在谷歌上搜索并阅读了 elasticsearch 和 Tire 文档,但还没有找到可以遵循的这种模式的工作示例。感谢您的想法!
【问题讨论】:
-
请注意,Artist 模型上的其他索引字段(名称和 url)已按预期使用上述映射进行索引和搜索。
-
您能否尝试使用
:as选项 - 不幸的是,我目前没有时间深入研究。 -
另外,您能否提供一个带有
to_indexed_json输出链接的paste/hastebin/etc? -
感谢您的快速回复,@karmi! (感谢您提供的很棒的宝石!)我能够找出两种方法来索引 Artist 模型的方法。请在此处查看我的馅饼:pastie.org/5456743。但是,我现在看到的问题是这两种方法都将索引时间增加了至少 60 倍。如果没有这些方法,索引一批 1000 条记录只需不到一秒的时间。使用这些方法,索引一批 1000 条记录需要一分钟多的时间。在这种情况下如何加快索引速度?我有几百万条记录要索引。这里有更好的方法吗?再次感谢。
-
用相关方法更新了馅饼:pastie.org/5456766
标签: ruby-on-rails elasticsearch tire