public async Task IndexAsnyc(CancellationToken cancellationToken = default) { await Sync.WaitAsync(cancellationToken); try { if (!await _searchRepository.IndexExistsAsync(cancellationToken)) { _logger.LogInformation("Creating Elastic index"); await _searchRepository.CreateIndexAsync(cancellationToken); } var expectedCount = await _contentRepository.CountAsync(cancellationToken); var actualCount = await _searchRepository.CountAsync(cancellationToken); if (expectedCount != actualCount) { var count = 0; var page = 1; var pageSize = 1000; while (count < expectedCount) { _logger.LogInformation("Indexing elastic documents {Count} / {Total}", count, expectedCount); var docs = await _contentRepository.ListAsync(page ++, pageSize, cancellationToken); await _searchRepository.IndexAsync(docs.Select(_mapper.Map <Models.Elastic.Content>), cancellationToken); count += docs.Count; } } _logger.LogInformation("Elastic index is up to date"); } finally { Sync.Release(); } }