Skip to content

Instantly share code, notes, and snippets.

@rubish
Created June 9, 2011 19:50
Show Gist options
  • Save rubish/1017556 to your computer and use it in GitHub Desktop.
Save rubish/1017556 to your computer and use it in GitHub Desktop.
Getting escargot working with mongoid
# add this line just after the line reading:
# Bundler.require(:default, Rails.env) if defined?(Bundler)
require File.expand_path('../../lib/gem_customizations/escargot_ext.rb', __FILE__)
# desc "Explaining what the task does"
# task :elastic_rails do
# # Task goes here
# end
namespace :escargot do
desc "indexes the models"
task :index, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
puts "Indexing #{model}"
Escargot::LocalIndexing.create_index_for_model(model)
end
end
desc "indexes the models"
task :distributed_index, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
puts "Indexing #{model}"
Escargot::DistributedIndexing.create_index_for_model(model)
end
end
desc "prunes old index versions for this models"
task :prune_versions, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
$elastic_search_client.prune_index_versions(model.index_name)
end
end
task :load_all_models do
models = defined?(ActiveRecord::Base) ? ActiveRecord::Base.send(:subclasses) : []
Dir["#{Rails.root}/app/models/*.rb", "#{Rails.root}/app/models/*/*.rb"].each do |file|
model = File.basename(file, ".*").classify
unless models.include?(model)
require file
end
models << model
end
end
private
def each_indexed_model(args)
if args[:models]
models = args[:models].split(",").map { |m| m.classify.constantize }
else
models = Escargot.indexed_models
end
models.each { |m| yield m }
end
end
module Mongoid
module Document
module ClassMethods
def find_in_batches size=nil
limit = size || 1000
skip = 0
objects = self.limit(limit).skip(skip*limit)
t = Time.new
while objects.any?
yield objects
skip+=1
Rails.logger.debug("processed #{skip*limit} records in #{Time.new - t} seconds") if Rails.logger.debug?
break if objects.size < limit
objects = self.limit(limit).skip(skip*limit)
end
end
def table_exists?
!embedded?
end
end
end
end
ActiveRecord::Base.class_eval do
include Escargot::ActiveRecordExtensions
end if defined?(ActiveRecord::Base)
ElasticSearch::Api::Hit.class_eval do
include Escargot::HitExtensions
end
ElasticSearch::Client.class_eval do
include Escargot::AdminIndexVersions
end
unless File.exists?(File.join(Rails.root, "/config/elasticsearch.yml"))
Rails.logger.warn "No config/elasticsearch.yaml file found, connecting to localhost:9200"
$elastic_search_client = ElasticSearch.new("localhost:9200")
else
config = YAML.load_file(File.join(Rails.root, "/config/elasticsearch.yml"))
$elastic_search_client = ElasticSearch.new(config["host"] + ":" + config["port"].to_s, :timeout => 20)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment