Skip to content

On #import, raise an error if the index does not exists. #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions elasticsearch-model/lib/elasticsearch/model/importing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def import(options={}, &block)

if options.delete(:force)
self.create_index! force: true, index: target_index
elsif !self.index_exists? index: target_index
raise ArgumentError,
"#{target_index} does not exist to be imported into. Use create_index! or the :force option to create it."
end

__find_in_batches(options) do |batch|
Expand Down
18 changes: 17 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,30 @@ def create_index!(options={})

delete_index!(options.merge index: target_index) if options[:force]

unless ( self.client.indices.exists(index: target_index) rescue false )
unless index_exists?(index: target_index)
self.client.indices.create index: target_index,
body: {
settings: self.settings.to_hash,
mappings: self.mappings.to_hash }
end
end

# Returns true if the index exists
#
# @example Check whether the model's index exists
#
# Article.__elasticsearch__.index_exists?
#
# @example Check whether a specific index exists
#
# Article.__elasticsearch__.index_exists? index: 'my-index'
#
def index_exists?(options={})
target_index = options[:index] || self.index_name

self.client.indices.exists(index: target_index) rescue false
end

# Deletes the index with corresponding name
#
# @example Delete the index for the `Article` model
Expand Down
50 changes: 38 additions & 12 deletions elasticsearch-model/test/unit/importing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def importing_mixin
DummyImportingModel.expects(:client).returns(client)
DummyImportingModel.expects(:index_name).returns('foo')
DummyImportingModel.expects(:document_type).returns('foo')
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.stubs(:__batch_to_bulk)
assert_equal 0, DummyImportingModel.import
end
Expand All @@ -61,6 +62,7 @@ def importing_mixin
DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_name).returns('foo')
DummyImportingModel.stubs(:document_type).returns('foo')
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.stubs(:__batch_to_bulk)

assert_equal 1, DummyImportingModel.import
Expand All @@ -79,6 +81,7 @@ def importing_mixin
DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_name).returns('foo')
DummyImportingModel.stubs(:document_type).returns('foo')
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.stubs(:__batch_to_bulk)

assert_equal [{'index' => {'error' => 'FAILED'}}], DummyImportingModel.import(return: 'errors')
Expand All @@ -97,29 +100,49 @@ def importing_mixin
DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_name).returns('foo')
DummyImportingModel.stubs(:document_type).returns('foo')
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.stubs(:__batch_to_bulk)

DummyImportingModel.import do |response|
assert_equal 2, response['items'].size
end
end

should "delete and create the index with the force option" do
DummyImportingModel.expects(:__find_in_batches).with do |options|
assert_equal 'bar', options[:foo]
assert_nil options[:force]
true
end
context "when the index does not exist" do
should "raise" do
Elasticsearch::Model::Adapter.expects(:from_class)
.with(DummyImportingModel)
.returns(DummyImportingAdapter)

DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing

DummyImportingModel.expects(:create_index!).with do |options|
assert_equal true, options[:force]
true
DummyImportingModel.expects(:index_name).returns('foo')
DummyImportingModel.expects(:document_type).returns('foo')
DummyImportingModel.expects(:index_exists?).returns(false)
assert_raise ArgumentError do
DummyImportingModel.import
end
end
end

DummyImportingModel.expects(:index_name).returns('foo')
DummyImportingModel.expects(:document_type).returns('foo')
context "with the force option" do
should "delete and create the index" do
DummyImportingModel.expects(:__find_in_batches).with do |options|
assert_equal 'bar', options[:foo]
assert_nil options[:force]
true
end

DummyImportingModel.expects(:create_index!).with do |options|
assert_equal true, options[:force]
true
end

DummyImportingModel.import force: true, foo: 'bar'
DummyImportingModel.expects(:index_name).returns('foo')
DummyImportingModel.expects(:document_type).returns('foo')

DummyImportingModel.import force: true, foo: 'bar'
end
end

should "allow passing a different index / type" do
Expand All @@ -141,6 +164,7 @@ def importing_mixin
.returns({'items' => [ {'index' => {} }]})

DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.stubs(:__batch_to_bulk)

DummyImportingModel.import index: 'my-new-index', type: 'my-other-type'
Expand All @@ -151,6 +175,7 @@ def importing_mixin
transform = lambda {|a|}

DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.expects(:__transform).returns(transform)
DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)

Expand All @@ -162,6 +187,7 @@ def importing_mixin
transform = lambda {|a|}

DummyImportingModel.stubs(:client).returns(client)
DummyImportingModel.stubs(:index_exists?).returns(true)
DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)

DummyImportingModel.import index: 'foo', type: 'bar', transform: transform
Expand Down
68 changes: 57 additions & 11 deletions elasticsearch-model/test/unit/indexing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,55 @@ def as_indexed_json(options={})
end
end

context "Checking for index existence" do
context "the index exists" do
should "return true" do
indices = mock('indices', exists: true)
client = stub('client', indices: indices)

DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_equal true, DummyIndexingModelForRecreate.index_exists?
end
end

context "the index does not exists" do
should "return false" do
indices = mock('indices', exists: false)
client = stub('client', indices: indices)

DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_equal false, DummyIndexingModelForRecreate.index_exists?
end
end

context "the indices raises" do
should "return false" do
client = stub('client')
client.expects(:indices).raises(StandardError)

DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_equal false, DummyIndexingModelForRecreate.index_exists?
end
end

context "the indices raises" do
should "return false" do
indices = stub('indices')
client = stub('client')
client.expects(:indices).returns(indices)

indices.expects(:exists).raises(StandardError)

DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_equal false, DummyIndexingModelForRecreate.index_exists?
end
end
end

context "Re-creating the index" do
class ::DummyIndexingModelForRecreate
extend ActiveModel::Naming
Expand Down Expand Up @@ -468,15 +517,14 @@ class ::DummyIndexingModelForRecreate
indices = stub('indices')
client.stubs(:indices).returns(indices)

indices.expects(:exists).returns(false)

indices.expects(:create).with do |payload|
assert_equal 'dummy_indexing_model_for_recreates', payload[:index]
assert_equal 1, payload[:body][:settings][:index][:number_of_shards]
assert_equal 'keyword', payload[:body][:mappings][:dummy_indexing_model_for_recreate][:properties][:foo][:analyzer]
true
end.returns({})

DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
Expand All @@ -487,11 +535,10 @@ class ::DummyIndexingModelForRecreate
indices = stub('indices')
client.stubs(:indices).returns(indices)

indices.expects(:exists).returns(true)

indices.expects(:create).never

DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
DummyIndexingModelForRecreate.expects(:index_exists?).returns(true)
DummyIndexingModelForRecreate.expects(:client).returns(client).never

assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
end
Expand All @@ -502,9 +549,9 @@ class ::DummyIndexingModelForRecreate
client.stubs(:indices).returns(indices)

indices.expects(:delete).returns({})
indices.expects(:exists).returns(false)
indices.expects(:create).raises(Exception)
indices.expects(:create).raises(Exception).at_least_once

DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_raise(Exception) { DummyIndexingModelForRecreate.create_index! force: true }
Expand All @@ -516,9 +563,9 @@ class ::DummyIndexingModelForRecreate
client.stubs(:indices).returns(indices)

indices.expects(:delete).returns({})
indices.expects(:exists).returns(false)
indices.expects(:create).returns({}).at_least_once

DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once

assert_nothing_raised do
Expand Down Expand Up @@ -559,12 +606,11 @@ class ::DummyIndexingModelForRecreate
end

should "create the custom index" do
@indices.expects(:exists).with do |arguments|
@indices.expects(:create).with do |arguments|
assert_equal 'custom-foo', arguments[:index]
true
end

@indices.expects(:create).with do |arguments|
DummyIndexingModelForRecreate.expects(:index_exists?).with do |arguments|
assert_equal 'custom-foo', arguments[:index]
true
end
Expand Down