refactor elastic cache

pull/8989/head
Alper Ebicoglu 5 years ago
parent 64fa32b23c
commit 636cf02de7

@ -27,7 +27,7 @@ namespace Volo.Docs.Admin.Documents
private readonly IDistributedCache<DocumentUpdateInfo> _documentUpdateCache;
private readonly IDistributedCache<List<VersionInfo>> _versionCache;
private readonly IDistributedCache<LanguageConfig> _languageCache;
private readonly IDocumentFullSearch _documentFullSearch;
private readonly IDocumentFullSearch _elasticSearchService;
public DocumentAdminAppService(IProjectRepository projectRepository,
IDocumentRepository documentRepository,
@ -35,7 +35,7 @@ namespace Volo.Docs.Admin.Documents
IDistributedCache<DocumentUpdateInfo> documentUpdateCache,
IDistributedCache<List<VersionInfo>> versionCache,
IDistributedCache<LanguageConfig> languageCache,
IDocumentFullSearch documentFullSearch)
IDocumentFullSearch elasticSearchService)
{
_projectRepository = projectRepository;
_documentRepository = documentRepository;
@ -43,7 +43,7 @@ namespace Volo.Docs.Admin.Documents
_documentUpdateCache = documentUpdateCache;
_versionCache = versionCache;
_languageCache = languageCache;
_documentFullSearch = documentFullSearch;
_elasticSearchService = elasticSearchService;
LocalizationResource = typeof(DocsResource);
}
@ -210,9 +210,11 @@ namespace Volo.Docs.Admin.Documents
public async Task ReindexAsync(Guid documentId)
{
await _documentFullSearch.DeleteAsync(documentId);
_elasticSearchService.ValidateElasticSearchEnabled();
await _elasticSearchService.DeleteAsync(documentId);
var document = await _documentRepository.GetAsync(documentId);
await _documentFullSearch.AddOrUpdateAsync(document);
await _elasticSearchService.AddOrUpdateAsync(document);
}
private async Task UpdateDocumentUpdateInfoCache(Document document)

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
@ -18,13 +17,13 @@ namespace Volo.Docs.Admin.Projects
{
private readonly IProjectRepository _projectRepository;
private readonly IDocumentRepository _documentRepository;
private readonly IDocumentFullSearch _documentFullSearch;
private readonly IDocumentFullSearch _elasticSearchService;
private readonly IGuidGenerator _guidGenerator;
public ProjectAdminAppService(
IProjectRepository projectRepository,
IDocumentRepository documentRepository,
IDocumentFullSearch documentFullSearch,
IDocumentFullSearch elasticSearchService,
IGuidGenerator guidGenerator)
{
ObjectMapperContext = typeof(DocsAdminApplicationModule);
@ -32,7 +31,7 @@ namespace Volo.Docs.Admin.Projects
_projectRepository = projectRepository;
_documentRepository = documentRepository;
_documentFullSearch = documentFullSearch;
_elasticSearchService = elasticSearchService;
_guidGenerator = guidGenerator;
}
@ -120,11 +119,21 @@ namespace Volo.Docs.Admin.Projects
public async Task ReindexAsync(ReindexInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
_elasticSearchService.ValidateElasticSearchEnabled();
await _documentFullSearch.DeleteAllByProjectIdAsync(project.Id);
await ReindexProjectAsync(input.ProjectId);
}
private async Task ReindexProjectAsync(Guid projectId)
{
var project = await _projectRepository.FindAsync(projectId);
if (project == null)
{
throw new Exception("Cannot find the project with the Id " + projectId);
}
var docs = await _documentRepository.GetListByProjectId(project.Id);
await _elasticSearchService.DeleteAllByProjectIdAsync(project.Id);
foreach (var doc in docs)
{
@ -138,35 +147,18 @@ namespace Volo.Docs.Admin.Projects
continue;
}
await _documentFullSearch.AddOrUpdateAsync(doc);
await _elasticSearchService.AddOrUpdateAsync(doc);
}
}
public async Task ReindexAllAsync()
{
await _documentFullSearch.DeleteAllAsync();
var docs = await _documentRepository.GetListAsync();
_elasticSearchService.ValidateElasticSearchEnabled();
var projects = await _projectRepository.GetListAsync();
foreach (var doc in docs)
{
var project = projects.FirstOrDefault(x => x.Id == doc.ProjectId);
if (project == null)
{
continue;
}
if (doc.FileName == project.NavigationDocumentName)
foreach (var project in projects)
{
continue;
}
if (doc.FileName == project.ParametersDocumentName)
{
continue;
}
await _documentFullSearch.AddOrUpdateAsync(doc);
await ReindexProjectAsync(project.Id);
}
}
}

@ -56,8 +56,6 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
public virtual async Task AddOrUpdateAsync(Document document, CancellationToken cancellationToken = default)
{
ValidateElasticSearchEnabled();
var client = _clientProvider.GetClient();
var esDocument = new EsDocument
@ -76,8 +74,6 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
public virtual async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
{
ValidateElasticSearchEnabled();
HandleError(await _clientProvider.GetClient()
.DeleteAsync(DocumentPath<Document>.Id(NormalizeField(id)), x => x.Index(_options.IndexName), cancellationToken));
}
@ -210,7 +206,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
}
}
protected virtual void ValidateElasticSearchEnabled()
public virtual void ValidateElasticSearchEnabled()
{
if (!_options.Enable)
{

@ -20,5 +20,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
Task<List<EsDocument>> SearchAsync(string context, Guid projectId, string languageCode,
string version, int? skipCount = null, int? maxResultCount = null,
CancellationToken cancellationToken = default);
void ValidateElasticSearchEnabled();
}
}

Loading…
Cancel
Save