Module: Chewy::Type::Import::ClassMethods

Defined in:
lib/chewy/type/import.rb

Instance Method Summary collapse

Instance Method Details

#bulk(options = {}) ⇒ Object

Wraps elasticsearch-ruby client indices bulk method. Adds ‘:suffix` option to bulk import to index with specified suffix.



67
68
69
70
71
72
73
74
# File 'lib/chewy/type/import.rb', line 67

def bulk options = {}
  suffix = options.delete(:suffix)

  result = client.bulk options.merge(index: index.build_index_name(suffix: suffix), type: type_name)
  Chewy.wait_for_status

  extract_errors result
end

#import(*args) ⇒ Object

Perform import operation for specified documents. Returns true or false depending on success.

UsersIndex::User.import                          # imports default data set
UsersIndex::User.import User.active              # imports active users
UsersIndex::User.import [1, 2, 3]                # imports users with specified ids
UsersIndex::User.import users                    # imports users collection
UsersIndex::User.import refresh: false           # to disable index refreshing after import
UsersIndex::User.import suffix: Time.now.to_i    # imports data to index with specified suffix if such is exists
UsersIndex::User.import batch_size: 300          # import batch size

See adapters documentation for more details.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chewy/type/import.rb', line 20

def import *args
  import_options = args.extract_options!
  bulk_options = import_options.reject { |k, v| ![:refresh, :suffix].include?(k) }.reverse_merge!(refresh: true)

  index.create!(bulk_options.slice(:suffix)) unless index.exists?
  build_root unless self.root_object

  ActiveSupport::Notifications.instrument 'import_objects.chewy', type: self do |payload|
    adapter.import(*args, import_options) do |action_objects|
      indexed_objects = self.root_object.parent_id && fetch_indexed_objects(action_objects.values.flatten)
      body = bulk_body(action_objects, indexed_objects)

      errors = bulk(bulk_options.merge(body: body)) if body.any?

      fill_payload_import payload, action_objects
      fill_payload_errors payload, errors if errors.present?
      !errors.present?
    end
  end
end

#import!(*args) ⇒ Object

Perform import operation for specified documents. Raises Chewy::ImportFailed exception in case of import errors.

UsersIndex::User.import!                          # imports default data set
UsersIndex::User.import! User.active              # imports active users
UsersIndex::User.import! [1, 2, 3]                # imports users with specified ids
UsersIndex::User.import! users                    # imports users collection
UsersIndex::User.import! refresh: false           # to disable index refreshing after import
UsersIndex::User.import! suffix: Time.now.to_i    # imports data to index with specified suffix if such is exists
UsersIndex::User.import! batch_size: 300          # import batch size

See adapters documentation for more details.



54
55
56
57
58
59
60
61
62
63
# File 'lib/chewy/type/import.rb', line 54

def import! *args
  errors = nil
  subscriber = ActiveSupport::Notifications.subscribe('import_objects.chewy') do |*args|
    errors = args.last[:errors]
  end
  import *args
  ActiveSupport::Notifications.unsubscribe(subscriber)
  raise Chewy::ImportFailed.new(self, errors) if errors.present?
  true
end