Skip to content

Commit 6f4a57a

Browse files
committed
[MODEL] Added the ability to pass a custom index name to .create_index!, .delete_index! and .refresh_index!
1 parent ec70871 commit 6f4a57a

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+26-8
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,21 @@ def settings(settings={}, &block)
175175
#
176176
# Article.__elasticsearch__.create_index! force: true
177177
#
178+
# @example Pass a specific index name
179+
#
180+
# Article.__elasticsearch__.create_index! index: 'my-index'
181+
#
178182
def create_index!(options={})
179-
delete_index!(options) if options[:force]
183+
target_index = options.delete(:index) || self.index_name
184+
185+
delete_index!(options.merge index: target_index) if options[:force]
180186

181-
unless ( self.client.indices.exists(index: self.index_name) rescue false )
187+
unless ( self.client.indices.exists(index: target_index) rescue false )
182188
begin
183-
self.client.indices.create index: self.index_name,
184-
body: {
185-
settings: self.settings.to_hash,
186-
mappings: self.mappings.to_hash }
189+
self.client.indices.create index: target_index,
190+
body: {
191+
settings: self.settings.to_hash,
192+
mappings: self.mappings.to_hash }
187193
rescue Exception => e
188194
unless e.class.to_s =~ /NotFound/ && options[:force]
189195
STDERR.puts "[!!!] Error when creating the index: #{e.class}", "#{e.message}"
@@ -199,9 +205,15 @@ def create_index!(options={})
199205
#
200206
# Article.__elasticsearch__.delete_index!
201207
#
208+
# @example Pass a specific index name
209+
#
210+
# Article.__elasticsearch__.delete_index! index: 'my-index'
211+
#
202212
def delete_index!(options={})
213+
target_index = options.delete(:index) || self.index_name
214+
203215
begin
204-
self.client.indices.delete index: self.index_name
216+
self.client.indices.delete index: target_index
205217
rescue Exception => e
206218
unless e.class.to_s =~ /NotFound/ && options[:force]
207219
STDERR.puts "[!!!] Error when deleting the index: #{e.class}", "#{e.message}"
@@ -215,11 +227,17 @@ def delete_index!(options={})
215227
#
216228
# Article.__elasticsearch__.refresh_index!
217229
#
230+
# @example Pass a specific index name
231+
#
232+
# Article.__elasticsearch__.refresh_index! index: 'my-index'
233+
#
218234
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
219235
#
220236
def refresh_index!(options={})
237+
target_index = options.delete(:index) || self.index_name
238+
221239
begin
222-
self.client.indices.refresh index: self.index_name
240+
self.client.indices.refresh index: target_index
223241
rescue Exception => e
224242
unless e.class.to_s =~ /NotFound/ && options[:force]
225243
STDERR.puts "[!!!] Error when refreshing the index: #{e.class}", "#{e.message}"

elasticsearch-model/test/unit/indexing_test.rb

+37
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,43 @@ class ::DummyIndexingModelForRecreate
358358
DummyIndexingModelForRecreate.refresh_index!
359359
end
360360
end
361+
362+
context "with a custom index name" do
363+
setup do
364+
@client = stub('client')
365+
@indices = stub('indices')
366+
@client.stubs(:indices).returns(@indices)
367+
DummyIndexingModelForRecreate.expects(:client).returns(@client).at_least_once
368+
end
369+
370+
should "create the custom index" do
371+
@indices.expects(:exists).with do |arguments|
372+
assert_equal 'custom-foo', arguments[:index]
373+
end
374+
375+
@indices.expects(:create).with do |arguments|
376+
assert_equal 'custom-foo', arguments[:index]
377+
end
378+
379+
DummyIndexingModelForRecreate.create_index! index: 'custom-foo'
380+
end
381+
382+
should "delete the custom index" do
383+
@indices.expects(:delete).with do |arguments|
384+
assert_equal 'custom-foo', arguments[:index]
385+
end
386+
387+
DummyIndexingModelForRecreate.delete_index! index: 'custom-foo'
388+
end
389+
390+
should "refresh the custom index" do
391+
@indices.expects(:refresh).with do |arguments|
392+
assert_equal 'custom-foo', arguments[:index]
393+
end
394+
395+
DummyIndexingModelForRecreate.refresh_index! index: 'custom-foo'
396+
end
397+
end
361398
end
362399

363400
end

0 commit comments

Comments
 (0)