```ruby
module Searchable
  class DocumentInstanceAccessor < Sunspot::Adapters::InstanceAdapter
    def id
      @instance.id
    end
  end

  class DocumentDataAccessor < Sunspot::Adapters::DataAccessor
    def load(id)
      Child.get(id)
    end
  end

  def self.included(klass)
    klass.extend ClassMethods
    klass.class_eval do
      after_create :index_record
      after_update :index_record
      after_save :index_record

      def index_record
        begin
          Child.build_solar_schema
          Rails.env.production? ? Sunspot.index(self) : Sunspot.index!(self)
        rescue
          Rails.logger.error "***Problem indexing record for searching, is SOLR running?"
        end
        true
      end
    end
  end

  module ClassMethods
    def sunspot_search(query = "")
      Child.build_solar_schema

      response = Sunspot.search(self) do
        fulltext(query)
        without(:duplicate, true)
        paginate :page => 1, :per_page => 50
        adjust_solr_params do |params|
          params[:defType] = "lucene"
          params[:qf] = nil
        end
      end
      response.results

    end

    def reindex!
      Child.build_solar_schema
      Sunspot.remove_all(self)
      self.all.each { |record| Sunspot.index!(record) }
    end
  end

end
```

FIX - Ashok - Reindex solr if solr is not in sync with couchdb data
user
```ruby
module Searchable
  class DocumentInstanceAccessor < Sunspot::Adapters::InstanceAdapter
    def id
      @instance.id
    end
  end

  class DocumentDataAccessor < Sunspot::Adapters::DataAccessor
    def load(id)
      Child.get(id)
    end
  end

  def self.included(klass)
    klass.extend ClassMethods
    klass.class_eval do
      after_create :index_record
      after_update :index_record
      after_save :index_record

      def index_record
        begin
          Child.build_solar_schema
          Rails.env.production? ? Sunspot.index(self) : Sunspot.index!(self)
        rescue
          Rails.logger.error "***Problem indexing record for searching, is SOLR running?"
        end
        true
      end
    end
  end

  module ClassMethods
    def sunspot_search(query = "")
      Child.build_solar_schema

      begin
        return get_search(query).results
      rescue
        self.reindex!
        Sunspot.commit
        return get_search(query).results
      end

    end

    def reindex!
      Child.build_solar_schema
      Sunspot.remove_all(self)
      self.all.each { |record| Sunspot.index!(record) }
    end

    def get_search(query)
      response = Sunspot.search(self) do
        fulltext(query)
        without(:duplicate, true)
        paginate :page => 1, :per_page => 50
        adjust_solr_params do |params|
          params[:defType] = "lucene"
          params[:qf] = nil
        end
      end
      response
    end
  end

end
```
assistant