Unfortunately, the NEST implementation is not quite what I expected. In my opinion, this is a bit overhauled, perhaps for the most common use case.
Many people just want to update their comparisons with zero downtime ...
In my case, I already took care of creating the index with all its settings and mappings, but NEST insists that it must create a new index when reindexing. This is among many other things. Too many other things.
It was much harder for me to simply implement directly - since NEST already has the Search , Scroll and Bulk methods. (this is taken from the NEST implementation):
// Assuming you have already created and setup the index yourself public void Reindex(ElasticClient client, string aliasName, string currentIndexName, string nextIndexName) { Console.WriteLine("Reindexing documents to new index..."); var searchResult = client.Search<object>(s => s.Index(currentIndexName).AllTypes().From(0).Size(100).Query(q => q.MatchAll()).SearchType(SearchType.Scan).Scroll("2m")); if (searchResult.Total <= 0) { Console.WriteLine("Existing index has no documents, nothing to reindex."); } else { var page = 0; IBulkResponse bulkResponse = null; do { var result = searchResult; searchResult = client.Scroll<object>(s => s.Scroll("2m").ScrollId(result.ScrollId)); if (searchResult.Documents != null && searchResult.Documents.Any()) { searchResult.ThrowOnError("reindex scroll " + page); bulkResponse = client.Bulk(b => { foreach (var hit in searchResult.Hits) { b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id)); } return b; }).ThrowOnError("reindex page " + page); Console.WriteLine("Reindexing progress: " + (page + 1) * 100); } ++page; } while (searchResult.IsValid && bulkResponse != null && bulkResponse.IsValid && searchResult.Documents != null && searchResult.Documents.Any()); Console.WriteLine("Reindexing complete!"); } Console.WriteLine("Updating alias to point to new index..."); client.Alias(a => a .Add(aa => aa.Alias(aliasName).Index(nextIndexName)) .Remove(aa => aa.Alias(aliasName).Index(currentIndexName))); // TODO: Don't forget to delete the old index if you want }
And the extension method ThrowOnError in case you want it:
public static T ThrowOnError<T>(this T response, string actionDescription = null) where T : IResponse { if (!response.IsValid) { throw new CustomExceptionOfYourChoice(actionDescription == null ? string.Empty : "Failed to " + actionDescription + ": " + response.ServerError.Error); } return response; }
source share