diff --git a/CHANGES.md b/CHANGES.md index e6c8fd5..d9b665d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,7 @@ ### Added -* +* Allow customization of Solr indexing `batching` mode batch size. * diff --git a/app/indexing/kithe/indexable/thread_settings.rb b/app/indexing/kithe/indexable/thread_settings.rb index 1d34263..81a315c 100644 --- a/app/indexing/kithe/indexable/thread_settings.rb +++ b/app/indexing/kithe/indexable/thread_settings.rb @@ -57,7 +57,7 @@ def self.current def initialize(batching:, disable_callbacks:, original_settings:, writer:, on_finish:) @original_settings = original_settings - @batching = !!batching + @batching = batching @disable_callbacks = disable_callbacks @on_finish = on_finish @@ -80,8 +80,9 @@ def initialize(batching:, disable_callbacks:, original_settings:, def writer @writer ||= begin if @batching + batch_size = (@batching == true) ? Kithe.indexable_settings.batching_mode_batch_size : @batching @local_writer = true - Kithe.indexable_settings.writer_instance!("solr_writer.batch_size" => 100) + Kithe.indexable_settings.writer_instance!("solr_writer.batch_size" => batch_size) end end end diff --git a/guides/solr_indexing.md b/guides/solr_indexing.md index 8447717..74a8bfc 100644 --- a/guides/solr_indexing.md +++ b/guides/solr_indexing.md @@ -97,6 +97,19 @@ Saving or destroying will still automatically trigger a solr sync, but all of th Note that `index_with` is implemented in terms of Thread.current, so it's batching settings apply to everything in the block, but do not automatically apply to any new threads you might create in the block. +By default, we batch 100 records. You can change this globally: + +```ruby +Kithe.indexable_settings.batching_mode_batch_size = 20 +``` + +Or on an individual call to index_with: + +```ruby +Kithe::Indexable.index_with(batching: 20) do + #... +``` + ### Batch every controller? Would you like to have every controller in your app batch solr index updates within each action? You can! diff --git a/lib/kithe/indexable_settings.rb b/lib/kithe/indexable_settings.rb index 65f2a0a..37a305b 100644 --- a/lib/kithe/indexable_settings.rb +++ b/lib/kithe/indexable_settings.rb @@ -1,14 +1,17 @@ module Kithe class IndexableSettings attr_accessor :solr_url, :writer_class_name, :writer_settings, - :model_name_solr_field, :solr_id_value_attribute, :disable_callbacks + :model_name_solr_field, :solr_id_value_attribute, :disable_callbacks, + :batching_mode_batch_size def initialize(solr_url:, writer_class_name:, writer_settings:, - model_name_solr_field:, solr_id_value_attribute:, disable_callbacks: false) + model_name_solr_field:, solr_id_value_attribute:, disable_callbacks: false, + batching_mode_batch_size: 100) @solr_url = solr_url @writer_class_name = writer_class_name @writer_settings = writer_settings @model_name_solr_field = model_name_solr_field @solr_id_value_attribute = solr_id_value_attribute || 'id' + @batching_mode_batch_size = batching_mode_batch_size end # Use configured solr_url, and merge together with configured diff --git a/spec/indexing/indexable_spec.rb b/spec/indexing/indexable_spec.rb index 1c40a13..bec6c27 100644 --- a/spec/indexing/indexable_spec.rb +++ b/spec/indexing/indexable_spec.rb @@ -170,6 +170,43 @@ Kithe::Indexable.index_with(batching: true, on_finish: ->(w){ raise "should not be called" }) do end end + + describe "custom batching_mode_batch_size" do + let(:custom_size) { 20 } + + it "can be set globally" do + stub_request(:post, @solr_update_url) + + # set custom setting + expect(Kithe.indexable_settings).to receive(:batching_mode_batch_size).and_return(custom_size) + + # expect custom setting to be passed to index writer + expect(Kithe.indexable_settings.writer_class_name.constantize).to receive(:new). + with(hash_including("solr_writer.batch_size"=> custom_size)). + and_call_original + + # exersize + Kithe::Indexable.index_with(batching: true) do + TestWork.create!(title: "test1") + TestWork.create!(title: "test2") + end + end + + it "can be set per-call" do + stub_request(:post, @solr_update_url) + + # expect custom setting to be passed to index writer + expect(Kithe.indexable_settings.writer_class_name.constantize).to receive(:new). + with(hash_including("solr_writer.batch_size"=> custom_size)). + and_call_original + + # exersize + Kithe::Indexable.index_with(batching: custom_size) do + TestWork.create!(title: "test1") + TestWork.create!(title: "test2") + end + end + end end describe "auto_callbacks" do