'Delete all documents from index using ElasticsearchOperations

I'm trying to delete all the documents from particular index of ES using the following code:

@Autowired
protected ElasticsearchOperations elasticsearchOperations;

@BeforeEach
void beforeEach() {
    Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    elasticsearchOperations.delete(query, elasticsearchOperations.getIndexCoordinatesFor(ReviewRequestDocument.class));
}

which fails with

java.lang.IllegalArgumentException: id must not be null

I'm puzzled, because similar approach works for counting documents in index:

private long countReviewRequestDocuments() {
    Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
    return elasticsearchOperations.count(query, ReviewRequestDocument.class);
}

So my question is what is the correct way to remove all documents from index?



Solution 1:[1]

deleting all the documents from an index is super inefficient in Elasticsearch and is not the way to go about this

you are far better off deleting the index and then recreating it from code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Walkom