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



118
119
120
# File 'lib/sunspot/indexer.rb', line 118

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 = Array(model).map { |m| prepare(m) }
  if @batch.nil?
    add_documents(documents)
  else
    @batch.concat(documents)
  end
end

#flush_batchObject



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

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



52
53
54
# File 'lib/sunspot/indexer.rb', line 52

def start_batch
  @batch = []
end