Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public class BulkRequestBuilder extends ActionRequestLazyBuilder<BulkRequest, Bu
new ArrayList<>();
private ActiveShardCount waitForActiveShards;
private TimeValue timeout;
private String timeoutString;
private String globalPipeline;
private String globalRouting;
private WriteRequest.RefreshPolicy refreshPolicy;
private String refreshPolicyString;

public BulkRequestBuilder(ElasticsearchClient client, @Nullable String globalIndex) {
super(client, BulkAction.INSTANCE);
Expand Down Expand Up @@ -166,7 +164,7 @@ public final BulkRequestBuilder setTimeout(TimeValue timeout) {
* A timeout to wait if the index operation can't be performed immediately. Defaults to {@code 1m}.
*/
public final BulkRequestBuilder setTimeout(String timeout) {
this.timeoutString = timeout;
this.timeout = TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout");
return this;
}

Expand Down Expand Up @@ -195,7 +193,7 @@ public BulkRequestBuilder setRefreshPolicy(WriteRequest.RefreshPolicy refreshPol

@Override
public BulkRequestBuilder setRefreshPolicy(String refreshPolicy) {
this.refreshPolicyString = refreshPolicy;
this.refreshPolicy = WriteRequest.RefreshPolicy.parse(refreshPolicy);
return this;
}

Expand Down Expand Up @@ -223,9 +221,6 @@ public BulkRequest request() {
if (timeout != null) {
request.timeout(timeout);
}
if (timeoutString != null) {
request.timeout(timeoutString);
}
if (globalPipeline != null) {
request.pipeline(globalPipeline);
}
Expand All @@ -235,9 +230,6 @@ public BulkRequest request() {
if (refreshPolicy != null) {
request.setRefreshPolicy(refreshPolicy);
}
if (refreshPolicyString != null) {
request.setRefreshPolicy(refreshPolicyString);
}
return request;
}

Expand All @@ -247,12 +239,6 @@ private void validate() {
"Must use only request builders, requests, or byte arrays within a single bulk request. Cannot mix and match"
);
}
if (timeout != null && timeoutString != null) {
throw new IllegalStateException("Must use only one setTimeout method");
}
if (refreshPolicy != null && refreshPolicyString != null) {
throw new IllegalStateException("Must use only one setRefreshPolicy method");
}
}

private int countNonEmptyLists(List<?>... lists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class DeleteRequestBuilder extends ReplicationRequestBuilder<DeleteReques
private Long seqNo;
private Long term;
private WriteRequest.RefreshPolicy refreshPolicy;
private String refreshPolicyString;

public DeleteRequestBuilder(ElasticsearchClient client, @Nullable String index) {
super(client, TransportDeleteAction.TYPE);
Expand Down Expand Up @@ -102,13 +101,12 @@ public DeleteRequestBuilder setRefreshPolicy(WriteRequest.RefreshPolicy refreshP

@Override
public DeleteRequestBuilder setRefreshPolicy(String refreshPolicy) {
this.refreshPolicyString = refreshPolicy;
this.refreshPolicy = WriteRequest.RefreshPolicy.parse(refreshPolicy);
return this;
}

@Override
public DeleteRequest request() {
validate();
DeleteRequest request = new DeleteRequest();
super.apply(request);
if (id != null) {
Expand All @@ -132,17 +130,6 @@ public DeleteRequest request() {
if (refreshPolicy != null) {
request.setRefreshPolicy(refreshPolicy);
}
if (refreshPolicyString != null) {
request.setRefreshPolicy(refreshPolicyString);
}
return request;
}

@Override
protected void validate() throws IllegalStateException {
super.validate();
if (refreshPolicy != null && refreshPolicyString != null) {
throw new IllegalStateException("Must use only one setRefreshPolicy method");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ public IndexRequest source(Object... source) {
* </p>
*/
public IndexRequest source(XContentType xContentType, Object... source) {
return source(getXContentBuilder(xContentType, source));
}

/**
* Returns an XContentBuilder for the given xContentType and source array
* <p>
* <b>Note: the number of objects passed to this method as varargs must be an even
* number. Also the first argument in each pair (the field name) must have a
* valid String representation.</b>
* </p>
*/
public static XContentBuilder getXContentBuilder(XContentType xContentType, Object... source) {
if (source.length % 2 != 0) {
throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
}
Expand All @@ -489,11 +501,14 @@ public IndexRequest source(XContentType xContentType, Object... source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
builder.startObject();
for (int i = 0; i < source.length; i++) {
builder.field(source[i++].toString(), source[i]);
// This for loop increments by 2 because the source array contains adjacent key/value pairs:
for (int i = 0; i < source.length; i = i + 2) {
String field = source[i].toString();
Object value = source[i + 1];
builder.field(field, value);
}
builder.endObject();
return source(builder);
return builder;
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class IndexRequestBuilder extends ReplicationRequestBuilder<IndexRequest,
private Boolean requireDataStream;
private String routing;
private WriteRequest.RefreshPolicy refreshPolicy;
private String refreshPolicyString;
private Long ifSeqNo;
private Long ifPrimaryTerm;
private DocWriteRequest.OpType opType;
Expand Down Expand Up @@ -175,20 +174,7 @@ public IndexRequestBuilder setSource(Object... source) {
* </p>
*/
public IndexRequestBuilder setSource(XContentType xContentType, Object... source) {
if (source.length % 2 != 0) {
throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
}
try {
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
builder.startObject();
for (int i = 0; i < source.length; i++) {
builder.field(source[i++].toString(), source[i]);
}
builder.endObject();
return setSource(builder);
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate", e);
}
return setSource(IndexRequest.getXContentBuilder(xContentType, source));
}

/**
Expand Down Expand Up @@ -278,13 +264,12 @@ public IndexRequestBuilder setRefreshPolicy(WriteRequest.RefreshPolicy refreshPo
}

public IndexRequestBuilder setRefreshPolicy(String refreshPolicy) {
this.refreshPolicyString = refreshPolicy;
this.refreshPolicy = WriteRequest.RefreshPolicy.parse(refreshPolicy);
return this;
}

@Override
public IndexRequest request() {
validate();
IndexRequest request = new IndexRequest();
super.apply(request);
request.id(id);
Expand All @@ -300,9 +285,6 @@ public IndexRequest request() {
if (refreshPolicy != null) {
request.setRefreshPolicy(refreshPolicy);
}
if (refreshPolicyString != null) {
request.setRefreshPolicy(refreshPolicyString);
}
if (ifSeqNo != null) {
request.setIfSeqNo(ifSeqNo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public abstract class ReplicationRequestBuilder<
Response> {
private String index;
private TimeValue timeout;
private String timeoutString;
private ActiveShardCount waitForActiveShards;

protected ReplicationRequestBuilder(ElasticsearchClient client, ActionType<Response> action) {
Expand All @@ -44,7 +43,7 @@ public RequestBuilder setTimeout(TimeValue timeout) {
*/
@SuppressWarnings("unchecked")
public RequestBuilder setTimeout(String timeout) {
this.timeoutString = timeout;
this.timeout = TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout");
return (RequestBuilder) this;
}

Expand Down Expand Up @@ -84,17 +83,9 @@ protected void apply(Request request) {
if (timeout != null) {
request.timeout(timeout);
}
if (timeoutString != null) {
request.timeout(timeoutString);
}
if (waitForActiveShards != null) {
request.waitForActiveShards(waitForActiveShards);
}
}

protected void validate() throws IllegalStateException {
if (timeout != null && timeoutString != null) {
throw new IllegalStateException("Must use only one setTimeout method");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public abstract class InstanceShardOperationRequestBuilder<
Response> {
private String index;
private TimeValue timeout;
private String timeoutString;

protected InstanceShardOperationRequestBuilder(ElasticsearchClient client, ActionType<Response> action) {
super(client, action);
Expand Down Expand Up @@ -52,7 +51,7 @@ public RequestBuilder setTimeout(TimeValue timeout) {
*/
@SuppressWarnings("unchecked")
public RequestBuilder setTimeout(String timeout) {
this.timeoutString = timeout;
this.timeout = TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout");
return (RequestBuilder) this;
}

Expand All @@ -63,14 +62,5 @@ protected void apply(Request request) {
if (timeout != null) {
request.timeout(timeout);
}
if (timeoutString != null) {
request.timeout(timeoutString);
}
}

protected void validate() throws IllegalStateException {
if (timeoutString != null && timeout != null) {
throw new IllegalStateException("Must use only one setTimeout method");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,14 @@ private IndexRequest safeDoc() {
return doc;
}

/**
* Sets the doc source of the update request to be used when the document does not exists.
*/
public UpdateRequest upsert(BytesReference source, XContentType contentType) {
safeUpsertRequest().source(source, contentType);
return this;
}

/**
* Sets the index request to be used if the document does not exists. Otherwise, a
* {@link org.elasticsearch.index.engine.DocumentMissingException} is thrown.
Expand Down
Loading