co.elastic.clients.elasticsearch.core.BulkRequest Maven / Gradle / Ivy
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//----------------------------------------------------
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
//----------------------------------------------------
package co.elastic.clients.elasticsearch.core;
import co.elastic.clients.elasticsearch._types.ErrorResponse;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch._types.RequestBase;
import co.elastic.clients.elasticsearch._types.Time;
import co.elastic.clients.elasticsearch._types.WaitForActiveShards;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.SourceConfigParam;
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.JsonpSerializable;
import co.elastic.clients.json.NdJsonpSerializable;
import co.elastic.clients.json.ObjectBuilderDeserializer;
import co.elastic.clients.json.ObjectDeserializer;
import co.elastic.clients.transport.Endpoint;
import co.elastic.clients.transport.endpoints.SimpleEndpoint;
import co.elastic.clients.util.ApiTypeHelper;
import co.elastic.clients.util.ObjectBuilder;
import co.elastic.clients.util.ObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;
import java.lang.Boolean;
import java.lang.String;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
// typedef: _global.bulk.Request
/**
* Allows to perform multiple index/update/delete operations in a single
* request.
*
* @see API
* specification
*/
public class BulkRequest extends RequestBase implements NdJsonpSerializable, JsonpSerializable {
@Nullable
private final SourceConfigParam source;
private final List sourceExcludes;
private final List sourceIncludes;
@Nullable
private final String index;
@Nullable
private final String pipeline;
@Nullable
private final Refresh refresh;
@Nullable
private final Boolean requireAlias;
@Nullable
private final String routing;
@Nullable
private final Time timeout;
@Nullable
private final WaitForActiveShards waitForActiveShards;
private final List operations;
// ---------------------------------------------------------------------------------------------
private BulkRequest(Builder builder) {
this.source = builder.source;
this.sourceExcludes = ApiTypeHelper.unmodifiable(builder.sourceExcludes);
this.sourceIncludes = ApiTypeHelper.unmodifiable(builder.sourceIncludes);
this.index = builder.index;
this.pipeline = builder.pipeline;
this.refresh = builder.refresh;
this.requireAlias = builder.requireAlias;
this.routing = builder.routing;
this.timeout = builder.timeout;
this.waitForActiveShards = builder.waitForActiveShards;
this.operations = ApiTypeHelper.unmodifiableRequired(builder.operations, this, "operations");
}
public static BulkRequest of(Function> fn) {
return fn.apply(new Builder()).build();
}
@Override
public Iterator> _serializables() {
return this.operations.iterator();
}
/**
* True or false to return the _source field or not, or default list of fields
* to return, can be overridden on each sub-request
*
* API name: {@code _source}
*/
@Nullable
public final SourceConfigParam source() {
return this.source;
}
/**
* Default list of fields to exclude from the returned _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_excludes}
*/
public final List sourceExcludes() {
return this.sourceExcludes;
}
/**
* Default list of fields to extract and return from the _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_includes}
*/
public final List sourceIncludes() {
return this.sourceIncludes;
}
/**
* Default index for items which don't provide one
*
* API name: {@code index}
*/
@Nullable
public final String index() {
return this.index;
}
/**
* The pipeline id to preprocess incoming documents with
*
* API name: {@code pipeline}
*/
@Nullable
public final String pipeline() {
return this.pipeline;
}
/**
* If true
then refresh the affected shards to make this operation
* visible to search, if wait_for
then wait for a refresh to make
* this operation visible to search, if false
(the default) then do
* nothing with refreshes.
*
* API name: {@code refresh}
*/
@Nullable
public final Refresh refresh() {
return this.refresh;
}
/**
* Sets require_alias for all incoming documents. Defaults to unset (false)
*
* API name: {@code require_alias}
*/
@Nullable
public final Boolean requireAlias() {
return this.requireAlias;
}
/**
* Specific routing value
*
* API name: {@code routing}
*/
@Nullable
public final String routing() {
return this.routing;
}
/**
* Explicit operation timeout
*
* API name: {@code timeout}
*/
@Nullable
public final Time timeout() {
return this.timeout;
}
/**
* Sets the number of shard copies that must be active before proceeding with
* the bulk operation. Defaults to 1, meaning the primary shard only. Set to
* all
for all shard copies, otherwise set to any non-negative
* value less than or equal to the total number of copies for the shard (number
* of replicas + 1)
*
* API name: {@code wait_for_active_shards}
*/
@Nullable
public final WaitForActiveShards waitForActiveShards() {
return this.waitForActiveShards;
}
/**
* Required - Request body.
*
* API name: {@code _value_body}
*/
public final List operations() {
return this.operations;
}
/**
* Serialize this value to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartArray();
for (BulkOperation item0 : this.operations) {
item0.serialize(generator, mapper);
}
generator.writeEnd();
}
// ---------------------------------------------------------------------------------------------
/**
* Builder for {@link BulkRequest}.
*/
public static class Builder extends ObjectBuilderBase implements ObjectBuilder {
@Nullable
private SourceConfigParam source;
@Nullable
private List sourceExcludes;
@Nullable
private List sourceIncludes;
@Nullable
private String index;
@Nullable
private String pipeline;
@Nullable
private Refresh refresh;
@Nullable
private Boolean requireAlias;
@Nullable
private String routing;
@Nullable
private Time timeout;
@Nullable
private WaitForActiveShards waitForActiveShards;
private List operations;
/**
* True or false to return the _source field or not, or default list of fields
* to return, can be overridden on each sub-request
*
* API name: {@code _source}
*/
public final Builder source(@Nullable SourceConfigParam value) {
this.source = value;
return this;
}
/**
* True or false to return the _source field or not, or default list of fields
* to return, can be overridden on each sub-request
*
* API name: {@code _source}
*/
public final Builder source(Function> fn) {
return this.source(fn.apply(new SourceConfigParam.Builder()).build());
}
/**
* Default list of fields to exclude from the returned _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_excludes}
*
* Adds all elements of list
to sourceExcludes
.
*/
public final Builder sourceExcludes(List list) {
this.sourceExcludes = _listAddAll(this.sourceExcludes, list);
return this;
}
/**
* Default list of fields to exclude from the returned _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_excludes}
*
* Adds one or more values to sourceExcludes
.
*/
public final Builder sourceExcludes(String value, String... values) {
this.sourceExcludes = _listAdd(this.sourceExcludes, value, values);
return this;
}
/**
* Default list of fields to extract and return from the _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_includes}
*
* Adds all elements of list
to sourceIncludes
.
*/
public final Builder sourceIncludes(List list) {
this.sourceIncludes = _listAddAll(this.sourceIncludes, list);
return this;
}
/**
* Default list of fields to extract and return from the _source field, can be
* overridden on each sub-request
*
* API name: {@code _source_includes}
*
* Adds one or more values to sourceIncludes
.
*/
public final Builder sourceIncludes(String value, String... values) {
this.sourceIncludes = _listAdd(this.sourceIncludes, value, values);
return this;
}
/**
* Default index for items which don't provide one
*
* API name: {@code index}
*/
public final Builder index(@Nullable String value) {
this.index = value;
return this;
}
/**
* The pipeline id to preprocess incoming documents with
*
* API name: {@code pipeline}
*/
public final Builder pipeline(@Nullable String value) {
this.pipeline = value;
return this;
}
/**
* If true
then refresh the affected shards to make this operation
* visible to search, if wait_for
then wait for a refresh to make
* this operation visible to search, if false
(the default) then do
* nothing with refreshes.
*
* API name: {@code refresh}
*/
public final Builder refresh(@Nullable Refresh value) {
this.refresh = value;
return this;
}
/**
* Sets require_alias for all incoming documents. Defaults to unset (false)
*
* API name: {@code require_alias}
*/
public final Builder requireAlias(@Nullable Boolean value) {
this.requireAlias = value;
return this;
}
/**
* Specific routing value
*
* API name: {@code routing}
*/
public final Builder routing(@Nullable String value) {
this.routing = value;
return this;
}
/**
* Explicit operation timeout
*
* API name: {@code timeout}
*/
public final Builder timeout(@Nullable Time value) {
this.timeout = value;
return this;
}
/**
* Explicit operation timeout
*
* API name: {@code timeout}
*/
public final Builder timeout(Function> fn) {
return this.timeout(fn.apply(new Time.Builder()).build());
}
/**
* Sets the number of shard copies that must be active before proceeding with
* the bulk operation. Defaults to 1, meaning the primary shard only. Set to
* all
for all shard copies, otherwise set to any non-negative
* value less than or equal to the total number of copies for the shard (number
* of replicas + 1)
*
* API name: {@code wait_for_active_shards}
*/
public final Builder waitForActiveShards(@Nullable WaitForActiveShards value) {
this.waitForActiveShards = value;
return this;
}
/**
* Sets the number of shard copies that must be active before proceeding with
* the bulk operation. Defaults to 1, meaning the primary shard only. Set to
* all
for all shard copies, otherwise set to any non-negative
* value less than or equal to the total number of copies for the shard (number
* of replicas + 1)
*
* API name: {@code wait_for_active_shards}
*/
public final Builder waitForActiveShards(
Function> fn) {
return this.waitForActiveShards(fn.apply(new WaitForActiveShards.Builder()).build());
}
/**
* Required - Request body.
*
* API name: {@code _value_body}
*
* Adds all elements of list
to operations
.
*/
public final Builder operations(List list) {
this.operations = _listAddAll(this.operations, list);
return this;
}
/**
* Required - Request body.
*
* API name: {@code _value_body}
*
* Adds one or more values to operations
.
*/
public final Builder operations(BulkOperation value, BulkOperation... values) {
this.operations = _listAdd(this.operations, value, values);
return this;
}
/**
* Required - Request body.
*
* API name: {@code _value_body}
*
* Adds a value to operations
using a builder lambda.
*/
public final Builder operations(Function> fn) {
return operations(fn.apply(new BulkOperation.Builder()).build());
}
/**
* Builds a {@link BulkRequest}.
*
* @throws NullPointerException
* if some of the required fields are null.
*/
public BulkRequest build() {
_checkSingleUse();
return new BulkRequest(this);
}
}
// ---------------------------------------------------------------------------------------------
/**
* Endpoint "{@code bulk}".
*/
public static final Endpoint _ENDPOINT = new SimpleEndpoint<>("es/bulk",
// Request method
request -> {
return "POST";
},
// Request path
request -> {
final int _index = 1 << 0;
int propsSet = 0;
if (request.index() != null)
propsSet |= _index;
if (propsSet == 0) {
StringBuilder buf = new StringBuilder();
buf.append("/_bulk");
return buf.toString();
}
if (propsSet == (_index)) {
StringBuilder buf = new StringBuilder();
buf.append("/");
SimpleEndpoint.pathEncode(request.index, buf);
buf.append("/_bulk");
return buf.toString();
}
throw SimpleEndpoint.noPathTemplateFound("path");
},
// Request parameters
request -> {
Map params = new HashMap<>();
if (request.pipeline != null) {
params.put("pipeline", request.pipeline);
}
if (request.routing != null) {
params.put("routing", request.routing);
}
if (request.requireAlias != null) {
params.put("require_alias", String.valueOf(request.requireAlias));
}
if (request.refresh != null) {
params.put("refresh", request.refresh.jsonValue());
}
if (request.waitForActiveShards != null) {
params.put("wait_for_active_shards", request.waitForActiveShards._toJsonString());
}
if (request.source != null) {
params.put("_source", request.source._toJsonString());
}
if (ApiTypeHelper.isDefined(request.sourceExcludes)) {
params.put("_source_excludes",
request.sourceExcludes.stream().map(v -> v).collect(Collectors.joining(",")));
}
if (ApiTypeHelper.isDefined(request.sourceIncludes)) {
params.put("_source_includes",
request.sourceIncludes.stream().map(v -> v).collect(Collectors.joining(",")));
}
if (request.timeout != null) {
params.put("timeout", request.timeout._toJsonString());
}
return params;
}, SimpleEndpoint.emptyMap(), true, BulkResponse._DESERIALIZER);
}