Removing all documents with a missing index in the elasticsearch java API

Is there any simple Java API to remove all documents from elastic search without dropping the index.

I know that we could get all the identifiers and delete each document one at a time

DeleteResponse response = _client.prepareDelete(INDEX, TYPE, id)
            .setRefresh(true)
            .execute()
            .actionGet();

But I was looking for a TRUNCATE script.

I am currently deleting the index and recreating the mapping in unit tests.

+4
source share
4 answers

To achieve this, you can use the plugin to remove it on demand .

You need to install it on all nodes using

sudo bin/plugin install delete-by-query

Then you can add this dependency to your pom.xml

<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>delete-by-query</artifactId>
    <version>2.2.0</version>
</dependency>

, DeleteByQueryRequestBuilder, .

+3

, ES, . -0 -1 , , "".

, , my-data, my-data-0 my-data-1. my-data, my-data-0

, my-data, my-data-1, , my-data-0, , , re . my-data-0, . , , , .

, (, https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html).

, , ... . , , , . , , , .

+1

elaseticsearchservice.functioncall - , es

-

          DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = elasticsearchService.deleteAllDocument(PRODUCTSINDEX);
            deleteByQueryRequestBuilder.execute(new ActionListener<BulkIndexByScrollResponse>() {
                @Override
                public void onResponse(BulkIndexByScrollResponse bulkIndexByScrollResponse) {
                    if (bulkIndexByScrollResponse.getDeleted() == 0) {

                    } else if (bulkIndexByScrollResponse.getDeleted() > 0) {
                       // getDeleted() gives you how many docs in that index have been deleted
                    }
                }

                @Override
                public void onFailure(Exception e) {
                   //reason like no index exists, improper connection etc
                }
            });

  ------------------------------------------------------------------  
      public DeleteByQueryRequestBuilder deleteAllDocument(String indexName) {
            DeleteByQueryRequestBuilder DeleteByQueryRequestBuilder = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                    .filter(QueryBuilders.matchAllQuery())
                    .source(indexName);
            return DeleteByQueryRequestBuilder;

        }

The usual way is

public void deleteAllProduct(RoutingContext routingContext) {
        long starttime = System.currentTimeMillis();
       elasticsearchService.deleteAllDocument1(PRODUCTSINDEX);

    }
--------------------------------------------------------------

  public void deleteAllDocument1(String indexName) {
        ListenableActionFuture<BulkIndexByScrollResponse> execute = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchAllQuery())
                .source(indexName).execute();


        try {
            BulkIndexByScrollResponse   bulkIndexByScrollResponse = execute.get();
            if (bulkIndexByScrollResponse.getDeleted()>0)
            {
                System.out.println(bulkIndexByScrollResponse.getDeleted());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }
0
source

You can crop by clicking on the search results and deleting them:

    public static void truncateESNamespace(String namespace) {
    client.admin().indices().prepareRefresh(namespace).get();
    SearchResponse response = client.prepareSearch(namespace).setSize(10000).get();
    while (response.getHits().totalHits > 0) {
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        Arrays.stream(response.getHits().getHits()).forEach(h -> bulkRequest.add(client.prepareDelete(h.getIndex(), h.getType(), h.getId())));
        bulkRequest.get();
        client.admin().indices().prepareRefresh(namespace).get();
        response = client.prepareSearch(namespace).setSize(10000).get();
    }
}
0
source

All Articles