use of org.elasticsearch.action.delete.DeleteResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method deleteRequestBody.
@Test
public void deleteRequestBody() throws Exception {
String prefix = createPrefix();
// given
DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar");
// when
String documentId = template.requestBody("direct:index", new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
DeleteResponse response = template.requestBody("direct:delete", request.id(documentId), DeleteResponse.class);
// then
assertThat(response, notNullValue());
assertThat(documentId, equalTo(response.getId()));
}
use of org.elasticsearch.action.delete.DeleteResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testDelete.
@Test
public void testDelete() throws Exception {
//first, INDEX a value
Map<String, String> map = createIndexedData();
sendBody("direct:index", map);
String indexId = template.requestBody("direct:index", map, String.class);
assertNotNull("indexId should be set", indexId);
//now, verify GET succeeded
GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class);
assertNotNull("response should not be null", response);
assertNotNull("response source should not be null", response.getSource());
//now, perform DELETE
DeleteResponse deleteResponse = template.requestBody("direct:delete", indexId, DeleteResponse.class);
assertNotNull("response should not be null", deleteResponse);
//now, verify GET fails to find the indexed value
response = template.requestBody("direct:get", indexId, GetResponse.class);
assertNotNull("response should not be null", response);
assertNull("response source should be null", response.getSource());
}
use of org.elasticsearch.action.delete.DeleteResponse in project nifi by apache.
the class DeleteElasticsearch5 method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
synchronized (esClient) {
if (esClient.get() == null) {
setup(context);
}
}
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
final String documentId = context.getProperty(DOCUMENT_ID).evaluateAttributeExpressions(flowFile).getValue();
final String documentType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
final ComponentLog logger = getLogger();
if (StringUtils.isBlank(index)) {
logger.debug("Index is required but was empty {}", new Object[] { index });
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, "Index is required but was empty");
session.transfer(flowFile, REL_FAILURE);
return;
}
if (StringUtils.isBlank(documentType)) {
logger.debug("Document type is required but was empty {}", new Object[] { documentType });
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, "Document type is required but was empty");
session.transfer(flowFile, REL_FAILURE);
return;
}
if (StringUtils.isBlank(documentId)) {
logger.debug("Document id is required but was empty {}", new Object[] { documentId });
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, "Document id is required but was empty");
session.transfer(flowFile, REL_FAILURE);
return;
}
flowFile = session.putAllAttributes(flowFile, new HashMap<String, String>() {
{
put(ES_FILENAME, documentId);
put(ES_INDEX, index);
put(ES_TYPE, documentType);
}
});
try {
logger.debug("Deleting document {}/{}/{} from Elasticsearch", new Object[] { index, documentType, documentId });
DeleteRequestBuilder requestBuilder = prepareDeleteRequest(index, documentId, documentType);
final DeleteResponse response = doDelete(requestBuilder);
if (response.status() != RestStatus.OK) {
logger.warn("Failed to delete document {}/{}/{} from Elasticsearch: Status {}", new Object[] { index, documentType, documentId, response.status() });
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, UNABLE_TO_DELETE_DOCUMENT_MESSAGE);
flowFile = session.putAttribute(flowFile, ES_REST_STATUS, response.status().toString());
context.yield();
if (response.status() == RestStatus.NOT_FOUND) {
session.transfer(flowFile, REL_NOT_FOUND);
} else {
session.transfer(flowFile, REL_FAILURE);
}
} else {
logger.debug("Elasticsearch document " + documentId + " deleted");
session.transfer(flowFile, REL_SUCCESS);
}
} catch (ElasticsearchTimeoutException | ReceiveTimeoutTransportException exception) {
logger.error("Failed to delete document {} from Elasticsearch due to {}", new Object[] { documentId, exception.getLocalizedMessage() }, exception);
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, exception.getLocalizedMessage());
session.transfer(flowFile, REL_RETRY);
context.yield();
} catch (Exception e) {
logger.error("Failed to delete document {} from Elasticsearch due to {}", new Object[] { documentId, e.getLocalizedMessage() }, e);
flowFile = session.putAttribute(flowFile, ES_ERROR_MESSAGE, e.getLocalizedMessage());
session.transfer(flowFile, REL_FAILURE);
context.yield();
}
}
use of org.elasticsearch.action.delete.DeleteResponse in project nifi by apache.
the class TestDeleteElasticsearch5 method testDeleteSuccessful.
@Test
public void testDeleteSuccessful() throws IOException {
restStatus = RestStatus.OK;
deleteResponse = new DeleteResponse(null, TYPE1, documentId, 1, true) {
@Override
public RestStatus status() {
return restStatus;
}
};
runner.enqueue(new byte[] {}, new HashMap<String, String>() {
{
put("documentId", documentId);
}
});
runner.run(1, true, true);
runner.assertAllFlowFilesTransferred(DeleteElasticsearch5.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(DeleteElasticsearch5.REL_SUCCESS).get(0);
assertNotNull(out);
assertEquals(null, out.getAttribute(DeleteElasticsearch5.ES_ERROR_MESSAGE));
out.assertAttributeEquals(DeleteElasticsearch5.ES_FILENAME, documentId);
out.assertAttributeEquals(DeleteElasticsearch5.ES_INDEX, INDEX1);
out.assertAttributeEquals(DeleteElasticsearch5.ES_TYPE, TYPE1);
}
use of org.elasticsearch.action.delete.DeleteResponse in project nifi by apache.
the class TestDeleteElasticsearch5 method testDeleteNonRetryableException.
@Test
public void testDeleteNonRetryableException() throws IOException {
mockDeleteProcessor = new DeleteElasticsearch5() {
@Override
protected DeleteRequestBuilder prepareDeleteRequest(String index, String docId, String docType) {
return null;
}
@Override
protected DeleteResponse doDelete(DeleteRequestBuilder requestBuilder) throws InterruptedException, ExecutionException {
throw new InterruptedException("exception");
}
@Override
public void setup(ProcessContext context) {
}
};
runner = TestRunners.newTestRunner(mockDeleteProcessor);
runner.setValidateExpressionUsage(true);
runner.setProperty(AbstractElasticsearch5TransportClientProcessor.CLUSTER_NAME, "elasticsearch");
runner.setProperty(AbstractElasticsearch5TransportClientProcessor.HOSTS, "127.0.0.1:9300");
runner.setProperty(AbstractElasticsearch5TransportClientProcessor.PING_TIMEOUT, "5s");
runner.setProperty(AbstractElasticsearch5TransportClientProcessor.SAMPLER_INTERVAL, "5s");
runner.setProperty(DeleteElasticsearch5.INDEX, INDEX1);
runner.setProperty(DeleteElasticsearch5.TYPE, TYPE1);
runner.setProperty(DeleteElasticsearch5.DOCUMENT_ID, "${documentId}");
runner.assertValid();
runner.enqueue(new byte[] {}, new HashMap<String, String>() {
{
put("documentId", documentId);
}
});
runner.run(1, true, true);
runner.assertAllFlowFilesTransferred(DeleteElasticsearch5.REL_FAILURE, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(DeleteElasticsearch5.REL_FAILURE).get(0);
assertNotNull(out);
assertEquals("exception", out.getAttribute(DeleteElasticsearch5.ES_ERROR_MESSAGE));
out.assertAttributeEquals(DeleteElasticsearch5.ES_REST_STATUS, null);
out.assertAttributeEquals(DeleteElasticsearch5.ES_FILENAME, documentId);
out.assertAttributeEquals(DeleteElasticsearch5.ES_INDEX, INDEX1);
out.assertAttributeEquals(DeleteElasticsearch5.ES_TYPE, TYPE1);
}
Aggregations