Module: Kithe::Indexable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Model
- Defined in:
- app/indexing/kithe/indexable.rb,
app/indexing/kithe/indexable/thread_settings.rb,
app/indexing/kithe/indexable/record_index_updater.rb
Overview
Kithe::Indexable is a module that can add sync’ing to Solr (or maybe other index) to a model.
While it is currently only tested with Kithe::Models, it doesn’t have any Kithe::Model-specific code, and should work with any ActiveRecord model class, with ‘include Kithe::Indexable`.
For a complete overview, see the [Guide Documentation](../../../guides/solr_indexing.md)
The Solr instance to send updates to is global configuration:
Kithe.indexable_settings.solr_url = "http://localhost:8983/solr/collection_name"
To configure how a model is mapped to a Solr document, you create a ‘Kithe::Indexer` sub-class, which can use our obj_extract method, as well as any other traject indexer code.
“‘ruby class WorkIndexer < KitheIndexer
to_field "additional_title_ssim", obj_extract("additional_titles")
to_field "author_names_ssim", obj_extract("authors"), transform(->(auth) { "#{auth.lastname} #{auth.firstname}" })
end
Then you specify *an instance* as the indexer to use for mapping in your model class:
“‘ruby class Work < Kithe::Work
self.kithe_indexable_mapper = WorkIndexer.new
end “‘
Now by default every time you save or destroy a Work object, it will be sync’d to Solr.
For efficiency, if you’re going to be making a bunch of model saves, you will want to have them batched when sent to Solr:
“‘ruby Kithe::Indexable.index_with(batching: true) do
SomeModel.transaction do
some_model.save
other_model.save
end
end
You don’t need to use an ActiveRecord transaction, but if you do it should be inside the ‘index_with` block.
To force a sync to solr, you can call ‘model.update_index` on any Kithe::Indexable model.
There are also various ways to disable the automatic indexing callbacks, and other customizations. See the [Solr Indexing Guide](../../../guides/solr_indexing.md)
Defined Under Namespace
Classes: RecordIndexUpdater, ThreadSettings
Class Method Summary collapse
-
.auto_callbacks?(model) ⇒ Boolean
Are automatic after_commit callbacks currently enabled? Will check a number of things to see, as we have a number of places these can be turned on/off.
-
.index_with(batching: false, disable_callbacks: false, writer: nil, on_finish: nil) ⇒ Object
Set some indexing parameters for the block yielded.
Instance Method Summary collapse
-
#update_index(mapper: kithe_indexable_mapper, writer: nil) ⇒ Object
Update the Solr index for this object – may remove it from index or add it to index depending on state.
Class Method Details
.auto_callbacks?(model) ⇒ Boolean
Are automatic after_commit callbacks currently enabled? Will check a number of things to see, as we have a number of places these can be turned on/off.
-
Globally in ‘Kithe.indexable_settings.disable_callback`
-
On class or instance using class_attribute ‘kithe_indexable_auto_callbacks`
-
If no kithe_indexable_mapper is configured on record, then no callbacks.
-
Using thread-current settings usually set by .index_with
85 86 87 88 89 90 |
# File 'app/indexing/kithe/indexable.rb', line 85 def self.auto_callbacks?(model) !Kithe.indexable_settings.disable_callbacks && model.kithe_indexable_auto_callbacks && model.kithe_indexable_mapper && !ThreadSettings.current.disabled_callbacks? end |
.index_with(batching: false, disable_callbacks: false, writer: nil, on_finish: nil) ⇒ Object
Set some indexing parameters for the block yielded. For instance, to batch updates:
Kithe::Indexable.index_with(batching: true)
lots_of_records.each(&:update_index)
end
And they will use a batching Traject writer for much more efficiency.
Also pass in custom writer or mapper to #update_index
If using ActiveRecord transactions, ‘.transaction do` should be INSIDE `index_with`, not outside.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/indexing/kithe/indexable.rb', line 67 def self.index_with(batching: false, disable_callbacks: false, writer: nil, on_finish: nil) settings = ThreadSettings.push( batching: batching, disable_callbacks: disable_callbacks, writer: writer, on_finish: on_finish) yield settings ensure settings.pop if settings end |
Instance Method Details
#update_index(mapper: kithe_indexable_mapper, writer: nil) ⇒ Object
Update the Solr index for this object – may remove it from index or add it to index depending on state.
Will use the configured kithe_indexable_mapper by default, or you can pass one in.
By default will use a per-update writer, or thread/block-specific writer configured with ‘self.index_with`, or you can pass one in.
123 124 125 |
# File 'app/indexing/kithe/indexable.rb', line 123 def update_index(mapper: kithe_indexable_mapper, writer:nil) RecordIndexUpdater.new(self, mapper: mapper, writer: writer).update_index end |