Module: Elasticsearch::Model::Importing::ClassMethods
- Defined in:
- lib/elasticsearch/model/importing.rb
Instance Method Summary collapse
- #__batch_to_bulk(batch, transform) ⇒ Object
-
#import(options = {}, &block) {|Hash| ... } ⇒ Fixnum
Import all model records into the index.
Instance Method Details
#__batch_to_bulk(batch, transform) ⇒ Object
140 141 142 |
# File 'lib/elasticsearch/model/importing.rb', line 140 def __batch_to_bulk(batch, transform) batch.map { |model| transform.call(model) } end |
#import(options = {}, &block) {|Hash| ... } ⇒ Fixnum
Import all model records into the index
The method will pick up correct strategy based on the ‘Importing` module defined in the corresponding adapter.
Article.import preprocess: enrich
Article.import return: 'errors'
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/elasticsearch/model/importing.rb', line 102 def import(={}, &block) errors = [] refresh = .delete(:refresh) || false target_index = .delete(:index) || index_name target_type = .delete(:type) || document_type transform = .delete(:transform) || __transform return_value = .delete(:return) || 'count' unless transform.respond_to?(:call) raise ArgumentError, "Pass an object responding to `call` as the :transform option, #{transform.class} given" end if .delete(:force) self.create_index! force: true, index: target_index end __find_in_batches() do |batch| response = client.bulk \ index: target_index, type: target_type, body: __batch_to_bulk(batch, transform) yield response if block_given? errors += response['items'].select { |k, v| k.values.first['error'] } end self.refresh_index! if refresh case return_value when 'errors' errors else errors.size end end |