Skip to content

Commit

Permalink
[API] Fixed the incorrect create method
Browse files Browse the repository at this point in the history
Previously, the "Create" API has required the `:id` argument, although it is _not_ required
[https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#_automatic_id_generation].

The REST API specs list it as required, though: <https://github.com/elastic/elasticsearch/blob/b47b399f000fce42eac0e00b9fdcf969122660ab/rest-api-spec/src/main/resources/rest-api-spec/api/create.json#L11>

This patch changes the logic of calling the `index` API method: when the `:id` is present,
it adds the `op_type` parameter, if it's missing, it simply calls the `index` method with parameters "as is".

Closes #491

(cherry picked from commit b1a8482)
  • Loading branch information
karmiq committed Jan 28, 2018
1 parent 6383bbd commit ba5f6cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
43 changes: 25 additions & 18 deletions elasticsearch-api/lib/elasticsearch/api/actions/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@ module Elasticsearch
module API
module Actions

# Create a document.
# Create a new document.
#
# Enforce the _create_ operation when indexing a document --
# the operation will return an error when the document already exists.
# The API will create new document, if it doesn't exist yet -- in that case, it will return
# a `409` error (`version_conflict_engine_exception`).
#
# @example Create a document
# You can leave out the `:id` parameter for the ID to be generated automatically
#
# @example Create a document with an ID
#
# client.create index: 'myindex',
# type: 'mytype',
# id: '1',
# body: {
# title: 'Test 1',
# tags: ['y', 'z'],
# published: true,
# published_at: Time.now.utc.iso8601,
# counter: 1
# }
# type: 'doc',
# id: '1',
# body: {
# title: 'Test 1'
# }
#
# @option (see Actions#index)
# @example Create a document with an auto-generated ID
#
# (The `:op_type` argument is ignored.)
# client.create index: 'myindex',
# type: 'doc',
# body: {
# title: 'Test 1'
# }
#
# @option (see Actions#index)
#
# @see http://elasticsearch.org/guide/reference/api/index_/
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#_automatic_id_generation
#
def create(arguments={})
raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]
index arguments.update :op_type => 'create'
if arguments[:id]
index arguments.update :op_type => 'create'
else
index arguments
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-api/lib/elasticsearch/api/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Actions
# @option arguments [Number] :version Explicit version number for concurrency control
# @option arguments [String] :version_type Specific version type (options: internal, external, external_gte, force)
#
# @see http://elasticsearch.org/guide/reference/api/index_/
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
#
def index(arguments={})
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
Expand Down
27 changes: 21 additions & 6 deletions elasticsearch-api/test/unit/create_document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ class IndexDocumentTest < ::Test::Unit::TestCase
context "Creating a document with the #create method" do
subject { FakeClient.new }

should "require the ID" do
assert_raise ArgumentError do
subject.create :index => 'foo', :type => 'bar', :id => nil, :body => {:foo => 'bar'}
end
end

should "perform the create request with a specific ID" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'PUT', method
Expand All @@ -34,6 +28,27 @@ class IndexDocumentTest < ::Test::Unit::TestCase

subject.create :index => 'foo', :type => 'bar/bam', :id => '123', :body => {}
end

should "update the arguments with `op_type` when the `:id` argument is present" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'foo/bar/1', url
assert_not_nil params[:op_type]
true
end.returns(FakeResponse.new)

subject.create :index => 'foo', :type => 'bar', :id => 1, :body => { :foo => 'bar' }
end

should "pass the arguments when the `:id` argument is missing" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'foo/bar', url
assert_nil params[:op_type]
assert_nil params[:id]
true
end.returns(FakeResponse.new)

subject.create :index => 'foo', :type => 'bar', :body => { :foo => 'bar' }
end
end

end
Expand Down

0 comments on commit ba5f6cf

Please sign in to comment.