@@ -168,6 +168,70 @@ def as_indexed_json(options={})
168
168
assert response . results . any? , "Search has not returned results: #{ response . to_a } "
169
169
end
170
170
end
171
+
172
+ context "importing when the model has a default scope" do
173
+ class ::MongoidArticleWithDefaultScope
174
+ include Mongoid ::Document
175
+ include Elasticsearch ::Model
176
+
177
+ default_scope -> { where ( status : 'active' ) }
178
+
179
+ field :id , type : String
180
+ field :title , type : String
181
+ field :status , type : String , default : 'active'
182
+
183
+ attr_accessible :title if respond_to? :attr_accessible
184
+ attr_accessible :status if respond_to? :attr_accessible
185
+
186
+ settings index : { number_of_shards : 1 , number_of_replicas : 0 } do
187
+ mapping do
188
+ indexes :title , type : 'text' , analyzer : 'snowball'
189
+ indexes :status , type : 'text'
190
+ indexes :created_at , type : 'date'
191
+ end
192
+ end
193
+
194
+ def as_indexed_json ( options = { } )
195
+ as_json ( except : [ :id , :_id ] )
196
+ end
197
+ end
198
+
199
+ setup do
200
+ Elasticsearch ::Model ::Adapter . register \
201
+ Elasticsearch ::Model ::Adapter ::Mongoid ,
202
+ lambda { |klass | !!defined? ( ::Mongoid ::Document ) && klass . respond_to? ( :ancestors ) && klass . ancestors . include? ( ::Mongoid ::Document ) }
203
+
204
+ MongoidArticleWithDefaultScope . __elasticsearch__ . create_index! force : true
205
+
206
+ MongoidArticleWithDefaultScope . delete_all
207
+
208
+ MongoidArticleWithDefaultScope . create! title : 'Test'
209
+ MongoidArticleWithDefaultScope . create! title : 'Testing Coding'
210
+ MongoidArticleWithDefaultScope . create! title : 'Coding'
211
+ MongoidArticleWithDefaultScope . create! title : 'Test legacy code' , status : 'removed'
212
+
213
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
214
+ MongoidArticleWithDefaultScope . __elasticsearch__ . client . cluster . health wait_for_status : 'yellow'
215
+ end
216
+
217
+ should "import only documents from the default scope" do
218
+ assert_equal 3 , MongoidArticleWithDefaultScope . count
219
+
220
+ assert_equal 0 , MongoidArticleWithDefaultScope . import
221
+
222
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
223
+ assert_equal 3 , MongoidArticleWithDefaultScope . search ( '*' ) . results . total
224
+ end
225
+
226
+ should "import only documents from a specific query combined with the default scope" do
227
+ assert_equal 3 , MongoidArticleWithDefaultScope . count
228
+
229
+ assert_equal 0 , MongoidArticleWithDefaultScope . import ( query : -> { where ( title : /^Test/ ) } )
230
+
231
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
232
+ assert_equal 2 , MongoidArticleWithDefaultScope . search ( '*' ) . results . total
233
+ end
234
+ end
171
235
end
172
236
173
237
end
0 commit comments