Indexes your ActiveRecord models in elasticsearch asynchronously by making use of tire and resque (hence the name: resque + tire = lifesaver). Using lifesaver, you can easily control when or if to reindex your model depending on your context. Lifesaver also provides the ability to traverse ActiveRecord associations to trigger the index updates of related models.
Add this line to your application's Gemfile:
gem 'lifesaver'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lifesaver
Replaces the tire callbacks in your models
class Article < ActiveRecord::Base
include Tire::Model::Search
# Replace the following include with Lifesaver
# include Tire::Model::Callbacks
enqueues_indexing
endYou can decided when or if the index gets updated at all based on your current situation. Lifesaver exposes two methods (supress_indexing, unsuppress_indexing) that set a model's indexing behavior until that model is saved.
class ArticlesController < ApplicationController
def suppressed_update
@article = Article.find(params[:id])
@article.attributes = params[:article]
# No reindexing will occur at all
@article.suppress_indexing
@article.save!
# Not neccessary but if saved
# after this following call,
# this article would reindex
@article.unsuppress_indexing
end
endLifesaver can trigger other models to reindex if you have nested models in your indexes that you would like to update. Use the notifies_for_indexing method to indicate which related models should be marked for indexing. Any associations passed will be both updated when a model is changed (save or destroy) and when another model notifies it. Any associations passed in the options will only notify when the model is changed or notified when specified in the only_on_change or only_on_notify keys, respectively.
class Article < ActiveRecord::Base
belongs_to :author
belongs_to :category
has_many :watchers
has_one :moderator
notifies_for_indexing :author,
only_on_change: :category,
only_on_notify: [:watchers, :moderator]
endYou will see two new queues: lifesaver_indexing and lifesaver_notification
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
- specify which fields will trigger indexing changes
- configuration options
- bulk indexing
- resque-scheduler to provide
delay_indexingandenqueues_indexing after: 30.minutes, on: :save - unsuppress indexing after save
- sidekiq support
- prepare for new elasticsearch library




