Search code examples
javaelasticsearchspring-data-elasticsearch

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

  • I've found the solution:

    @BeforeEach
    void beforeEach() {
      IndexCoordinates coordinates = elasticsearchOperations.getIndexCoordinatesFor(ReviewRequestDocument.class);
    
      Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
      String[] ids = elasticsearchOperations.search(query, ReviewRequestDocument.class, coordinates)
              .stream()
              .map(SearchHit::getId)
              .toArray(String[]::new);
    
      Query idsQuery = new NativeSearchQueryBuilder().withQuery(idsQuery().addIds(ids)).build();
      elasticsearchOperations.delete(idsQuery, ReviewRequestDocument.class, coordinates);
    
      assertThat(countReviewRequestDocuments()).isZero();
    }
    

    it looks like ES does not allow bulk removal of all documents in index, so the solution is to fetch ids and then pass them into delete query.