Class: Kithe::Indexable::RecordIndexUpdater

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(record, mapper: nil, writer: nil) ⇒ RecordIndexUpdater

Returns a new instance of RecordIndexUpdater.

Parameters:

  • record (ActiveRecord::Base)

    The record to be sync’d to index, usually a Kithe::Model

  • mapper (Traject::Indexer) (defaults to: nil)

    Can pass in a custom Traject::Indexer to use to map from source record to index (Solr) document. Any configured ‘writer’ in the indexer is ignored, we decouple Traject indexer from writer. By default it’s nil, meaning we’ll find the indexer to use from current thread settings, or global settings.

  • writer (Traject::Writer) (defaults to: nil)

    Can pass i a custom Traject::Writer which the index representation will be sent to. By default it’s nil, meaning we’ll find the writer to use from current thread settings or global settings.



30
31
32
33
34
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 30

def initialize(record, mapper:nil, writer:nil)
  @record = record
  @writer = writer
  @mapper = mapper
end

Instance Attribute Details

#recordObject (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

#mapperObject

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.



58
59
60
61
62
63
64
65
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 58

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?

Returns:

  • (Boolean)


75
76
77
78
79
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 75

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_indexObject

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.



41
42
43
44
45
46
47
48
49
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 41

def update_index
  if should_be_in_index?
    mapper.process_with([record]) do |context|
      writer.put(context)
    end
  else
    writer.delete(record.id)
  end
end

#writerObject

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.



70
71
72
# File 'app/indexing/kithe/indexable/record_index_updater.rb', line 70

def writer
  @writer ||= ThreadSettings.current.writer  || Kithe.indexable_settings.writer_instance!
end