Fix after 1st review
* Sort alphabetically method names * Add javadoc link to the ref guide * Add more tests about routing, version and version type
This commit is contained in:
parent
14f04905f8
commit
5aa2ab3bbc
|
@ -71,8 +71,21 @@ final class Request {
|
|||
'}';
|
||||
}
|
||||
|
||||
static Request ping() {
|
||||
return new Request("HEAD", "/", Collections.emptyMap(), null);
|
||||
// For developers please add your new methods in the alphabetical order
|
||||
|
||||
static Request delete(DeleteRequest deleteRequest) {
|
||||
String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id());
|
||||
|
||||
Params parameters = Params.builder();
|
||||
parameters.withRouting(deleteRequest.routing());
|
||||
parameters.withParent(deleteRequest.parent());
|
||||
parameters.withTimeout(deleteRequest.timeout());
|
||||
parameters.withVersion(deleteRequest.version());
|
||||
parameters.withVersionType(deleteRequest.versionType());
|
||||
parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy());
|
||||
parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards());
|
||||
|
||||
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
|
||||
}
|
||||
|
||||
static Request exists(GetRequest getRequest) {
|
||||
|
@ -120,19 +133,8 @@ final class Request {
|
|||
return new Request(method, endpoint, parameters.getParams(), entity);
|
||||
}
|
||||
|
||||
static Request delete(DeleteRequest deleteRequest) {
|
||||
String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id());
|
||||
|
||||
Params parameters = Params.builder();
|
||||
parameters.withRouting(deleteRequest.routing());
|
||||
parameters.withParent(deleteRequest.parent());
|
||||
parameters.withTimeout(deleteRequest.timeout());
|
||||
parameters.withVersion(deleteRequest.version());
|
||||
parameters.withVersionType(deleteRequest.versionType());
|
||||
parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy());
|
||||
parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards());
|
||||
|
||||
return new Request(HttpDelete.METHOD_NAME, endpoint, parameters.getParams(), null);
|
||||
static Request ping() {
|
||||
return new Request("HEAD", "/", Collections.emptyMap(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -124,7 +124,9 @@ public class RestHighLevelClient {
|
|||
}
|
||||
|
||||
/**
|
||||
* Deletes a document by id using the delete api
|
||||
* Deletes a document by id using the Delete api
|
||||
*
|
||||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
|
||||
*/
|
||||
public DeleteResponse delete(DeleteRequest deleteRequest, Header... headers) throws IOException {
|
||||
return performRequestAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, Collections.singleton(404),
|
||||
|
@ -132,7 +134,9 @@ public class RestHighLevelClient {
|
|||
}
|
||||
|
||||
/**
|
||||
* Asynchronously deletes a document by id using the delete api
|
||||
* Asynchronously deletes a document by id using the Delete api
|
||||
*
|
||||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html">Delete API on elastic.co</a>
|
||||
*/
|
||||
public void deleteAsync(DeleteRequest deleteRequest, ActionListener<DeleteResponse> listener, Header... headers) {
|
||||
performRequestAsyncAndParseEntity(deleteRequest, Request::delete, DeleteResponse::fromXContent, listener,
|
||||
|
|
|
@ -46,6 +46,79 @@ import static org.hamcrest.CoreMatchers.containsString;
|
|||
|
||||
public class CrudIT extends ESRestHighLevelClientTestCase {
|
||||
|
||||
public void testDelete() throws IOException {
|
||||
{
|
||||
// Testing non existing document
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist");
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals("does_not_exist", deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
|
||||
}
|
||||
{
|
||||
// Testing deletion
|
||||
String docId = createSampleDocument(createSampleIndexRequest());
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId);
|
||||
if (randomBoolean()) {
|
||||
deleteRequest.version(1L);
|
||||
}
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals(docId, deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
|
||||
}
|
||||
{
|
||||
// Testing version conflict
|
||||
String docId = createSampleDocument(createSampleIndexRequest());
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).version(2);
|
||||
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
|
||||
() -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync));
|
||||
assertEquals(RestStatus.CONFLICT, exception.status());
|
||||
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " +
|
||||
"reason=[type][" + docId + "]: " +
|
||||
"version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
|
||||
assertEquals("index", exception.getMetadata("es.index").get(0));
|
||||
}
|
||||
{
|
||||
// Testing version type
|
||||
String docId = createSampleDocument(createSampleIndexRequest()
|
||||
.id(randomAsciiOfLength(15))
|
||||
.versionType(VersionType.EXTERNAL).version(12));
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(13);
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals(docId, deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
|
||||
}
|
||||
{
|
||||
// Testing version type with a wrong version
|
||||
String docId = createSampleDocument(createSampleIndexRequest()
|
||||
.id(randomAsciiOfLength(15))
|
||||
.versionType(VersionType.EXTERNAL).version(12));
|
||||
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).versionType(VersionType.EXTERNAL).version(10);
|
||||
execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
});
|
||||
assertEquals(RestStatus.CONFLICT, exception.status());
|
||||
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][" +
|
||||
docId + "]: version conflict, current version [12] is higher or equal to the one provided [10]]", exception.getMessage());
|
||||
assertEquals("index", exception.getMetadata("es.index").get(0));
|
||||
}
|
||||
{
|
||||
// Testing routing
|
||||
String docId = createSampleDocument(createSampleIndexRequest().routing("foo"));
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId).routing("foo");
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals(docId, deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
|
||||
}
|
||||
}
|
||||
|
||||
public void testExists() throws IOException {
|
||||
{
|
||||
GetRequest getRequest = new GetRequest("index", "type", "id");
|
||||
|
@ -268,39 +341,26 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testDelete() throws IOException {
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "does_not_exist");
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals("does_not_exist", deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
|
||||
assertEquals(1, deleteResponse.getVersion());
|
||||
}
|
||||
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());
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id").version(2);
|
||||
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
|
||||
() -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync));
|
||||
assertEquals(RestStatus.CONFLICT, exception.status());
|
||||
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " +
|
||||
"version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
|
||||
assertEquals("index", exception.getMetadata("es.index").get(0));
|
||||
}
|
||||
{
|
||||
DeleteRequest deleteRequest = new DeleteRequest("index", "type", "id");
|
||||
if (randomBoolean()) {
|
||||
deleteRequest.version(1L);
|
||||
}
|
||||
DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
|
||||
assertEquals("index", deleteResponse.getIndex());
|
||||
assertEquals("type", deleteResponse.getType());
|
||||
assertEquals("id", deleteResponse.getId());
|
||||
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
|
||||
}
|
||||
/**
|
||||
* @return A sample index request
|
||||
* @throws IOException If problem when generating the content
|
||||
*/
|
||||
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());
|
||||
return indexRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an index request and returns the document _id
|
||||
* @param request index request
|
||||
* @return the document _id
|
||||
* @throws IOException if something goes wrong while executing the index request
|
||||
*/
|
||||
private String createSampleDocument(IndexRequest request) throws IOException {
|
||||
IndexResponse indexResponse = execute(request, highLevelClient()::index, highLevelClient()::indexAsync);
|
||||
assertEquals(RestStatus.CREATED, indexResponse.status());
|
||||
return indexResponse.getId();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue