I'm indexing a data set for elasticsearch using Tire and ActiveRecord. I have an Artist model, which has_many :images. How can I index a method of the Artist model which returns a specific image? Or alternatively reference a method of the associated model? My desired Artist result will include the paths for the primary Image associated with the Artist (both the original and the thumbnail).
I've tried this mapping:
mapping do
indexes :id, :index => :not_analyzed
indexes :name
indexes :url
indexes :primary_image_original
indexes :primary_image_thumbnail
end
to reference these Artist methods:
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
This just ignores the indexed methods. Based on other answers like Elasticsearch, Tire, and Nested queries / associations with ActiveRecord, I tried this:
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
But this also doesn't return what I'm after. I've spent several hours googling and reading the elasticsearch and Tire documentation and haven't found a working example of this pattern to follow. Thanks for your ideas!
:as
option -- I don't have time to dig into this more at the moment, unfortunately. – Ostrowskito_indexed_json
? – Ostrowski