Home > other >  Delete all documents from index using ElasticsearchOperations
Delete all documents from index using ElasticsearchOperations

Time:02-16

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related