Method: ElasticsearchRecord::ModelApi#bulk

Defined in:
lib/elasticsearch_record/model_api.rb

#bulk(data, operation = :index, refresh: true, **options) ⇒ Object

bulk handle provided data (single Hash or multiple Array).

Parameters:

  • data (Hash, Array<Hash<Symbol=>Object>>)
    • the data to insert/update/delete ...
  • operation (Symbol) (defaults to: :index)
  • refresh (Boolean, Symbol) (defaults to: true)


267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/elasticsearch_record/model_api.rb', line 267

def bulk(data, operation = :index, refresh: true, **options)
  data = [data] unless data.is_a?(Array)

  _connection.api(:core, :bulk, {
    index:   _index_name,
    body:    if operation == :update
               data.map { |item| { operation => { _id: (item[:_id].presence || item['_id']), data: { doc: item.except(:_id, '_id') } } } }
             else
               data.map { |item| { operation => { _id: (item[:_id].presence || item['_id']), data: item.except(:_id, '_id') } } }
             end,
    refresh: refresh
  }, "BULK #{operation.to_s.upcase}", **options)
end