Bulk Delete elastic-search client

How to use deletebyQuery elastic 8.x version ?

public long deleteESDocuments(String indexName, Map<String, Object> termQueryMap, Map<String, Object[]> rangeQueryMap) throws IOException{
        log.debug("Inside deleteESDocuments method");
        DeleteByQueryRequest request = null;
        BulkByScrollResponse bulkResponse = null;
        BoolQueryBuilder builder = null;
        request = new DeleteByQueryRequest(indexName);
        builder = QueryBuilders.boolQuery();
        for(String key : termQueryMap.keySet()) {
            builder.must(QueryBuilders.termQuery(key, termQueryMap.get(key)));
        }
        for (String key: rangeQueryMap.keySet()){
            Object[] range = rangeQueryMap.get(key);
            log.debug("{} range from {} to {}, ", key,range[0],range[1]);
            builder.must(QueryBuilders.rangeQuery(key).gte(range[0]).lte(range[1]));
        }
        log.debug("builderQuery is: {} ", builder);
        request.setQuery(builder);
        bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
        log.debug("ES Document Deletion response: {}",bulkResponse.getStatus());
        log.debug("{} ES Document/s is/are deleted.",bulkResponse.getDeleted());

        return bulkResponse.getDeleted();
    }

Hi @Manoj_Upadhyay,

Are you receiving a particular error from the above code? Or is it not working in the way that you expect?

Hi Carly,

After ES 8. x upgrade, am using ElasticsearchClient in place of RestHighClient and don't see an option to use deleteByQuery. Tried to read the document but saw only option the to search first and then all IDs in DeleteByQueryRequest then delete them.

Have a look at this example:

Thanks a lot. if there is any direct APIs(similar to ES 7) to delete documents in bulk in place of finding the search result, iterating hits, and adding them in DeleteByQueryRequest then deleting them in bulk.