Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reindex type automatically. #1643

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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| source.key?(tk) }
source[:type] = document_type(true)
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/inheritance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ 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)

# Now we let the callbacks work
animal = Cat.first
animal.update!(name: "Oliver")

sleep 1

assert_equal ["Oliver"], Animal.search("oliver", load: false).map(&:name)
assert_equal ["cat"], Animal.search("oliver", type: [Cat], 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