File tree 2 files changed +36
-6
lines changed
2 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -60,20 +60,24 @@ module ClassMethods
60
60
#
61
61
# Article.import refresh: true
62
62
#
63
+ # @example Import the records into a different index/type than the default one
64
+ #
65
+ # Article.import index: 'my-new-index', type: 'my-other-type'
63
66
#
64
67
def import ( options = { } , &block )
65
- errors = 0
68
+ errors = 0
69
+ refresh = options . delete ( :refresh ) || false
70
+ target_index = options . delete ( :index ) || index_name
71
+ target_type = options . delete ( :type ) || document_type
66
72
67
73
if options . delete ( :force )
68
- self . create_index! force : true
74
+ self . create_index! force : true , index : target_index
69
75
end
70
76
71
- refresh = options . delete ( :refresh ) || false
72
-
73
77
__find_in_batches ( options ) do |batch |
74
78
response = client . bulk \
75
- index : index_name ,
76
- type : document_type ,
79
+ index : target_index ,
80
+ type : target_type ,
77
81
body : batch
78
82
79
83
yield response if block_given?
Original file line number Diff line number Diff line change @@ -91,7 +91,33 @@ def importing_mixin
91
91
assert_equal true , options [ :force ]
92
92
end
93
93
94
+ DummyImportingModel . expects ( :index_name ) . returns ( 'foo' )
95
+ DummyImportingModel . expects ( :document_type ) . returns ( 'foo' )
96
+
94
97
DummyImportingModel . import force : true , foo : 'bar'
95
98
end
99
+
100
+ should "allow passing a different index / type" do
101
+ Elasticsearch ::Model ::Adapter . expects ( :from_class )
102
+ . with ( DummyImportingModel )
103
+ . returns ( DummyImportingAdapter )
104
+
105
+ DummyImportingModel . __send__ :include , Elasticsearch ::Model ::Importing
106
+
107
+ client = mock ( 'client' )
108
+
109
+ client
110
+ . expects ( :bulk )
111
+ . with do |options |
112
+ assert_equal 'my-new-index' , options [ :index ]
113
+ assert_equal 'my-other-type' , options [ :type ]
114
+ true
115
+ end
116
+ . returns ( { 'items' => [ { 'index' => { } } ] } )
117
+
118
+ DummyImportingModel . stubs ( :client ) . returns ( client )
119
+
120
+ DummyImportingModel . import index : 'my-new-index' , type : 'my-other-type'
121
+ end
96
122
end
97
123
end
You can’t perform that action at this time.
0 commit comments