diff --git a/lib/searchkick/record_data.rb b/lib/searchkick/record_data.rb index b70a6630..1476f8b9 100644 --- a/lib/searchkick/record_data.rb +++ b/lib/searchkick/record_data.rb @@ -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 diff --git a/test/inheritance_test.rb b/test/inheritance_test.rb index 4cec78ce..de8dd965 100644 --- a/test/inheritance_test.rb +++ b/test/inheritance_test.rb @@ -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 diff --git a/test/models/animal.rb b/test/models/animal.rb index 7e0668e3..ed48f661 100644 --- a/test/models/animal.rb +++ b/test/models/animal.rb @@ -3,4 +3,8 @@ class Animal inheritance: true, text_start: [:name], suggest: [:name] + + def full_name_data + { full_name: "#{name} the #{type}" } + end end