lib/mebla/context.rb in mebla-1.0.1 vs lib/mebla/context.rb in mebla-1.0.2
- old
+ new
@@ -14,11 +14,11 @@
@slingshot_index_name = Mebla::Configuration.instance.index
end
# @private
# Adds a model to the list of indexed models
- def add_indexed_model(model, mappings = {})
+ def add_indexed_model(model, mappings = {})
model = model.name if model.is_a?(Class)
@indexed_models << model
@indexed_models.uniq!
@indexed_models.sort!
@@ -89,11 +89,11 @@
def index_data(*models)
if models.nil? || models.empty?
only_index = @indexed_models
else
only_index = models.collect{|m| m.to_s}
- end
+ end
Mebla.log("Indexing #{only_index.join(", ")}", :debug)
# Build up a bulk query to save processing and time
bulk_query = ""
@@ -109,28 +109,37 @@
to_index = model.camelize.constantize
# Get the records
entries = []
unless to_index.embedded?
- entries = to_index.all.only(to_index.search_fields)
+ if to_index.sub_class?
+ entries = to_index.any_in(:_type => [to_index.name]).only(to_index.search_fields)
+ else
+ entries = to_index.any_in(:_type => [nil, to_index.name]).only(to_index.search_fields)
+ end
else
parent = to_index.embedded_parent
access_method = to_index.embedded_as
- parent.all.each do |parent_record|
- entries += parent_record.send(access_method.to_sym).all.only(to_index.search_fields)
+ parent.all.each do |parent_record|
+ if to_index.sub_class?
+ entries += parent_record.send(access_method.to_sym).any_in(:_type => [to_index.name]).only(to_index.search_fields)
+ else
+ entries += parent_record.send(access_method.to_sym).any_in(:_type => [nil, to_index.name]).only(to_index.search_fields)
+ end
end
end
# Save the number of entries to be indexed
- indexed_count[model] = entries.count
+ indexed_count[model] = entries.count
# Build the queries for this model
entries.each do |document|
attrs = document.attributes.dup # make sure we dont modify the document it self
attrs["id"] = attrs.delete("_id") # the id is already added in the meta data of the action part of the query
- # only index search fields
+
+ # only index search fields
attrs.select!{|field, value| document.class.search_fields.include?(field.to_sym)}
# If embedded get the parent id
if document.embedded?
parent_id = document.send(document.class.embedded_parent_foreign_key.to_sym).id.to_s
\ No newline at end of file