Skip to content

Fix buildage #2611

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

Merged
merged 2 commits into from
Mar 31, 2025
Merged
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
1 change: 1 addition & 0 deletions elasticsearch-api/Rakefile
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ namespace :test do
task :unit
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = 'spec/unit/**/*_spec.rb'
t.exclude_pattern = 'spec/unit/perform_request_spec.rb' unless ENV['TEST_WITH_OTEL']
end

desc 'Run unit and integration tests'
35 changes: 30 additions & 5 deletions elasticsearch-api/lib/elasticsearch/api/actions/create.rb
Original file line number Diff line number Diff line change
@@ -77,13 +77,23 @@ module Actions
# @option arguments [String] :index The name of the data stream or index to target.
# If the target doesn't exist and matches the name or wildcard (+*+) pattern of an index template with a +data_stream+ definition, this request creates the data stream.
# If the target doesn't exist and doesn’t match a data stream template, this request creates the index. (*Required*)
# @option arguments [Integer] :if_primary_term Only perform the operation if the document has this primary term.
# @option arguments [Integer] :if_seq_no Only perform the operation if the document has this sequence number.
# @option arguments [Boolean] :include_source_on_error True or false if to include the document source in the error message in case of parsing errors. Server default: true.
# @option arguments [String] :op_type Set to +create+ to only index the document if it does not already exist (put if absent).
# If a document with the specified +_id+ already exists, the indexing operation will fail.
# The behavior is the same as using the +<index>/_create+ endpoint.
# If a document ID is specified, this paramater defaults to +index+.
# Otherwise, it defaults to +create+.
# If the request targets a data stream, an +op_type+ of +create+ is required.
# @option arguments [String] :pipeline The ID of the pipeline to use to preprocess incoming documents.
# If the index has a default ingest pipeline specified, setting the value to +_none+ turns off the default ingest pipeline for this request.
# If a final pipeline is configured, it will always run regardless of the value of this parameter.
# @option arguments [String] :refresh If +true+, Elasticsearch refreshes the affected shards to make this operation visible to search.
# If +wait_for+, it waits for a refresh to make this operation visible to search.
# If +false+, it does nothing with refreshes. Server default: false.
# @option arguments [Boolean] :require_alias If +true+, the destination must be an index alias.
# @option arguments [Boolean] :require_data_stream If +true+, the request's actions must target a data stream (existing or to be created).
# @option arguments [String] :routing A custom value that is used to route operations to a specific shard.
# @option arguments [Time] :timeout The period the request waits for the following operations: automatic index creation, dynamic mapping updates, waiting for active shards.
# Elasticsearch waits for at least the specified timeout period before failing.
@@ -110,11 +120,26 @@ def create(arguments = {})
end
request_opts[:defined_params] = defined_params unless defined_params.empty?

if arguments[:id]
index arguments.update op_type: 'create'
else
index arguments
end
raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]

arguments = arguments.clone
headers = arguments.delete(:headers) || {}

body = arguments.delete(:body)

_id = arguments.delete(:id)

_index = arguments.delete(:index)

method = Elasticsearch::API::HTTP_PUT
path = "#{Utils.listify(_index)}/_create/#{Utils.listify(_id)}"
params = Utils.process_params(arguments)

Elasticsearch::API::Response.new(
perform_request(method, path, params, body, headers, request_opts)
)
end
end
end
75 changes: 22 additions & 53 deletions elasticsearch-api/spec/unit/actions/create_document_spec.rb
Original file line number Diff line number Diff line change
@@ -18,42 +18,40 @@
require 'spec_helper'

describe 'client#create_document' do

let(:expected_args) do
[
'PUT',
'foo/_doc/123',
{ op_type: 'create' },
{ foo: 'bar' },
{},
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'index' }
'PUT',
'foo/_create/123',
{},
{ foo: 'bar' },
{},
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'create' }
]
end

it 'performs the request' do
expect(client_double.create(index: 'foo', id: '123', body: { foo: 'bar'})).to be_a Elasticsearch::API::Response
expect(client_double.create(index: 'foo', id: '123', body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
end

context 'when the request needs to be URL-escaped' do

let(:expected_args) do
[
'PUT',
'foo/_doc/123',
{ op_type: 'create' },
{},
{}
'PUT',
'foo/_doc/123',
{},
{},
{}
]
end

let(:expected_args) do
[
'PUT',
'foo/_doc/123',
{ op_type: 'create' },
'foo/_create/123',
{},
{},
{},
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'index' }
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'create' }
]
end

@@ -63,58 +61,29 @@
end

context 'when an id is provided as an integer' do

let(:expected_args) do
[
'PUT',
'foo/_doc/1',
{ op_type: 'create' },
{ foo: 'bar' },
{}
]
end

let(:expected_args) do
[
'PUT',
'foo/_doc/1',
{ op_type: 'create' },
{ foo: 'bar' },
{},
{ defined_params: { id: 1, index: 'foo' }, endpoint: 'index' }
{}
]
end

it 'updates the arguments with the `op_type`' do
expect(client_double.create(index: 'foo', id: 1, body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
end
end

context 'when an id is not provided' do

let(:expected_args) do
[
'POST',
'foo/_doc',
{ },
{ foo: 'bar' },
{}
]
end

let(:expected_args) do
[
'POST',
'foo/_doc',
{ },
'PUT',
'foo/_create/1',
{},
{ foo: 'bar' },
{},
{ defined_params: { index: 'foo' }, endpoint: 'index' }
{ defined_params: { id: 1, index: 'foo' }, endpoint: 'create' }
]
end

it 'updates the arguments with the `op_type`' do
expect(client_double.create(index: 'foo', body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
it 'performs the request' do
expect(client_double.create(index: 'foo', id: 1, body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
end
end
end
Loading