Skip to content

Commit

Permalink
Do not reindex type automatically.
Browse files Browse the repository at this point in the history
When doing a partial reindex (to index a new field, for example), searchkick was
reindexing `type`  even when not asked to.

This is a problem because maybe you want to reindex `type` with a different value, and after
doing a partial index, you need to call the `reindex` again by type — mainly because searchkick
has overridden the type using the `document_type` method.

Performance is a concern here. Imagine doing that for millions of rows;
it will take more time to finish the reindexing.
  • Loading branch information
vgsantoniazzi committed Aug 1, 2023
1 parent 34be0ea commit bf38908
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/searchkick/record_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def search_data(method_name = nil)
end

if index.options[:inheritance]
if !TYPE_KEYS.any? { |tk| source.key?(tk) }
if !partial_reindex || TYPE_KEYS.any? { |tk| method_name.equal?(tk) }
source[:type] = document_type(true)
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/inheritance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ def test_child_models_option
assert_equal 2, Searchkick.search("bear", models: [Cat, Dog]).size
end

def test_partial_reindex
store_names ["Buddy"], Dog

assert_equal ["Buddy"], Animal.search("budd", type: [Dog], load: false).map(&:name)

# Update the record to change the type to mess with the record
# and do not call the callbacks to avoid reindexing
animal = Dog.first
animal.name = "Charlie"
animal.type = "Cat"
Searchkick.callbacks(false) do
animal.save!
end

# Reindex records to have the `full_name` available
Animal.reindex(:full_name_data)

# full_name works now
assert_equal ["Charlie the Cat"], Animal.search("charlie", load: false).map(&:full_name)

# Does not change the original index (name still Buddy and type still Dog)
assert_equal ["Buddy"], Animal.search("buddy", type: [Dog], load: false).map(&:name)
assert_equal ["dog"], Animal.search("buddy", type: [Dog], load: false).map(&:type)
end


def test_missing_records
store_names ["Bear A"], Cat
store_names ["Bear B"], Dog
Expand Down
4 changes: 4 additions & 0 deletions test/models/animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Animal
inheritance: true,
text_start: [:name],
suggest: [:name]

def full_name_data
{ full_name: "#{name} the #{type}" }
end
end

0 comments on commit bf38908

Please sign in to comment.