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).

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Indexer

Returns a new instance of Indexer.



13
14
15
# File 'lib/sunspot/indexer.rb', line 13

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



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

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

#flush_batchObject

Write batch out to Solr and clear it



81
82
83
# File 'lib/sunspot/indexer.rb', line 81

def flush_batch
  add_documents(batcher.end_current)
end

#remove(*models) ⇒ Object

Remove the given model from the Solr index



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

def remove(*models)
  @connection.delete_by_id(
    models.map { |model| Adapters::InstanceAdapter.adapt(model).index_id }
  )
end

#remove_all(clazz = nil) ⇒ Object

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



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

def remove_all(clazz = nil)
  if clazz
    @connection.delete_by_query("type:#{escape(clazz.name)}")
  else
    @connection.delete_by_query("*:*")
  end
end

#remove_by_id(class_name, *ids) ⇒ Object

Remove the model from the Solr index by specifying the class and ID



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

def remove_by_id(class_name, *ids)
  ids.flatten!
  @connection.delete_by_id(
    ids.map { |id| Adapters::InstanceAdapter.index_id_for(class_name, id) }
  )
end

#remove_by_scope(scope) ⇒ Object

Remove all documents that match the scope given in the Query



67
68
69
# File 'lib/sunspot/indexer.rb', line 67

def remove_by_scope(scope)
  @connection.delete_by_query(scope.to_boolean_phrase)
end

#start_batchObject

Start batch processing



74
75
76
# File 'lib/sunspot/indexer.rb', line 74

def start_batch
  batcher.start_new
end