Enhanced reindex function.

Resolve #3718
pull/3721/head
maliming 6 years ago
parent fa310ad364
commit ea9d13c4ff

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
namespace Volo.Docs.Admin.Documents
@ -10,7 +11,5 @@ namespace Volo.Docs.Admin.Documents
Task PullAllAsync(PullAllDocumentInput input);
Task PullAsync(PullDocumentInput input);
Task ReindexAsync();
}
}

@ -16,5 +16,9 @@ namespace Volo.Docs.Admin.Projects
Task<ProjectDto> UpdateAsync(Guid id, UpdateProjectDto input);
Task DeleteAsync(Guid id);
Task ReindexAsync(ReindexInput input);
Task ReindexAllAsync();
}
}

@ -0,0 +1,9 @@
using System;
namespace Volo.Docs.Admin.Projects
{
public class ReindexInput
{
public Guid ProjectId { get; set; }
}
}

@ -25,17 +25,13 @@ namespace Volo.Docs.Admin.Documents
private readonly IDistributedCache<DocumentUpdateInfo> _documentUpdateCache;
private readonly IDistributedCache<List<VersionInfo>> _versionCache;
private readonly IDistributedCache<LanguageConfig> _languageCache;
private readonly IDistributedCache<DocumentResource> _resourceCache;
private readonly IDocumentFullSearch _documentFullSearch;
public DocumentAdminAppService(IProjectRepository projectRepository,
IDocumentRepository documentRepository,
IDocumentSourceFactory documentStoreFactory,
IDistributedCache<DocumentUpdateInfo> documentUpdateCache,
IDistributedCache<List<VersionInfo>> versionCache,
IDistributedCache<LanguageConfig> languageCache,
IDistributedCache<DocumentResource> resourceCache,
IDocumentFullSearch documentFullSearch)
IDistributedCache<LanguageConfig> languageCache)
{
_projectRepository = projectRepository;
_documentRepository = documentRepository;
@ -43,8 +39,6 @@ namespace Volo.Docs.Admin.Documents
_documentUpdateCache = documentUpdateCache;
_versionCache = versionCache;
_languageCache = languageCache;
_resourceCache = resourceCache;
_documentFullSearch = documentFullSearch;
LocalizationResource = typeof(DocsResource);
}
@ -125,32 +119,6 @@ namespace Volo.Docs.Admin.Documents
await UpdateDocumentUpdateInfoCache(sourceDocument);
}
public async Task ReindexAsync()
{
var docs = await _documentRepository.GetListAsync();
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)
{
continue;
}
if (doc.FileName == project.ParametersDocumentName)
{
continue;
}
await _documentFullSearch.AddOrUpdateAsync(doc);
}
}
private async Task UpdateDocumentUpdateInfoCache(Document document)
{
var cacheKey = $"DocumentUpdateInfo{document.ProjectId}#{document.Name}#{document.LanguageCode}#{document.Version}";

@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Guids;
using Volo.Docs.Documents;
using Volo.Docs.Documents.FullSearch.Elastic;
using Volo.Docs.Localization;
using Volo.Docs.Projects;
@ -14,15 +17,22 @@ namespace Volo.Docs.Admin.Projects
public class ProjectAdminAppService : ApplicationService, IProjectAdminAppService
{
private readonly IProjectRepository _projectRepository;
private readonly IDocumentRepository _documentRepository;
private readonly IDocumentFullSearch _documentFullSearch;
private readonly IGuidGenerator _guidGenerator;
public ProjectAdminAppService(
IProjectRepository projectRepository, IGuidGenerator guidGenerator)
IProjectRepository projectRepository,
IDocumentRepository documentRepository,
IDocumentFullSearch documentFullSearch,
IGuidGenerator guidGenerator)
{
ObjectMapperContext = typeof(DocsAdminApplicationModule);
LocalizationResource = typeof(DocsResource);
_projectRepository = projectRepository;
_documentRepository = documentRepository;
_documentFullSearch = documentFullSearch;
_guidGenerator = guidGenerator;
}
@ -107,5 +117,56 @@ namespace Volo.Docs.Admin.Projects
await _projectRepository.DeleteAsync(id);
}
public async Task ReindexAsync(ReindexInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
await _documentFullSearch.DeleteAllByProjectIdAsync(project.Id);
var docs = await _documentRepository.GetListByProjectId(project.Id);
foreach (var doc in docs)
{
if (doc.FileName == project.NavigationDocumentName)
{
continue;
}
if (doc.FileName == project.ParametersDocumentName)
{
continue;
}
await _documentFullSearch.AddOrUpdateAsync(doc);
}
}
public async Task ReindexAllAsync()
{
await _documentFullSearch.DeleteAllAsync();
var docs = await _documentRepository.GetListAsync();
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)
{
continue;
}
if (doc.FileName == project.ParametersDocumentName)
{
continue;
}
await _documentFullSearch.AddOrUpdateAsync(doc);
}
}
}
}

@ -39,12 +39,5 @@ namespace Volo.Docs.Admin
{
return _documentAdminAppService.PullAsync(input);
}
[HttpPost]
[Route("Reindex")]
public Task ReindexAsync()
{
return _documentAdminAppService.ReindexAsync();
}
}
}

@ -53,5 +53,19 @@ namespace Volo.Docs.Admin
{
return _projectAppService.DeleteAsync(id);
}
[HttpPost]
[Route("ReindexAll")]
public Task ReindexAllAsync()
{
return _projectAppService.ReindexAllAsync();
}
[HttpPost]
[Route("Reindex")]
public Task ReindexAsync(ReindexInput input)
{
return _projectAppService.ReindexAsync(input);
}
}
}

@ -39,6 +39,10 @@
</abp-dropdown-menu>
</abp-dropdown>
}
@if (await Authorization.IsGrantedAsync(DocsAdminPermissions.Projects.Default))
{
<abp-button button-type="Primary" icon="plus" text="@L["ReIndexAllProjects"].Value" id="ReIndexAllProjects" />
}
</abp-column>
</abp-row>
</abp-card-header>

@ -74,6 +74,18 @@
_dataTable.ajax.reload();
});
}
},
{
text: l('ReIndexProject'),
visible: abp.auth.isGranted('Docs.Admin.Documents'),
confirmMessage: function (data) { return l('ReIndexProjectConfirmationMessage', data.record.name); },
action: function (data) {
volo.docs.admin.projectsAdmin
.reindex({ projectId: data.record.id})
.then(function () {
abp.message.success(l('SuccessfullyReIndexProject', data.record.name));
});
}
}
]
}
@ -110,6 +122,19 @@
_createModal.open({source:"GitHub"});
});
$("#ReIndexAllProjects").click(function (event) {
abp.message.confirm(l('ReIndexAllProjectConfirmationMessage'))
.done(function (accepted) {
if (accepted) {
volo.docs.admin.projectsAdmin
.reindexAll()
.then(function () {
abp.message.success(l('SuccessfullyReIndexAllProject'));
});
}
});
});
_createModal.onClose(function () {
_dataTable.ajax.reload();
});

@ -92,6 +92,48 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
.DeleteAsync(DocumentPath<Document>.Id(id), x => x.Index(_options.IndexName), cancellationToken));
}
public async Task DeleteAllAsync(CancellationToken cancellationToken = default)
{
ValidateElasticSearchEnabled();
var request = new DeleteByQueryRequest(_options.IndexName)
{
Query = new MatchAllQuery()
};
HandleError(await _clientProvider.GetClient()
.DeleteByQueryAsync(request, cancellationToken));
}
public async Task DeleteAllByProjectIdAsync(Guid projectId, CancellationToken cancellationToken = default)
{
ValidateElasticSearchEnabled();
var request = new DeleteByQueryRequest(_options.IndexName)
{
Query = new BoolQuery
{
Filter = new QueryContainer[]
{
new BoolQuery
{
Must = new QueryContainer[]
{
new TermQuery
{
Field = "projectId",
Value = projectId
}
}
}
}
},
};
HandleError(await _clientProvider.GetClient()
.DeleteByQueryAsync(request, cancellationToken));
}
public async Task<List<EsDocument>> SearchAsync(string context, Guid projectId, string languageCode,
string version, int? skipCount = null, int? maxResultCount = null,
CancellationToken cancellationToken = default)

@ -13,6 +13,10 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
Task DeleteAsync(Guid id, CancellationToken cancellationToken = default);
Task DeleteAllAsync(CancellationToken cancellationToken = default);
Task DeleteAllByProjectIdAsync(Guid projectId, CancellationToken cancellationToken = default);
Task<List<EsDocument>> SearchAsync(string context, Guid projectId, string languageCode,
string version, int? skipCount = null, int? maxResultCount = null,
CancellationToken cancellationToken = default);

@ -11,6 +11,12 @@
"Delete": "Delete",
"ClearCache": "Clear cache",
"ClearCacheConfirmationMessage": "Are you sure to clear all caches for project \"{0}\"",
"ReIndexAllProjects": "ReIndex all projects",
"ReIndexProject": "ReIndex project",
"ReIndexProjectConfirmationMessage":"Are you sure to reindex for project \"{0}\"",
"SuccessfullyReIndexProject": "Successfully reindex for project \"{0}\"",
"ReIndexAllProjectConfirmationMessage": "Are you sure to reindex all project?",
"SuccessfullyReIndexAllProject": "Successfully reindex for all projects",
"InThisDocument": "In this document",
"GoToTop": "Go to top",
"Projects": "Project(s)",

Loading…
Cancel
Save