Class: Sunspot::Indexer
- Inherits:
-
Object
- Object
- Sunspot::Indexer
- 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
-
#add(model) ⇒ Object
Construct a representation of the model for indexing and send it to the connection for indexing.
-
#add_atomic_update(clazz, updates = {}) ⇒ Object
Construct a representation of the given class instances for atomic properties update and send it to the connection for indexing.
-
#flush_batch ⇒ Object
Write batch out to Solr and clear it.
-
#initialize(connection) ⇒ Indexer
constructor
:nodoc:.
-
#remove(*models) ⇒ Object
Remove the given model from the Solr index.
-
#remove_all(clazz = nil) ⇒ Object
Delete all documents of the class indexed by this indexer from Solr.
-
#remove_by_id(class_name, *ids) ⇒ Object
Remove the model from the Solr index by specifying the class and ID.
-
#remove_by_scope(scope) ⇒ Object
Remove all documents that match the scope given in the Query.
-
#start_batch ⇒ Object
Start batch processing.
Constructor Details
#initialize(connection) ⇒ Indexer
:nodoc:
12 13 14 |
# File 'lib/sunspot/indexer.rb', line 12 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
24 25 26 27 |
# File 'lib/sunspot/indexer.rb', line 24 def add(model) documents = Util.Array(model).map { |m| prepare_full_update(m) } add_batch_documents(documents) end |
#add_atomic_update(clazz, updates = {}) ⇒ Object
Construct a representation of the given class instances for atomic properties update and send it to the connection for indexing
Parameters
- clazz<Class>
-
the class of the models to be updated
- updates<Hash>
-
hash of updates where keys are model ids and values are hash with property name/values to be updated
39 40 41 42 |
# File 'lib/sunspot/indexer.rb', line 39 def add_atomic_update(clazz, updates={}) documents = updates.map { |id, m| prepare_atomic_update(clazz, id, m) } add_batch_documents(documents) end |
#flush_batch ⇒ Object
Write batch out to Solr and clear it
106 107 108 |
# File 'lib/sunspot/indexer.rb', line 106 def flush_batch add_documents(batcher.end_current) end |
#remove(*models) ⇒ Object
Remove the given model from the Solr index
47 48 49 50 51 |
# File 'lib/sunspot/indexer.rb', line 47 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.
81 82 83 84 85 86 87 |
# File 'lib/sunspot/indexer.rb', line 81 def remove_all(clazz = nil) if clazz @connection.delete_by_query("type:#{Util.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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sunspot/indexer.rb', line 56 def remove_by_id(class_name, *ids) if class_name.is_a?(String) and class_name.index("!") partition = class_name.rpartition("!") id_prefix = partition[0..1].join class_name = partition[2] else clazz_setup = setup_for_class(Util.full_const_get(class_name)) id_prefix = if clazz_setup.id_prefix_defined? if clazz_setup.id_prefix_requires_instance? warn(Sunspot::RemoveByIdNotSupportCompositeIdMessage.call(class_name)) else clazz_setup.id_prefix_for_class end end end ids.flatten! @connection.delete_by_id( ids.map { |id| Adapters::InstanceAdapter.index_id_for("#{id_prefix}#{class_name}", id) } ) end |
#remove_by_scope(scope) ⇒ Object
Remove all documents that match the scope given in the Query
92 93 94 |
# File 'lib/sunspot/indexer.rb', line 92 def remove_by_scope(scope) @connection.delete_by_query(scope.to_boolean_phrase) end |
#start_batch ⇒ Object
Start batch processing
99 100 101 |
# File 'lib/sunspot/indexer.rb', line 99 def start_batch batcher.start_new end |