Add delete API to the High Level Rest Client#23187
Conversation
| assertEquals("type", deleteResponse.getType()); | ||
| assertEquals("does_not_exist", deleteResponse.getId()); | ||
| assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); | ||
| assertEquals(1, deleteResponse.getVersion()); |
There was a problem hiding this comment.
This is surprising to me. I'd expect it to be -1 similar to the GET request
There was a problem hiding this comment.
I think Get request returns an error with NOT FOUND response code in this case? Which make more sense to me.
Anyway a delete increments the version even if the document is not found, and is kept in the versions map as a tombstone.
# Conflicts: # client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java # client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java
| return new Request(method, endpoint, parameters.getParams(), entity); | ||
| } | ||
|
|
||
| static Request delete(DeleteRequest deleteRequest) { |
There was a problem hiding this comment.
Can you please move this up so that methods are somewhat alphabetically sorted?
There was a problem hiding this comment.
Ok. I'm also moving ping method. And add a comments for coming developers
| } | ||
|
|
||
| /** | ||
| * Deletes a document by id using the delete api |
There was a problem hiding this comment.
"Deletes a document using the Delete API" with a link to the documentation like it is done for other methods
There was a problem hiding this comment.
Right. I did not notice the modifications when I merged the master branch into my branch.
| /** | ||
| * Asynchronously deletes a document by id using the delete api | ||
| */ | ||
| public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteResponse> listener, Header... headers) { |
| assertEquals("index", deleteResponse.getIndex()); | ||
| assertEquals("type", deleteResponse.getType()); | ||
| assertEquals("id", deleteResponse.getId()); | ||
| assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); |
There was a problem hiding this comment.
I don't see tests for routing, parent, version type etc. That would be nice to check that parameters are correctly propagated.
| String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; | ||
| StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON); | ||
| Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity); | ||
| assertEquals(201, response.getStatusLine().getStatusCode()); |
There was a problem hiding this comment.
I think we could start using our own high level rest client for those? Now we have Index API support...
| } | ||
| } | ||
|
|
||
| public void testDelete() throws IOException { |
There was a problem hiding this comment.
Can you move this up so that it is alphabetically sorted?
| assertEquals("type", deleteResponse.getType()); | ||
| assertEquals("does_not_exist", deleteResponse.getId()); | ||
| assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); | ||
| assertEquals(1, deleteResponse.getVersion()); |
There was a problem hiding this comment.
I think Get request returns an error with NOT FOUND response code in this case? Which make more sense to me.
Anyway a delete increments the version even if the document is not found, and is kept in the versions map as a tombstone.
* Sort alphabetically method names * Add javadoc link to the ref guide * Add more tests about routing, version and version type
|
Thanks @tlrx for the review! I pushed a new commit to address your comments. LMK. |
javanna
left a comment
There was a problem hiding this comment.
left a few comments LGTM otherwise
|
|
||
| static Request ping() { | ||
| return new Request("HEAD", "/", Collections.emptyMap(), null); | ||
| // For developers please add your new methods in the alphabetical order |
There was a problem hiding this comment.
I think this comment won't be enough to enforce this convention... why does order matter anyways @tlrx ?
There was a problem hiding this comment.
Agreed. But at least as I was not aware of this convention and did not find it in code comments I added it.
Any suggestion? Remove the comment?
There was a problem hiding this comment.
@javanna It doesn't matter, that's not a convention and it doesn't make sense to add a comment for that. I just find it easier to find and read class methods when they are sorted, specially when it's a suite of operations like this. But if I'm the only one to care about this, let's add new method wherever you want.
There was a problem hiding this comment.
ok let's remove the comment.
| * @return A sample index request | ||
| * @throws IOException If problem when generating the content | ||
| */ | ||
| private IndexRequest createSampleIndexRequest() throws IOException { |
There was a problem hiding this comment.
do we need a method for this? don't we always do createSampleDocument(createSampleIndexRequest))?
I also wonder if it's ok to always rely on auto-generated ids. maybe we should provide our ids instead.
There was a problem hiding this comment.
don't we always do createSampleDocument(createSampleIndexRequest))?
No. We also do:
createSampleDocument(createSampleIndexRequest().routing("foo"));See
There was a problem hiding this comment.
I also wonder if it's ok to always rely on auto-generated ids. maybe we should provide our ids instead.
Why not?
note that sometimes I'm doing:
createSampleIndexRequest().id(randomAsciiOfLength(15));Where I basically force the id. So I'd say that we have both covered.
There was a problem hiding this comment.
that's ok. anyways we are testing delete here.
There was a problem hiding this comment.
So I'm now using my own ids for every test.
| parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy()); | ||
| parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards()); | ||
|
|
||
| return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null); |
There was a problem hiding this comment.
I think we should test this in RequestTests and have unit tests to make sure that all supported parameters are propagated.
There was a problem hiding this comment.
Do you want to test the Params builder actually?
Something like:
public void testWithParams() {
Request.Params params = Request.Params.builder();
params.withFetchSourceContext(FetchSourceContext.FETCH_SOURCE);
params.withParent("parent");
params.withPipeline("pipeline");
params.withPreference("preference");
params.withRealtime(true);
if (randomBoolean()) {
params.withRefresh(true);
} else {
params.withRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
}
params.withRouting("routing");
params.withStoredFields(new String[]{"field1","field2"});
params.withTimeout(TimeValue.timeValueMillis(100));
params.withVersion(2);
params.withVersionType(VersionType.EXTERNAL);
params.withWaitForActiveShards(ActiveShardCount.ALL);
Map<String, String> requestParams = params.getParams();
assertEquals(10, requestParams.size());
}I can add it in the PR or later.
There was a problem hiding this comment.
I don't think the above is that useful, I would rather like to make sure that the new method to generate a request calls all of the with* methods that need to be called and the params from map to request are all propagated.
There was a problem hiding this comment.
I think I understand. So test something like the following?
public void testDelete() {
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id")
.routing("routing");
Request request = Request.delete(deleteRequest);
assertEquals(true, request.params.containsKey("routing"));
}There was a problem hiding this comment.
yes something along those lines. we have existing tests for this in RequestTests, see e.g. testIndex
There was a problem hiding this comment.
Thanks for the pointer. I did that in RequestTests.
| private IndexRequest createSampleIndexRequest() throws IOException { | ||
| final XContentType xContentType = randomFrom(XContentType.values()); | ||
| IndexRequest indexRequest = new IndexRequest("index", "type"); | ||
| indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("test", "test").endObject()); |
There was a problem hiding this comment.
if this is only used for testing deletions, I don't think we need to randomize the content type when indexing. Let's use json? we can also provide it as a string.
There was a problem hiding this comment.
Agreed. I simplified all that.
| * @throws IOException if something goes wrong while executing the index request | ||
| */ | ||
| private void createSampleDocument(IndexRequest request) throws IOException { | ||
| IndexResponse indexResponse = execute(request, highLevelClient()::index, highLevelClient()::indexAsync); |
There was a problem hiding this comment.
one more thing, given that this is used only to test deletions, let's just use the sync version of the method rather than randomizing. I do wonder whether this additional method is needed.
There was a problem hiding this comment.
If we do only the sync method, then we can definitely remove that method IMO. I'll do that.
| expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep()); | ||
| } | ||
|
|
||
| if (frequently()) { |
There was a problem hiding this comment.
out of curiosity, why frequently and not e.g. half of the times?
There was a problem hiding this comment.
Copy and paste from index test. I can change it in both places or only here. As you prefer.
| expectedParams.put("refresh", refreshPolicy.getValue()); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
maybe we can share some common parts between index and delete?
There was a problem hiding this comment.
Haha! Funny. I wanted to propose that but was unsure. I totally agree.
| () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); | ||
| assertEquals(RestStatus.CONFLICT, exception.status()); | ||
| assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + | ||
| "reason=[type][" + docId + "]: " + |
There was a problem hiding this comment.
The format of the line seems a bit weird
| Request request = Request.delete(deleteRequest); | ||
| assertEquals("/" + index + "/" + type + "/" + id, request.endpoint); | ||
| assertEquals(expectedParams, request.params); | ||
| assertEquals("DELETE", request.method); |
There was a problem hiding this comment.
Can you check that the request entity is null as well?
| } | ||
| } No newline at end of file | ||
|
|
||
| private void enrichReplicationRequest(ReplicatedWriteRequest request, Map<String, String> expectedParams) { |
There was a problem hiding this comment.
I'd have different static methods setRandomTimeout(..) and setRandomRefreshPolicy(...)
| } | ||
| } | ||
|
|
||
| private void enrichDocWriteRequest(DocWriteRequest request, Map<String, String> expectedParams) { |
There was a problem hiding this comment.
I'd prefer to have two static methods setRandomVersion() and setRandomVersionType
|
|
||
| private void enrichDocWriteRequest(DocWriteRequest request, Map<String, String> expectedParams) { | ||
| // There is some logic around _create endpoint and version/version type | ||
| if (request.opType() == DocWriteRequest.OpType.CREATE) { |
There was a problem hiding this comment.
This part should be in the testIndex() method because it is specific to Index requests, it could be:
public void testIndex() {
...
if (request.opType() == DocWriteRequest.OpType.CREATE) {
...
} else {
setRandomVersion(indexRequest, expectedParams);
setRandomVersionType(indexRequest, expectedParams);
...
}
|
Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually? |
|
@dadoonet can you merge this please? it's ready no? |
|
I hope doing it tomorrow morning CET. |
# Conflicts: # client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java # client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java # client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java # client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java
|
Thanks Tanguy. That makes the code much more readable. I like it! I think I should now have one more step which is to try to use all the randomXXX() methods I have to the other tests. |
|
@dadoonet I think this can go in. Feel free to create a follow up pull request to apply randomVersion() etc to other tests. |
No description provided.