module Mongoid::Mebla

A wrapper for slingshot elastic-search adapter for Mongoid

Constants

SLINGSHOT_TYPE_MAPPING

Private Instance Methods

add_to_index() click to toggle source

Adds the document to the index @return [Boolean] true if the operation is successful

# File lib/mebla/mongoid/mebla.rb, line 241
def add_to_index      
  return false unless ::Mebla.context.index_exists? # only try to index if the index exists
  return false unless ::Mebla.context.indexed_models.include?(self.class.name)
  
  # Prepare attributes to hash
  to_index_hash = {:id => self.id.to_s}
  
  # If the document is embedded set _parent to the parent's id
  if self.embedded?
    parent_id = self.send(self.class.embedded_parent_foreign_key.to_sym).id.to_s        
    to_index_hash.merge!({ 
      (self.class.embedded_parent_foreign_key + "_id").to_sym => parent_id,
      :_parent => parent_id
    })
  end
  
  # Add indexed fields to the hash
  self.class.search_fields.each do |sfield|
    if self.class.fields[sfield.to_s]
      to_index_hash[sfield] = self.attributes[sfield.to_s]
    else
      to_index_hash[sfield] = self.send(sfield)
    end
  end
  
  # Add indexed relations to the hash
  self.class.search_relations.each do |relation, fields|        
    entries = self.send(relation.to_sym)
    
    next if entries.nil?
    
    if entries.is_a?(Array) || entries.is_a?(Mongoid::Relations::Targets::Enumerable)
      next if entries.empty?
      to_index_hash[relation] = []
      entries.each do |entry|
        if fields.is_a?(Array)
          to_index_hash[relation] << entry.attributes.reject{|key, value| !fields.include?(key.to_sym)}
        else
          to_index_hash[relation] << { fields => entry.attributes[fields.to_s] }
        end
      end
    else          
      to_index_hash[relation] = {}
      if fields.is_a?(Array)
        to_index_hash[relation].merge!(entries.attributes.reject{|key, value| !fields.include?(key.to_sym)})
      else
        to_index_hash[relation].merge!({ fields => entries.attributes[fields.to_s] })
      end
    end
  end      
  
  ::Mebla.log("Indexing #{self.class.slingshot_type_name}: #{to_index_hash.to_s}", :debug)
  
  # Index the data under its correct type
  response = ::Mebla.context.slingshot_index.store(self.class.slingshot_type_name.to_sym, to_index_hash)
  
  ::Mebla.log("Response for indexing #{self.class.slingshot_type_name}: #{response.to_s}", :debug)
  
  # Refresh the index
  ::Mebla.context.refresh_index
  return true
rescue => error
  raise_synchronization_exception(error)
  
  return false
end
raise_synchronization_exception(error) click to toggle source

Raises synchronization exception in either add_to_index or remove_from_index

# File lib/mebla/mongoid/mebla.rb, line 330
def raise_synchronization_exception(error)
  exception_message = "#{self.class.slingshot_type_name} synchronization failed with the following error: #{error.message}"
  if self.class.whiny_indexing
    # Whine when mebla is not able to synchronize
    raise ::Mebla::Errors::MeblaSynchronizationException.new(exception_message)
  else
    # Whining is not allowed, silently log the exception
    ::Mebla.log(exception_message, :warn)
  end
end
remove_from_index() click to toggle source

Deletes the document from the index @return [Boolean] true if the operation is successful

# File lib/mebla/mongoid/mebla.rb, line 310
def remove_from_index
  return false unless ::Mebla.context.index_exists? # only try to index if the index exists
  
  ::Mebla.log("Removing #{self.class.slingshot_type_name} with id: #{self.id.to_s}", :debug)

  # Delete the document
  response = Slingshot::Configuration.client.delete "#{::Mebla::Configuration.instance.url}/#{::Mebla.context.slingshot_index_name}/#{self.class.slingshot_type_name}/#{self.id.to_s}"
  
  ::Mebla.log("Response for removing #{self.class.slingshot_type_name}: #{response.to_s}", :debug)
  
  # Refresh the index
  ::Mebla.context.refresh_index
  return true
rescue => error
  raise_synchronization_exception(error)
  
  return false
end