Skip to content

Commit

Permalink
Use ReadOnly Session when indixing content items (OrchardCMS#15216)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Feb 1, 2024
1 parent 6c73723 commit 187b0e8
Showing 1 changed file with 6 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AzureAISearchIndexingService
private readonly IIndexingTaskManager _indexingTaskManager;
private readonly AzureAISearchIndexSettingsService _azureAISearchIndexSettingsService;
private readonly AzureAIIndexDocumentManager _indexDocumentManager;
private readonly ISession _session;
private readonly IStore _store;
private readonly IContentManager _contentManager;
private readonly IEnumerable<IContentItemIndexHandler> _contentItemIndexHandlers;
private readonly ILogger _logger;
Expand All @@ -30,15 +30,15 @@ public class AzureAISearchIndexingService
IIndexingTaskManager indexingTaskManager,
AzureAISearchIndexSettingsService azureAISearchIndexSettingsService,
AzureAIIndexDocumentManager indexDocumentManager,
ISession session,
IStore store,
IContentManager contentManager,
IEnumerable<IContentItemIndexHandler> contentItemIndexHandlers,
ILogger<AzureAISearchIndexingService> logger)
{
_indexingTaskManager = indexingTaskManager;
_azureAISearchIndexSettingsService = azureAISearchIndexSettingsService;
_indexDocumentManager = indexDocumentManager;
_session = session;
_store = store;
_contentManager = contentManager;
_contentItemIndexHandlers = contentItemIndexHandlers;
_logger = logger;
Expand Down Expand Up @@ -81,6 +81,7 @@ public async Task ProcessContentItemsAsync(params string[] indexNames)
var tasks = new List<IndexingTask>();

var allContentTypes = indexSettings.SelectMany(x => x.IndexedContentTypes ?? []).Distinct().ToList();
var readOnlySession = _store.CreateSession(withTracking: false);

while (tasks.Count <= _batchSize)
{
Expand All @@ -107,25 +108,15 @@ public async Task ProcessContentItemsAsync(params string[] indexNames)

if (indexSettings.Any(x => !x.IndexLatest))
{
var publishedContentItems = await _session.Query<ContentItem, ContentItemIndex>(index => index.Published && index.ContentType.IsIn(allContentTypes) && index.ContentItemId.IsIn(updatedContentItemIds)).ListAsync();
var publishedContentItems = await readOnlySession.Query<ContentItem, ContentItemIndex>(index => index.Published && index.ContentType.IsIn(allContentTypes) && index.ContentItemId.IsIn(updatedContentItemIds)).ListAsync();
allPublished = publishedContentItems.DistinctBy(x => x.ContentItemId)
.ToDictionary(k => k.ContentItemId);

foreach (var publishedContentItem in publishedContentItems)
{
_session.Detach(publishedContentItem);
}
}

if (indexSettings.Any(x => x.IndexLatest))
{
var latestContentItems = await _session.Query<ContentItem, ContentItemIndex>(index => index.Latest && index.ContentType.IsIn(allContentTypes) && index.ContentItemId.IsIn(updatedContentItemIds)).ListAsync();
var latestContentItems = await readOnlySession.Query<ContentItem, ContentItemIndex>(index => index.Latest && index.ContentType.IsIn(allContentTypes) && index.ContentItemId.IsIn(updatedContentItemIds)).ListAsync();
allLatest = latestContentItems.DistinctBy(x => x.ContentItemId).ToDictionary(k => k.ContentItemId);

foreach (var latestContentItem in latestContentItems)
{
_session.Detach(latestContentItem);
}
}

foreach (var task in tasks)
Expand Down

0 comments on commit 187b0e8

Please sign in to comment.