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



76
77
78
# File 'lib/sunspot/indexer.rb', line 76

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
28
# File 'lib/sunspot/indexer.rb', line 21

def add(model)
  if model.is_a?(Array)
    docs = model.map { |m| prepare(m) }
  else
    docs = [prepare(model)]
  end
  @connection.add(docs)
end

#remove(model) ⇒ Object

Remove the given model from the Solr index



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

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.



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

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