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

Allow to skip reindex #1584

Open
wants to merge 2 commits 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2145,10 +2145,11 @@ Reindex conditionally

```ruby
class Product < ApplicationRecord
searchkick callbacks: false
searchkick

# add the callbacks manually
after_commit :reindex, if: -> (model) { model.previous_changes.key?("name") } # use your own condition
def skip_searchkick_reindex?
persisted? && saved_changes.keys == ['updated_at']
end
end
```

Expand Down
10 changes: 7 additions & 3 deletions lib/searchkick/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def search_data
def should_index?
true
end unless base.method_defined?(:should_index?)

def skip_searchkick_reindex?
false
end
end

class_eval do
Expand Down Expand Up @@ -99,10 +103,10 @@ def searchkick_index_name
# always add callbacks, even when callbacks is false
# so Model.callbacks block can be used
if respond_to?(:after_commit)
after_commit :reindex, if: -> { Searchkick.callbacks?(default: callbacks) }
after_commit :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? }
elsif respond_to?(:after_save)
after_save :reindex, if: -> { Searchkick.callbacks?(default: callbacks) }
after_destroy :reindex, if: -> { Searchkick.callbacks?(default: callbacks) }
after_save :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? }
after_destroy :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? }
end
end
end
Expand Down