Skip to content

Commit

Permalink
allow customization of solr indexing batching mode batch size
Browse files Browse the repository at this point in the history
  • Loading branch information
jrochkind committed Dec 2, 2021
1 parent 21dc1e6 commit e37b939
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Added

*
* Allow customization of Solr indexing `batching` mode batch size.

*

Expand Down
5 changes: 3 additions & 2 deletions app/indexing/kithe/indexable/thread_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions guides/solr_indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
7 changes: 5 additions & 2 deletions lib/kithe/indexable_settings.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 37 additions & 0 deletions spec/indexing/indexable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e37b939

Please sign in to comment.