Commit 6d7d9e8b18ba610596f044402a0c2b4d5822eb60
1 parent
49427b5d
Exists in
master
and in
21 other branches
Added stay synced section
Showing
1 changed file
with
38 additions
and
32 deletions
Show diff stats
README.md
... | ... | @@ -303,7 +303,43 @@ end |
303 | 303 | #### No need to reindex |
304 | 304 | |
305 | 305 | - App starts |
306 | -- Records are inserted, updated or deleted (syncs automatically) | |
306 | + | |
307 | +### Stay Synced | |
308 | + | |
309 | +There are three strategies for keeping the index synced with your database. | |
310 | + | |
311 | +1. Immediately (default) | |
312 | + | |
313 | + Anytime a record is inserted, updated, or deleted | |
314 | + | |
315 | +2. Asynchronously | |
316 | + | |
317 | + Use background jobs for better performance | |
318 | + | |
319 | + ```ruby | |
320 | + class Product < ActiveRecord::Base | |
321 | + searchkick callbacks: false | |
322 | + | |
323 | + def reindex_async | |
324 | + delay.reindex # delayed job | |
325 | + end | |
326 | + | |
327 | + after_commit :reindex_async | |
328 | + # or for Mongoid | |
329 | + # after_save :reindex_async | |
330 | + # after_destroy :reindex_async | |
331 | + end | |
332 | + ``` | |
333 | + | |
334 | +3. Manually | |
335 | + | |
336 | + Turn off automatic syncing | |
337 | + | |
338 | + ```ruby | |
339 | + class Product < ActiveRecord::Base | |
340 | + searchkick callbacks: false | |
341 | + end | |
342 | + ``` | |
307 | 343 | |
308 | 344 | ### Keep Getting Better |
309 | 345 | |
... | ... | @@ -742,15 +778,7 @@ class Product < ActiveRecord::Base |
742 | 778 | end |
743 | 779 | ``` |
744 | 780 | |
745 | -Turn off callbacks permanently | |
746 | - | |
747 | -```ruby | |
748 | -class Product < ActiveRecord::Base | |
749 | - searchkick callbacks: false | |
750 | -end | |
751 | -``` | |
752 | - | |
753 | -or temporarily | |
781 | +Turn off callbacks temporarily | |
754 | 782 | |
755 | 783 | ```ruby |
756 | 784 | Product.disable_search_callbacks # or use Searchkick.disable_callbacks for all models |
... | ... | @@ -794,28 +822,6 @@ class Product < ActiveRecord::Base |
794 | 822 | end |
795 | 823 | ``` |
796 | 824 | |
797 | -Asynchronous reindexing | |
798 | - | |
799 | -```ruby | |
800 | -class Product < ActiveRecord::Base | |
801 | - searchkick callbacks: false | |
802 | - | |
803 | - # add the callbacks manually | |
804 | - | |
805 | - # ActiveRecord - one callback | |
806 | - after_commit :reindex_async | |
807 | - | |
808 | - # Mongoid - two callbacks | |
809 | - after_save :reindex_async | |
810 | - after_destroy :reindex_async | |
811 | - | |
812 | - def reindex_async | |
813 | - # delayed job | |
814 | - delay.reindex | |
815 | - end | |
816 | -end | |
817 | -``` | |
818 | - | |
819 | 825 | Reindex conditionally |
820 | 826 | |
821 | 827 | **Note:** With ActiveRecord, use this feature with caution - [transaction rollbacks can cause data inconstencies](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/README.md#custom-callbacks) | ... | ... |