-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Add UpdateRequest support to High Level Rest client #23266
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,16 @@ | |
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.action.update.UpdateRequest; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.lucene.uid.Versions; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.search.fetch.subphase.FetchSourceContext; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
|
@@ -118,6 +122,48 @@ static Request index(IndexRequest indexRequest) { | |
return new Request(method, endpoint, parameters.getParams(), entity); | ||
} | ||
|
||
static Request update(UpdateRequest updateRequest) throws IOException { | ||
String endpoint = endpoint(updateRequest.index(), updateRequest.type(), updateRequest.id(), "_update"); | ||
|
||
Params parameters = Params.builder(); | ||
parameters.withRouting(updateRequest.routing()); | ||
parameters.withParent(updateRequest.parent()); | ||
parameters.withTimeout(updateRequest.timeout()); | ||
parameters.withRefreshPolicy(updateRequest.getRefreshPolicy()); | ||
parameters.withWaitForActiveShards(updateRequest.waitForActiveShards()); | ||
parameters.withDocAsUpsert(updateRequest.docAsUpsert()); | ||
parameters.withFetchSourceContext(updateRequest.fetchSource()); | ||
parameters.withRetryOnConflict(updateRequest.retryOnConflict()); | ||
parameters.withVersion(updateRequest.version()); | ||
parameters.withVersionType(updateRequest.versionType()); | ||
|
||
// The Java API allows update requests with different content types | ||
// set for the partial document and the upsert document. This client | ||
// only accepts update requests that have the same content types set | ||
// for both doc and upsert. | ||
XContentType xContentType = null; | ||
if (updateRequest.doc() != null) { | ||
xContentType = updateRequest.doc().getContentType(); | ||
} | ||
if (updateRequest.upsertRequest() != null) { | ||
XContentType upsertContentType = updateRequest.upsertRequest().getContentType(); | ||
if ((xContentType != null) && (xContentType != upsertContentType)) { | ||
throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the assumption up until now was that these methods wouldn't throw exceptions. if they do, we have to modify the async methods to catch and call the listener. I think users wouldn't expect to have exceptions thrown with async methods, even if this happens synchronously before the request gets sent :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think I did it in performRequestAsync() ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh I had missed this. But we only catch IOException, while we can also throw IllegalStateException now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right - it should catch Exception instead (and also ensure that the listener get called anyway because I just notive that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh oh, I think those exceptions thrown in RestClient#performRequestAsync are a problem to be handled in the low level client. the bug is there I'm afraid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right - I didn't notice I was looking at the RestClient itself. I'll take care of changing it |
||
" and upsert [" + upsertContentType + "] documents"); | ||
} else { | ||
xContentType = upsertContentType; | ||
} | ||
} | ||
if (xContentType == null) { | ||
xContentType = Requests.INDEX_CONTENT_TYPE; | ||
} | ||
|
||
BytesRef source = XContentHelper.toXContent(updateRequest, xContentType, false).toBytesRef(); | ||
HttpEntity entity = new ByteArrayEntity(source.bytes, source.offset, source.length, ContentType.create(xContentType.mediaType())); | ||
|
||
return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), entity); | ||
} | ||
|
||
/** | ||
* Utility method to build request's endpoint. | ||
*/ | ||
|
@@ -160,6 +206,13 @@ Params putParam(String key, TimeValue value) { | |
return this; | ||
} | ||
|
||
Params withDocAsUpsert(boolean docAsUpsert) { | ||
if (docAsUpsert) { | ||
return putParam("doc_as_upsert", Boolean.TRUE.toString()); | ||
} | ||
return this; | ||
} | ||
|
||
Params withFetchSourceContext(FetchSourceContext fetchSourceContext) { | ||
if (fetchSourceContext != null) { | ||
if (fetchSourceContext.fetchSource() == false) { | ||
|
@@ -203,7 +256,14 @@ Params withRefresh(boolean refresh) { | |
|
||
Params withRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { | ||
if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) { | ||
putParam("refresh", refreshPolicy.getValue()); | ||
return putParam("refresh", refreshPolicy.getValue()); | ||
} | ||
return this; | ||
} | ||
|
||
Params withRetryOnConflict(int retryOnConflict) { | ||
if (retryOnConflict > 0) { | ||
return putParam("retry_on_conflict", String.valueOf(retryOnConflict)); | ||
} | ||
return this; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering if we should rather catch IOException and rethrow UncheckedIOException. getting tired of all these checked exceptions, which force us to move to CheckedFunction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I'm on the fence on this. We already use CheckedFunction for response parsers I think we could also use CheckedFunction for request converters? I don't feel like it adds a lot of boling code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok fine with me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks