File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -220,15 +220,19 @@ def load_settings_from_io(settings)
220
220
# Article.__elasticsearch__.create_index! index: 'my-index'
221
221
#
222
222
def create_index! ( options = { } )
223
- target_index = options . delete ( :index ) || self . index_name
223
+ options = options . clone
224
+
225
+ target_index = options . delete ( :index ) || self . index_name
226
+ settings = options . delete ( :settings ) || self . settings . to_hash
227
+ mappings = options . delete ( :mappings ) || self . mappings . to_hash
224
228
225
229
delete_index! ( options . merge index : target_index ) if options [ :force ]
226
230
227
231
unless index_exists? ( index : target_index )
228
232
self . client . indices . create index : target_index ,
229
233
body : {
230
- settings : self . settings . to_hash ,
231
- mappings : self . mappings . to_hash }
234
+ settings : settings ,
235
+ mappings : mappings }
232
236
end
233
237
end
234
238
Original file line number Diff line number Diff line change @@ -528,6 +528,27 @@ class ::DummyIndexingModelForRecreate
528
528
assert_nothing_raised { DummyIndexingModelForRecreate . create_index! }
529
529
end
530
530
531
+ should "get the index settings and mappings from options" do
532
+ client = stub ( 'client' )
533
+ indices = stub ( 'indices' )
534
+ client . stubs ( :indices ) . returns ( indices )
535
+
536
+ indices . expects ( :create ) . with do |payload |
537
+ assert_equal 'foobar' , payload [ :index ]
538
+ assert_equal 3 , payload [ :body ] [ :settings ] [ :index ] [ :number_of_shards ]
539
+ assert_equal 'bar' , payload [ :body ] [ :mappings ] [ :foobar ] [ :properties ] [ :foo ] [ :analyzer ]
540
+ true
541
+ end . returns ( { } )
542
+
543
+ DummyIndexingModelForRecreate . expects ( :index_exists? ) . returns ( false )
544
+ DummyIndexingModelForRecreate . expects ( :client ) . returns ( client ) . at_least_once
545
+
546
+ DummyIndexingModelForRecreate . create_index! \
547
+ index : 'foobar' ,
548
+ settings : { index : { number_of_shards : 3 } } ,
549
+ mappings : { foobar : { properties : { foo : { analyzer : 'bar' } } } }
550
+ end
551
+
531
552
should "not create the index when it exists" do
532
553
client = stub ( 'client' )
533
554
indices = stub ( 'indices' )
You can’t perform that action at this time.
0 commit comments