Skip to content

Commit

Permalink
Enhanced reindex function.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Apr 23, 2020
1 parent fa310ad commit ea9d13c
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Volo.Docs.Admin.Documents
Expand All @@ -10,7 +11,5 @@ public interface IDocumentAdminAppService : IApplicationService
Task PullAllAsync(PullAllDocumentInput input);

Task PullAsync(PullDocumentInput input);

Task ReindexAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public interface IProjectAdminAppService : IApplicationService
Task<ProjectDto> GetAsync(Guid id);

Task<ProjectDto> CreateAsync(CreateProjectDto input);

Task<ProjectDto> UpdateAsync(Guid id, UpdateProjectDto input);

Task DeleteAsync(Guid id);

Task ReindexAsync(ReindexInput input);

Task ReindexAllAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Volo.Docs.Admin.Projects
{
public class ReindexInput
{
public Guid ProjectId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,20 @@ public class DocumentAdminAppService : ApplicationService, IDocumentAdminAppServ
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;
_documentStoreFactory = documentStoreFactory;
_documentUpdateCache = documentUpdateCache;
_versionCache = versionCache;
_languageCache = languageCache;
_resourceCache = resourceCache;
_documentFullSearch = documentFullSearch;

LocalizationResource = typeof(DocsResource);
}
Expand Down Expand Up @@ -125,32 +119,6 @@ await _documentRepository.DeleteAsync(sourceDocument.ProjectId, sourceDocument.N
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}";
Expand All @@ -174,4 +142,4 @@ private async Task<Document> GetDocumentAsync(
return document;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -51,7 +61,7 @@ public async Task<ProjectDto> CreateAsync(CreateProjectDto input)
{
throw new ProjectShortNameAlreadyExistsException(input.ShortName);
}

var project = new Project(_guidGenerator.Create(),
input.Name,
input.ShortName,
Expand All @@ -71,7 +81,7 @@ public async Task<ProjectDto> CreateAsync(CreateProjectDto input)
{
project.ExtraProperties.Add(extraProperty.Key, extraProperty.Value);
}

project = await _projectRepository.InsertAsync(project);

return ObjectMapper.Map<Project, ProjectDto>(project);
Expand Down Expand Up @@ -106,6 +116,57 @@ public async Task DeleteAsync(Guid id)
{
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,5 @@ public Task PullAsync(PullDocumentInput input)
{
return _documentAdminAppService.PullAsync(input);
}

[HttpPost]
[Route("Reindex")]
public Task ReindexAsync()
{
return _documentAdminAppService.ReindexAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,19 @@ public Task DeleteAsync(Guid id)
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}
}
]
}
Expand Down Expand Up @@ -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();
});
Expand All @@ -118,4 +143,4 @@
_dataTable.ajax.reload();
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,48 @@ public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = def
.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)
Expand Down Expand Up @@ -185,4 +227,4 @@ protected void ValidateElasticSearchEnabled()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ public interface IDocumentFullSearch

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);
}
}
}
Loading

0 comments on commit ea9d13c

Please sign in to comment.