Skip to content

Commit 241898d

Browse files
committed
[API] Updates create endpoint for new behaviour.
The API now makes a request, instead of calling index like in previous versions (< 9.0.0). * `body`, `index` and `id` are now required parameters. * `op_type` is now a String parameter: 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. Additional new parameters: * :if_primary_term (Integer) - Only perform the operation if the document has this primary term. * :if_seq_no (Integer) - Only perform the operation if the document has this sequence number.
1 parent b915ef5 commit 241898d

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

elasticsearch-api/lib/elasticsearch/api/actions/create.rb

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,23 @@ module Actions
7777
# @option arguments [String] :index The name of the data stream or index to target.
7878
# 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.
7979
# If the target doesn't exist and doesn’t match a data stream template, this request creates the index. (*Required*)
80+
# @option arguments [Integer] :if_primary_term Only perform the operation if the document has this primary term.
81+
# @option arguments [Integer] :if_seq_no Only perform the operation if the document has this sequence number.
8082
# @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.
83+
# @option arguments [String] :op_type Set to +create+ to only index the document if it does not already exist (put if absent).
84+
# If a document with the specified +_id+ already exists, the indexing operation will fail.
85+
# The behavior is the same as using the +<index>/_create+ endpoint.
86+
# If a document ID is specified, this paramater defaults to +index+.
87+
# Otherwise, it defaults to +create+.
88+
# If the request targets a data stream, an +op_type+ of +create+ is required.
8189
# @option arguments [String] :pipeline The ID of the pipeline to use to preprocess incoming documents.
8290
# If the index has a default ingest pipeline specified, setting the value to +_none+ turns off the default ingest pipeline for this request.
8391
# If a final pipeline is configured, it will always run regardless of the value of this parameter.
8492
# @option arguments [String] :refresh If +true+, Elasticsearch refreshes the affected shards to make this operation visible to search.
8593
# If +wait_for+, it waits for a refresh to make this operation visible to search.
8694
# If +false+, it does nothing with refreshes. Server default: false.
95+
# @option arguments [Boolean] :require_alias If +true+, the destination must be an index alias.
96+
# @option arguments [Boolean] :require_data_stream If +true+, the request's actions must target a data stream (existing or to be created).
8797
# @option arguments [String] :routing A custom value that is used to route operations to a specific shard.
8898
# @option arguments [Time] :timeout The period the request waits for the following operations: automatic index creation, dynamic mapping updates, waiting for active shards.
8999
# Elasticsearch waits for at least the specified timeout period before failing.
@@ -110,11 +120,26 @@ def create(arguments = {})
110120
end
111121
request_opts[:defined_params] = defined_params unless defined_params.empty?
112122

113-
if arguments[:id]
114-
index arguments.update op_type: 'create'
115-
else
116-
index arguments
117-
end
123+
raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
124+
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
125+
raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]
126+
127+
arguments = arguments.clone
128+
headers = arguments.delete(:headers) || {}
129+
130+
body = arguments.delete(:body)
131+
132+
_id = arguments.delete(:id)
133+
134+
_index = arguments.delete(:index)
135+
136+
method = Elasticsearch::API::HTTP_PUT
137+
path = "#{Utils.listify(_index)}/_create/#{Utils.listify(_id)}"
138+
params = Utils.process_params(arguments)
139+
140+
Elasticsearch::API::Response.new(
141+
perform_request(method, path, params, body, headers, request_opts)
142+
)
118143
end
119144
end
120145
end

elasticsearch-api/spec/unit/actions/create_document_spec.rb

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,40 @@
1818
require 'spec_helper'
1919

2020
describe 'client#create_document' do
21-
2221
let(:expected_args) do
2322
[
24-
'PUT',
25-
'foo/_doc/123',
26-
{ op_type: 'create' },
27-
{ foo: 'bar' },
28-
{},
29-
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'index' }
23+
'PUT',
24+
'foo/_create/123',
25+
{},
26+
{ foo: 'bar' },
27+
{},
28+
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'create' }
3029
]
3130
end
3231

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

3736
context 'when the request needs to be URL-escaped' do
38-
3937
let(:expected_args) do
4038
[
41-
'PUT',
42-
'foo/_doc/123',
43-
{ op_type: 'create' },
44-
{},
45-
{}
39+
'PUT',
40+
'foo/_doc/123',
41+
{},
42+
{},
43+
{}
4644
]
4745
end
4846

4947
let(:expected_args) do
5048
[
5149
'PUT',
52-
'foo/_doc/123',
53-
{ op_type: 'create' },
50+
'foo/_create/123',
51+
{},
5452
{},
5553
{},
56-
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'index' }
54+
{ defined_params: { id: '123', index: 'foo' }, endpoint: 'create' }
5755
]
5856
end
5957

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

6563
context 'when an id is provided as an integer' do
66-
67-
let(:expected_args) do
68-
[
69-
'PUT',
70-
'foo/_doc/1',
71-
{ op_type: 'create' },
72-
{ foo: 'bar' },
73-
{}
74-
]
75-
end
76-
7764
let(:expected_args) do
7865
[
7966
'PUT',
8067
'foo/_doc/1',
8168
{ op_type: 'create' },
8269
{ foo: 'bar' },
83-
{},
84-
{ defined_params: { id: 1, index: 'foo' }, endpoint: 'index' }
70+
{}
8571
]
8672
end
8773

88-
it 'updates the arguments with the `op_type`' do
89-
expect(client_double.create(index: 'foo', id: 1, body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
90-
end
91-
end
92-
93-
context 'when an id is not provided' do
94-
9574
let(:expected_args) do
9675
[
97-
'POST',
98-
'foo/_doc',
99-
{ },
100-
{ foo: 'bar' },
101-
{}
102-
]
103-
end
104-
105-
let(:expected_args) do
106-
[
107-
'POST',
108-
'foo/_doc',
109-
{ },
76+
'PUT',
77+
'foo/_create/1',
78+
{},
11079
{ foo: 'bar' },
11180
{},
112-
{ defined_params: { index: 'foo' }, endpoint: 'index' }
81+
{ defined_params: { id: 1, index: 'foo' }, endpoint: 'create' }
11382
]
11483
end
11584

116-
it 'updates the arguments with the `op_type`' do
117-
expect(client_double.create(index: 'foo', body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
85+
it 'performs the request' do
86+
expect(client_double.create(index: 'foo', id: 1, body: { foo: 'bar' })).to be_a Elasticsearch::API::Response
11887
end
11988
end
12089
end

0 commit comments

Comments
 (0)