Class: Sunspot::Indexer

Inherits:
Object
  • Object
show all
Includes:
RSolr::Char
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) ⇒ Indexer

Returns a new instance of Indexer.



11
12
13
# File 'lib/sunspot/indexer.rb', line 11

def initialize(connection)
  @connection = connection
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



128
129
130
# File 'lib/sunspot/indexer.rb', line 128

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



23
24
25
26
27
28
29
30
# File 'lib/sunspot/indexer.rb', line 23

def add(model)
  documents = Util.Array(model).map { |m| prepare(m) }
  if @batch.nil?
    add_documents(documents)
  else
    @batch.concat(documents)
  end
end

#flush_batchObject

Write batch out to Solr and clear it



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

def flush_batch
  add_documents(@batch)
  @batch = nil
end

#remove(model) ⇒ Object

Remove the given model from the Solr index



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

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

#remove_all(clazz) ⇒ Object

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



48
49
50
# File 'lib/sunspot/indexer.rb', line 48

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

#remove_by_id(class_name, id) ⇒ Object



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

def remove_by_id(class_name, id)
  @connection.delete_by_id(
    Adapters::InstanceAdapter.index_id_for(class_name, id)
  )
end

#start_batchObject

Start batch processing



55
56
57
# File 'lib/sunspot/indexer.rb', line 55

def start_batch
  @batch = []
end