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



71
72
73
# File 'lib/sunspot/indexer.rb', line 71

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

def add(model)
  @connection.add(Array(model).map { |m| prepare(m) })
end

#remove(model) ⇒ Object

Remove the given model from the Solr index



28
29
30
# File 'lib/sunspot/indexer.rb', line 28

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.



35
36
37
# File 'lib/sunspot/indexer.rb', line 35

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