I'm using ElasticSearch with Tire to index and search a mongoid model, and I've been searching for the "right" way to index and search an embedded associations. I have a fully functional set up for this model but for some reason i can search for everything except by a field in a embedded model (ResourceTag)
Unnecessary code removed
class Resource
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
include Mongoid::Timestamps
embeds_many :tags, :class_name => "ResourceTag"
accepts_nested_attributes_for :tags, :allow_destroy => true
# Tire
mapping do
indexes :_id
indexes :version, analyzer: 'snowball', boost: 100
indexes :created_at, type: 'date', index: :not_analyzed
indexes :updated_at, type: 'date', index: :not_analyzed
indexes :assignment do
indexes :_id
indexes :name, analyzer: 'snowball', boost: 100
indexes :current_state, analyzer: 'snowball', boost: 100
do
indexes :resource_files do
indexes :_id
indexes :name, analyzer: 'snowball', boost: 100
end
indexes :tags do
indexes :_id
indexes :name, analyzer: 'snowball', boost: 100
end
end
def to_indexed_json
self.to_json(
:include => {
:assignment => {
:methods => :formatted_current_state
},
:resource_files => {
:methods => [:is_video, :is_image]
},
:tags => nil
}
)
end
end
class ResourceTag
include Mongoid::Document
# Attributes
attr_accessible :name
# Fields
field :name, :type => String
# Validations
validates_presence_of :name
validates_uniqueness_of :name, :case_sensitive => false
# Relations
embedded_in :resource
end
i'm not really sure what is wrong with my code but i'm sure i got it working before and now it screwed up :s
i was looking for an example of tire with mongoid and embedded documents but no luck and i'm runnign out of ideas.
Thanks for any ideas and contributions.