diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/create.rb b/elasticsearch-api/lib/elasticsearch/api/actions/create.rb index b934a70f15..70132fe2cb 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/create.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/create.rb @@ -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 diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/index.rb b/elasticsearch-api/lib/elasticsearch/api/actions/index.rb index 1c6c883955..e70674de8d 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/index.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/index.rb @@ -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] diff --git a/elasticsearch-api/test/unit/create_document_test.rb b/elasticsearch-api/test/unit/create_document_test.rb index c5dd5de254..6ca4b160d4 100644 --- a/elasticsearch-api/test/unit/create_document_test.rb +++ b/elasticsearch-api/test/unit/create_document_test.rb @@ -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 @@ -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