Delete By Query API

edit

Delete By Query API

edit

The delete by query API allows one to delete a given set of documents based on the result of a query:

BulkByScrollResponse response =
  new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
    .filter(QueryBuilders.matchQuery("gender", "male")) 
    .source("persons")                                  
    .get();                                             
long deleted = response.getDeleted();                   

query

index

execute the operation

number of deleted documents

As it can be a long running operation, if you wish to do it asynchronously, you can call execute instead of get and provide a listener like:

new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
    .filter(QueryBuilders.matchQuery("gender", "male"))     
    .source("persons")                                      
    .execute(new ActionListener<BulkByScrollResponse>() {   
        @Override
        public void onResponse(BulkByScrollResponse response) {
            long deleted = response.getDeleted();           
        }
        @Override
        public void onFailure(Exception e) {
            // Handle the exception
        }
    });

query

index

listener

number of deleted documents