Class: Sunspot::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/indexer.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, setup) ⇒ Indexer

:nodoc:



9
10
11
# File 'lib/sunspot/indexer.rb', line 9

def initialize(connection, setup)
  @connection, @setup = connection, setup
end

Class Method Details

.remove_all(connection) ⇒ Object

Delete all documents from the Solr index

Parameters

connection<Solr::Connection>

connection to which to send the delete request



63
64
65
# File 'lib/sunspot/indexer.rb', line 63

def remove_all(connection)
  connection.delete_by_query("type:[* TO *]")
end

Instance Method Details

#add(model) ⇒ Object

Construct a representation of the model for indexing and send it to the connection for indexing

Parameters

model<Object>

the model to index



21
22
23
24
25
26
27
# File 'lib/sunspot/indexer.rb', line 21

def add(model)
  hash = static_hash_for(model)
  for field in @setup.all_fields
    hash.merge!(field.pair_for(model))
  end
  @connection.add(hash)
end

#remove(model) ⇒ Object

Remove the given model from the Solr index



32
33
34
# File 'lib/sunspot/indexer.rb', line 32

def remove(model)
  @connection.delete(Adapters::InstanceAdapter.adapt(model).index_id)
end

#remove_allObject

Delete all documents of the class indexed by this indexer from Solr.



39
40
41
# File 'lib/sunspot/indexer.rb', line 39

def remove_all
  @connection.delete_by_query("type:#{@setup.clazz.name}")
end