Class: Elastictastic::BulkPersistenceStrategy
- Inherits:
-
Object
- Object
- Elastictastic::BulkPersistenceStrategy
- Defined in:
- lib/elastictastic/bulk_persistence_strategy.rb
Instance Method Summary collapse
- #create(instance, params = {}) ⇒ Object
- #destroy(instance) ⇒ Object
- #flush ⇒ Object
-
#initialize ⇒ BulkPersistenceStrategy
constructor
A new instance of BulkPersistenceStrategy.
- #update(instance) ⇒ Object
Constructor Details
#initialize ⇒ BulkPersistenceStrategy
Returns a new instance of BulkPersistenceStrategy.
5 6 7 8 |
# File 'lib/elastictastic/bulk_persistence_strategy.rb', line 5 def initialize @buffer = StringIO.new @handlers = [] end |
Instance Method Details
#create(instance, params = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/elastictastic/bulk_persistence_strategy.rb', line 10 def create(instance, params = {}) if instance.pending_save? raise Elastictastic::OperationNotAllowed, "Can't re-save transient document with pending save in bulk operation" end instance.pending_save! add( { 'create' => bulk_identifier(instance) }, instance.elasticsearch_doc ) do |response| instance.id = response['create']['_id'] instance.persisted! end end |
#destroy(instance) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/elastictastic/bulk_persistence_strategy.rb', line 33 def destroy(instance) instance.pending_destroy! add(:delete => bulk_identifier(instance)) do |response| instance.transient! end end |
#flush ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/elastictastic/bulk_persistence_strategy.rb', line 40 def flush return if @buffer.length.zero? params = {} params[:refresh] = true if Elastictastic.config.auto_refresh response = Elastictastic.client.bulk(@buffer.string, params) response['items'].each_with_index do |op_response, i| handler = @handlers[i] handler.call(op_response) if handler end response end |
#update(instance) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/elastictastic/bulk_persistence_strategy.rb', line 25 def update(instance) instance.pending_save! add( { 'index' => bulk_identifier(instance) }, instance.elasticsearch_doc ) end |