Skip to content

Commit 6c3ee7e

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 39df4f0 commit 6c3ee7e

File tree

1 file changed

+30
-5
lines changed
  • elasticsearch-api/lib/elasticsearch/api/actions

1 file changed

+30
-5
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

0 commit comments

Comments
 (0)