Search in sources :

Example 36 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project molgenis by molgenis.

the class ClientFacade method deleteById.

public void deleteById(Index index, Document document) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Deleting doc with id '{}' in index '{}' ...", document.getId(), index.getName());
    }
    String indexName = index.getName();
    String documentId = document.getId();
    DeleteRequestBuilder deleteRequest = client.prepareDelete().setIndex(indexName).setType(indexName).setId(documentId);
    DeleteResponse deleteResponse;
    try {
        deleteResponse = deleteRequest.get();
    } catch (ResourceNotFoundException e) {
        LOG.error("", e);
        throw new UnknownIndexException(index.getName());
    } catch (ElasticsearchException e) {
        LOG.debug("", e);
        throw new IndexException(format("Error deleting doc with id '%s' in index '%s'.", documentId, indexName));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleted doc with id '{}' in index '{}' and status '{}'", documentId, indexName, deleteResponse.getResult());
    }
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException)

Example 37 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project opencast by opencast.

the class AbstractSearchIndex method delete.

@Override
public boolean delete(String documentType, String uid) throws SearchIndexException {
    logger.debug("Removing element with id '{}' from searching index '{}'", uid, getIndexName());
    DeleteRequestBuilder deleteRequest = getSearchClient().prepareDelete(getIndexName(), documentType, uid);
    deleteRequest.setRefresh(true);
    DeleteResponse delete = deleteRequest.execute().actionGet();
    if (!delete.isFound()) {
        logger.trace("Document {} to delete was not found on index '{}'", uid, getIndexName());
        return false;
    }
    return true;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse)

Example 38 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project opencast by opencast.

the class AbstractElasticsearchIndex method delete.

/**
 * Removes the given document from the specified index.
 *
 * @param type
 *          the document type
 * @param uid
 *          the identifier
 * @return <code>true</code> if the element was found and deleted
 * @throws SearchIndexException
 *           if deletion fails
 */
protected boolean delete(String type, String uid) throws SearchIndexException {
    if (!preparedIndices.contains(index)) {
        try {
            createIndex(index);
        } catch (IOException e) {
            throw new SearchIndexException(e);
        }
    }
    logger.debug("Removing element with id '{}' from searching index", uid);
    DeleteRequestBuilder deleteRequest = nodeClient.prepareDelete(index, type, uid);
    deleteRequest.setRefresh(true);
    DeleteResponse delete = deleteRequest.execute().actionGet();
    if (!delete.isFound()) {
        logger.trace("Document {} to delete was not found", uid);
        return false;
    }
    return true;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) IOException(java.io.IOException)

Example 39 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project wildfly-camel by wildfly-extras.

the class Elasticsearch5IntegrationTest method testGetSearchUpdateDelete.

@Test
public void testGetSearchUpdateDelete() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:index").to("elasticsearch5://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost&port=" + ES_TRANSPORT_PORT);
            from("direct:get").to("elasticsearch5://elasticsearch?operation=GET_BY_ID&indexName=twitter&indexType=tweet&ip=localhost&port=" + ES_TRANSPORT_PORT);
            from("direct:search").to("elasticsearch5://elasticsearch?operation=SEARCH&indexName=twitter&indexType=tweet&ip=localhost&port=" + ES_TRANSPORT_PORT);
            from("direct:update").to("elasticsearch5://elasticsearch?operation=UPDATE&indexName=twitter&indexType=tweet&ip=localhost&port=" + ES_TRANSPORT_PORT);
            from("direct:delete").to("elasticsearch5://elasticsearch?operation=DELETE&indexName=twitter&indexType=tweet&ip=localhost&port=" + ES_TRANSPORT_PORT);
        }
    });
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        // first, INDEX a value
        Map<String, String> map = createIndexedData();
        template.sendBody("direct:index", map);
        String indexId = template.requestBody("direct:index", map, String.class);
        Assert.assertNotNull("indexId should be set", indexId);
        // now, verify GET succeeded
        GetResponse getres = template.requestBody("direct:get", indexId, GetResponse.class);
        Assert.assertNotNull("response should not be null", getres);
        Assert.assertNotNull("response source should not be null", getres.getSource());
        // now, verify GET succeeded
        String query = "{\"query\":{\"match\":{\"content\":\"searchtest\"}}}";
        SearchResponse searchres = template.requestBody("direct:search", query, SearchResponse.class);
        Assert.assertNotNull("response should not be null", searchres);
        Assert.assertNotNull("response hits should be == 1", searchres.getHits().totalHits());
        Map<String, String> newMap = new HashMap<>();
        newMap.put(createPrefix() + "key2", createPrefix() + "value2");
        Map<String, Object> headers = new HashMap<>();
        headers.put(ElasticsearchConstants.PARAM_INDEX_ID, indexId);
        indexId = template.requestBodyAndHeaders("direct:update", newMap, headers, String.class);
        Assert.assertNotNull("indexId should be set", indexId);
        // now, perform DELETE
        DeleteResponse delres = template.requestBody("direct:delete", indexId, DeleteResponse.class);
        Assert.assertNotNull("response should not be null", delres);
        // now, verify GET fails to find the indexed value
        getres = template.requestBody("direct:get", indexId, GetResponse.class);
        Assert.assertNotNull("response should not be null", getres);
        Assert.assertNull("response source should be null", getres.getSource());
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) GetResponse(org.elasticsearch.action.get.GetResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) Test(org.junit.Test)

Example 40 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project kylo by Teradata.

the class IndexElasticSearchTest method cleanUp.

@After
public void cleanUp() {
    DeleteResponse response = esClient.prepareDelete().setIndex(TEST_INDEX).setType(TEST_TYPE).setId("*").get();
    esClient.close();
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) After(org.junit.After)

Aggregations

DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)42 Test (org.junit.Test)14 IndexResponse (org.elasticsearch.action.index.IndexResponse)13 GetResponse (org.elasticsearch.action.get.GetResponse)11 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 HashMap (java.util.HashMap)7 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)7 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)3 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2