Sha256: d4608e53b82f646d1d7f37868e4c09a437f59ae0b0a47b2c31e15ddeffbcc2c6

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Sunspot
  # 
  # This class presents a service for adding, updating, and removing data
  # from the Solr index. An Indexer instance is associated with a particular
  # setup, and thus is capable of indexing instances of a certain class (and its
  # subclasses).
  #
  class Indexer #:nodoc:
    def initialize(connection, setup)
      @connection, @setup = connection, setup
    end

    # 
    # Construct a representation of the model for indexing and send it to the
    # connection for indexing
    #
    # ==== Parameters
    #
    # model<Object>:: the model to index
    #
    def add(model)
      hash = static_hash_for(model)
      for field in @setup.all_fields
        hash.merge!(field.pairs_for(model))
      end
      @connection.add(hash)
    end

    # 
    # Remove the given model from the Solr index
    #
    def remove(model)
      @connection.delete(Adapters::InstanceAdapter.adapt(model).index_id)
    end

    # 
    # Delete all documents of the class indexed by this indexer from Solr.
    #
    def remove_all
      @connection.delete_by_query("type:#{@setup.clazz.name}")
    end

    protected

    # 
    # All indexed documents index and store the +id+ and +type+ fields.
    # This method constructs the document hash containing those key-value
    # pairs.
    #
    def static_hash_for(model)
      { :id => Adapters::InstanceAdapter.adapt(model).index_id,
        :type => Util.superclasses_for(model.class).map { |clazz| clazz.name }}
    end

    class <<self
      # 
      # Delete all documents from the Solr index
      #
      # ==== Parameters
      #
      # connection<Solr::Connection>::
      #   connection to which to send the delete request
      def remove_all(connection)
        connection.delete_by_query("type:[* TO *]")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
outoftime-sunspot-0.8.0 lib/sunspot/indexer.rb
outoftime-sunspot-0.8.1 lib/sunspot/indexer.rb
outoftime-sunspot-0.8.2 lib/sunspot/indexer.rb