From 6d7d9e8b18ba610596f044402a0c2b4d5822eb60 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 16 Aug 2014 15:28:37 -0700 Subject: [PATCH] Added stay synced section --- README.md | 70 ++++++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 8e2c2d3..a93f436 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,43 @@ end #### No need to reindex - App starts -- Records are inserted, updated or deleted (syncs automatically) + +### Stay Synced + +There are three strategies for keeping the index synced with your database. + +1. Immediately (default) + + Anytime a record is inserted, updated, or deleted + +2. Asynchronously + + Use background jobs for better performance + + ```ruby + class Product < ActiveRecord::Base + searchkick callbacks: false + + def reindex_async + delay.reindex # delayed job + end + + after_commit :reindex_async + # or for Mongoid + # after_save :reindex_async + # after_destroy :reindex_async + end + ``` + +3. Manually + + Turn off automatic syncing + + ```ruby + class Product < ActiveRecord::Base + searchkick callbacks: false + end + ``` ### Keep Getting Better @@ -742,15 +778,7 @@ class Product < ActiveRecord::Base end ``` -Turn off callbacks permanently - -```ruby -class Product < ActiveRecord::Base - searchkick callbacks: false -end -``` - -or temporarily +Turn off callbacks temporarily ```ruby Product.disable_search_callbacks # or use Searchkick.disable_callbacks for all models @@ -794,28 +822,6 @@ class Product < ActiveRecord::Base end ``` -Asynchronous reindexing - -```ruby -class Product < ActiveRecord::Base - searchkick callbacks: false - - # add the callbacks manually - - # ActiveRecord - one callback - after_commit :reindex_async - - # Mongoid - two callbacks - after_save :reindex_async - after_destroy :reindex_async - - def reindex_async - # delayed job - delay.reindex - end -end -``` - Reindex conditionally **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) -- libgit2 0.21.0