Elasticsearch-rails: temporarily disable or skip elasticsearch callback

Created on 21 Nov 2016  路  4Comments  路  Source: elastic/elasticsearch-rails

I have a model with include Elasticsearch::Model::Callbacks. I have a task that imports a lot of data -- I'm going to reindex at the end anyway, so would like to skip the elasticsearch callbacks on .save!.
Are there any quick tricks for disabling or skipping callbacks? Something like instance.save(:elasticsearch => false)?

question stale waiting

Most helpful comment

Hi, as @rubyrider mentions, in complex applications, you'd usually implement your own callbacks -- the automatic Callbacks module is basically only a starting point.

But when it comes to your question, there isn't a configuration for disabling callbacks declaratively, and when I started snooping around the code during preparing my answer to your question, it seems like the Callbacks module would have to support additional methods to do something like http://apidock.com/rails/ActiveSupport/Callbacks/ClassMethods/skip_callback.

However, with the automatic or custom callbacks, it would be easy to implement the conditional logic for executing Elasticsearch callbacks, although with some more or less of a workaround:

class ActiveRecord::Base
  cattr_accessor :skip_elasticsearch_callbacks
end

ActiveRecord::Base.skip_elasticsearch_callbacks = true

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks unless skip_elasticsearch_callbacks
end

You can now disable or enable the mixing-in of the callbacks module (again, either the automatic one or custome one) by setting the ActiveRecord::Base.skip_elasticsearch_callbacks attribute.

All 4 comments

I would like to suggest to build your own callbacks for controlling your indexing based on your context instead using the default callbacks.

Hi, as @rubyrider mentions, in complex applications, you'd usually implement your own callbacks -- the automatic Callbacks module is basically only a starting point.

But when it comes to your question, there isn't a configuration for disabling callbacks declaratively, and when I started snooping around the code during preparing my answer to your question, it seems like the Callbacks module would have to support additional methods to do something like http://apidock.com/rails/ActiveSupport/Callbacks/ClassMethods/skip_callback.

However, with the automatic or custom callbacks, it would be easy to implement the conditional logic for executing Elasticsearch callbacks, although with some more or less of a workaround:

class ActiveRecord::Base
  cattr_accessor :skip_elasticsearch_callbacks
end

ActiveRecord::Base.skip_elasticsearch_callbacks = true

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks unless skip_elasticsearch_callbacks
end

You can now disable or enable the mixing-in of the callbacks module (again, either the automatic one or custome one) by setting the ActiveRecord::Base.skip_elasticsearch_callbacks attribute.

Have you tried to use this?

def destroy_without_es(article_id)
  ...
  article = Article.find(article_id)
  ...
  article._commit_callbacks.clear
  article._destroy_callbacks.clear
  article.destroy!
 ...
end

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tastycode picture tastycode  路  5Comments

AaronRustad picture AaronRustad  路  6Comments

fatbeard2 picture fatbeard2  路  8Comments

tian-xiaobo picture tian-xiaobo  路  6Comments

afterclassroom picture afterclassroom  路  7Comments