forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporting.rb
51 lines (44 loc) · 1.41 KB
/
importing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Elasticsearch
module Model
# This module provides the support for easily and efficiently importing
# all the records from the including class into the index.
#
module Importing
module ClassMethods
def self.included(base)
adapter = Adapter.from_class(base)
base.__send__ :include, adapter.importing_mixin
end
# Import all model records into the index
#
# The method will pick up correct strategy based on the `Importing` module
# defined in the corresponding adapter.
#
# @example Import all records into the index
#
# Article.import
#
# @example Set the batch size to 100
#
# Article.import(batch_size: 100)
#
# @example Process the response from Elasticsearch
#
# Article.import do |response|
# puts "Got " + response['items'].select { |i| i['index']['error'] }.size.to_s + " errors"
# end
#
def import(options={}, &block)
__find_in_batches(options) do |batch|
response = client.bulk \
index: index_name,
type: document_type,
body: batch,
refresh: options[:refresh]
yield response if block_given?
end
end
end
end
end
end