allomov
8/30/2010 - 2:38 PM

example.rb

module MongoAdapter
  class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
    def id
      @instance.id
    end
  end

  class DataAccessor < Sunspot::Adapters::DataAccessor
    def load(id)
      @clazz.find!(id)
    end
    
    def load_all(ids)
      @clazz.all(:id => ids)
    end
    
    def find_all(options)
      if options[:batch_size]
        offset = 0
        record_count = @clazz.count
        while(offset < record_count)
          yield @clazz.all(:limit => options[:batch_size], :skip => offset)
          offset += options[:batch_size]
        end
      else
        yield @clazz.all
      end
    end
  end
end

Sunspot::Adapters::DataAccessor.register(MongoAdapter::DataAccessor, MongoMapper::Document)
Sunspot::Adapters::InstanceAdapter.register(MongoAdapter::InstanceAdapter, MongoMapper::Document)
# gem install sunspot_rails
class Foo
  include MongoMapper::Document
  include Sunspot::Rails::Searchable

  key :title, String

  searchable do
    text :title
  end
end