0
votes

I've written a reindex method that does the following:

public void reindex() {
    IndexOperations indexOperations = elasticsearchOperations.indexOps(Song.class);
    List<Song> songs = songRepository.findAll();
    songSearchRepository.deleteAll();
    indexOperations.delete();
    indexOperations.create();
    songSearchRepository.saveAll(songs);
}

It does the job but I'm now sure whether it makes sence just to delete and then create an index. How can I improve this method?

1

1 Answers

1
votes

Actually I don't see the point in reindexing to the same index.

If you want to reindex to a different index, you should use the reindex API of Elasticsearch. This is not yet supported directly in Spring Data Elasticsearch.

To reindex to a different index using Spring Data Elasticsearch you should use paged queries in a loop to read from one index and write the data to the second index.

Edit 16.10.2020:

Don't use copying in a loop as I suggested, like @joeyave commented, the indices can get out of sync here.

I created an issue in Spring Data Elasticsearch Jira to have reindex support implemented.