Skip to content

Commit

Permalink
Reuse same function when activating new index
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkikos committed Jan 12, 2024
1 parent b3f9b2e commit 6b5533d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
18 changes: 2 additions & 16 deletions aspnetcore/src/ElasticService/ElasticSearchIndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,8 @@ public async Task IndexAsync(string indexName, List<object> entities, Type model
// Add entities to the new index.
await IndexEntities(indexToCreate, entities, modelType);

// Wait for new index to be operational.
await _elasticClient.Cluster
.HealthAsync(selector: s => s
.WaitForStatus(Elasticsearch.Net.WaitForStatus.Yellow)
.WaitForActiveShards("1")
.Index(indexToCreate));

// Add new alias from index_new to index
await _elasticClient.Indices.BulkAliasAsync(r => r
// Remove alias "index_old => index"
.Remove(remove => remove.Alias(indexName).Index("*"))
// Add alias "index_new => index"
.Add(add => add.Alias(indexName).Index(indexToCreate)));

// Delete the old index if it exists.
await _elasticClient.Indices.DeleteAsync(indexToDelete, d => d.RequestConfiguration(x => x.AllowedStatusCodes(404)));
// Switch indexes
await SwitchIndexes(indexName, indexToCreate, indexToDelete);

_logger.LogDebug("{EntityType}: Indexing to {IndexName} complete", modelType.Name, indexName);
}
Expand Down
6 changes: 4 additions & 2 deletions aspnetcore/src/Indexer/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private async Task IndexEntities(string indexName,

if (indexName.Contains("publication")) {

// Create new index
var (indexToCreate, indexToDelete) = await _indexService.GetIndexNames(indexName);
await _indexService.CreateIndex(indexToCreate, type);

Expand Down Expand Up @@ -166,11 +167,12 @@ private async Task IndexEntities(string indexName,
await _indexService.IndexChunkAsync(indexToCreate, finalized, type);
}
skipAmount = skipAmount + takeAmount;
finalized = new();
processedCount = processedCount + numOfResults;
_logger.LogInformation("{EntityType}: Total indexed count = {processedCount}", type.Name, processedCount);
finalized = new();
_logger.LogInformation("{EntityType}: Total documents indexed = {processedCount}", type.Name, processedCount);
} while(numOfResults >= takeAmount-1);

// Activate new index and delete old
await _indexService.SwitchIndexes(indexName, indexToCreate, indexToDelete);
}
else
Expand Down

0 comments on commit 6b5533d

Please sign in to comment.