Class: Kithe::Indexable::RecordIndexUpdater
- Inherits:
-
Object
- Object
- Kithe::Indexable::RecordIndexUpdater
- Defined in:
- app/indexing/kithe/indexable/record_index_updater.rb
Overview
The class actually responsible for updating a record to Solr. Normally called from #update_index in a Kithe::Indexable model.
Kithe::Indexable::RecordIndexUpdater.new(model).update_index
#update_index can add or remove the model from Solr index, depending on model state.
The RecordIndexUpdater will determine the correct Traject::Writer to send output to, from local initialize argument, current thread settings (usually set by Kithe::Indexable.index_with), or global settings.
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
record to be sync’d to Solr or other index.
Instance Method Summary collapse
-
#initialize(record, mapper: nil, writer: nil) ⇒ RecordIndexUpdater
constructor
A new instance of RecordIndexUpdater.
-
#mapper ⇒ Object
The Traject::Indexer we’ll use to map the #record into an index representation, by calling #process_with on the indexer.
-
#should_be_in_index? ⇒ Boolean
Is this record supposed to be represented in the solr index?.
-
#update_index ⇒ Object
Sync #record to the (Solr) index.
-
#writer ⇒ Object
The Traject::Writer we’ll send the indexed representation to after mapping it.
Constructor Details
#initialize(record, mapper: nil, writer: nil) ⇒ RecordIndexUpdater
Returns a new instance of RecordIndexUpdater.
33 34 35 36 37 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 33 def initialize(record, mapper:nil, writer:nil) @record = record @writer = writer @mapper = mapper end |
Instance Attribute Details
#record ⇒ Object (readonly)
record to be sync’d to Solr or other index
16 17 18 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 16 def record @record end |
Instance Method Details
#mapper ⇒ Object
The Traject::Indexer we’ll use to map the #record into an index representation, by calling #process_with on the indexer.
If a mapper was passed in #initialize, that’ll be used. Otherwise the one set on the record’s class_attribute ‘kithe_indexable_mapper` will be used.
If no mapper can be found, raises a TypeError.
61 62 63 64 65 66 67 68 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 61 def mapper @mapper ||= begin if record.kithe_indexable_mapper.nil? raise TypeError.new("Can't call update_index without `kithe_indexable_mapper` given for #{record.inspect}") end record.kithe_indexable_mapper end end |
#should_be_in_index? ⇒ Boolean
Is this record supposed to be represented in the solr index?
78 79 80 81 82 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 78 def should_be_in_index? # TODO, add a record should_index? method like searchkick # https://github.com/ankane/searchkick/blob/5d921bc3da69d6105cbc682ea3df6dce389b47dc/lib/searchkick/record_indexer.rb#L44 !record.destroyed? && record.persisted? end |
#update_index ⇒ Object
Sync #record to the (Solr) index. Depending on record state, we may:
-
Add object to index. Run it through the current #mapper, then send it to the current #writer with ‘writer.put`
-
Remove object from index. Call ‘#delete(id)` on the current #writer.
44 45 46 47 48 49 50 51 52 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 44 def update_index if should_be_in_index? mapper.process_with([record]) do |context| writer.put(context) end else writer.delete(record.send(Kithe.indexable_settings.solr_id_value_attribute)) end end |
#writer ⇒ Object
The Traject::Writer we’ll send the indexed representation to after mapping it. Could be an explicit writer passed into #initialize, or a current thread-settings writer, or a new writer created from global settings.
73 74 75 |
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 73 def writer @writer ||= ThreadSettings.current.writer || Kithe.indexable_settings.writer_instance! end |