Java Code Examples for org.elasticsearch.action.delete.DeleteResponse#status()

The following examples show how to use org.elasticsearch.action.delete.DeleteResponse#status() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfService.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
private void callRemoveES(ConfigurationLogstash cl) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(INDEX_STORAGE, "doc", cl.getIdEs());
    DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest);
    if (deleteResponse != null) {
        deleteResponse.status();
        deleteResponse.toString();
        cl.setIdEs(null);
    }
}
 
Example 2
Source File: DeleteElasticsearch5.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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();
    }
}