lib/mebla/context.rb in mebla-1.0.0.rc2 vs lib/mebla/context.rb in mebla-1.0.0
- old
+ new
@@ -1,13 +1,14 @@
-# @private
+# A wrapper for slingshot elastic-search adapter for Mongoid
module Mebla
# Handles indexing and reindexing
class Context
attr_reader :indexed_models, :slingshot_index, :slingshot_index_name
attr_reader :mappings
# @private
+ # Creates a new context object
def initialize
@indexed_models = []
@mappings = {}
@slingshot_index = Slingshot::Index.new(Mebla::Configuration.instance.index)
@slingshot_index_name = Mebla::Configuration.instance.index
@@ -28,13 +29,13 @@
# Deletes and rebuilds the index
# @note Doesn't index the data, use Mebla::Context#reindex_data to rebuild the index and index the data
# @return [nil]
def rebuild_index
# Only rebuild if the index exists
- raise ::Mebla::Errors::MeblaIndexException.new("#{@slingshot_index_name} does not exist !! use #create_index to create the index first.") unless index_exists?
+ raise Mebla::Errors::MeblaIndexException.new("#{@slingshot_index_name} does not exist !! use #create_index to create the index first.") unless index_exists?
- ::Mebla.log("Rebuilding index")
+ Mebla.log("Rebuilding index")
# Delete the index
if drop_index
# Create the index
return build_index
@@ -44,13 +45,13 @@
# Creates and indexes the document
# @note Doesn't index the data, use Mebla::Context#index_data to create the index and index the data
# @return [Boolean] true if operation is successful
def create_index
# Only create the index if it doesn't exist
- raise ::Mebla::Errors::MeblaIndexException.new("#{@slingshot_index_name} already exists !! use #rebuild_index to rebuild the index.") if index_exists?
+ raise Mebla::Errors::MeblaIndexException.new("#{@slingshot_index_name} already exists !! use #rebuild_index to rebuild the index.") if index_exists?
- ::Mebla.log("Creating index")
+ Mebla.log("Creating index")
# Create the index
build_index
end
@@ -58,16 +59,16 @@
# @return [Boolean] true if operation is successful
def drop_index
# Only drop the index if it exists
return true unless index_exists?
- ::Mebla.log("Dropping index: #{self.slingshot_index_name}", :debug)
+ Mebla.log("Dropping index: #{self.slingshot_index_name}", :debug)
# Drop the index
result = @slingshot_index.delete
- ::Mebla.log("Dropped #{self.slingshot_index_name}: #{result.to_s}", :debug)
+ Mebla.log("Dropped #{self.slingshot_index_name}: #{result.to_s}", :debug)
# Check that the index doesn't exist
!index_exists?
end
@@ -84,28 +85,28 @@
# Creates the index and indexes the data for all models or a list of models given
# @param *models a list of symbols each representing a model name to be indexed
# @return [nil]
def index_data(*models)
- if models.empty?
+ if models.nil? || models.empty?
only_index = @indexed_models
else
only_index = models.collect{|m| m.to_s}
end
- ::Mebla.log("Indexing #{only_index.join(", ")}", :debug)
+ Mebla.log("Indexing #{only_index.join(", ")}", :debug)
# Build up a bulk query to save processing and time
bulk_query = ""
# Keep track of indexed documents
indexed_count = {}
# Create the index
if create_index
# Start collecting documents
only_index.each do |model|
- ::Mebla.log("Indexing: #{model}")
+ Mebla.log("Indexing: #{model}")
# Get the class
to_index = model.camelize.constantize
# Get the records
entries = []
@@ -139,71 +140,72 @@
bulk_query << build_bulk_query(@slingshot_index_name, to_index.slingshot_type_name, document.id.to_s, attrs)
end
end
end
else
- raise ::Mebla::Errors::MeblaIndexException.new("Could not create #{@slingshot_index_name}!!!")
- end
+ raise Mebla::Errors::MeblaIndexException.new("Could not create #{@slingshot_index_name}!!!")
+ end
- # Add a new line to the query
- bulk_query << '\n'
+ Mebla.log("Bulk indexing:\n#{bulk_query}", :debug)
- ::Mebla.log("Bulk indexing:\n#{bulk_query}", :debug)
-
# Send the query
response = Slingshot::Configuration.client.post "#{Mebla::Configuration.instance.url}/_bulk", bulk_query
# Only refresh the index if no error ocurred
unless response =~ /error/
# Log results
- ::Mebla.log("Indexed #{only_index.count} model(s) to #{self.slingshot_index_name}: #{response}")
- ::Mebla.log("Indexing Report:")
+ Mebla.log("Indexed #{only_index.count} model(s) to #{self.slingshot_index_name}: #{response}")
+ Mebla.log("Indexing Report:")
indexed_count.each do |model_name, count|
- ::Mebla.log("Indexed #{model_name}: #{count} document(s)")
+ Mebla.log("Indexed #{model_name}: #{count} document(s)")
end
# Refresh the index
refresh_index
else
- raise ::Mebla::Errors::MeblaIndexException.new("Indexing #{only_index.join(", ")} failed with the following response:\n #{response}")
+ raise Mebla::Errors::MeblaIndexException.new("Indexing #{only_index.join(", ")} failed with the following response:\n #{response}")
end
rescue RestClient::Exception => error
- raise ::Mebla::Errors::MeblaIndexException.new("Indexing #{only_index.join(", ")} failed with the following error: #{error.message}")
+ raise Mebla::Errors::MeblaIndexException.new("Indexing #{only_index.join(", ")} failed with the following error: #{error.message}")
end
# Rebuilds the index and indexes the data for all models or a list of models given
# @param *models a list of symbols each representing a model name to rebuild it's index
# @return [nil]
def reindex_data(*models)
- ::Mebla.log("Rendexing: #{self.slingshot_index_name}")
+ Mebla.log("Rendexing: #{self.slingshot_index_name}")
unless drop_index
- raise ::Mebla::Errors::MeblaIndexException.new("Could not drop #{@slingshot_index_name}!!!")
+ raise Mebla::Errors::MeblaIndexException.new("Could not drop #{@slingshot_index_name}!!!")
end
# Create the index and index the data
- index_data(models)
+ if models && !models.empty?
+ index_data(models)
+ else
+ index_data
+ end
end
# Refreshes the index
# @return [nil]
def refresh_index
- ::Mebla.log("Refreshing: #{self.slingshot_index_name}", :debug)
+ Mebla.log("Refreshing: #{self.slingshot_index_name}", :debug)
result = @slingshot_index.refresh
- ::Mebla.log("Refreshed #{self.slingshot_index_name}: #{result}")
+ Mebla.log("Refreshed #{self.slingshot_index_name}: #{result}")
end
private
# Builds the index according to the mappings set
# @return [Boolean] true if the index was created successfully, false otherwise
def build_index
- ::Mebla.log("Building index", :debug)
+ Mebla.log("Building #{self.slingshot_index_name}", :debug)
# Create the index
result = @slingshot_index.create :mappings => @mappings
- ::Mebla.log("Created index: #{result.to_s}")
+ Mebla.log("Created #{self.slingshot_index_name}: #{result.to_s}")
# Check if the index exists
index_exists?
end
\ No newline at end of file