Browse Source

Normalize projectId field.

Related #5132
pull/5136/head
maliming 6 years ago
parent
commit
d8a43f3fc6
  1. 40
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs
  2. 6
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocument.cs

40
modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs

@ -26,7 +26,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
_options = options.Value; _options = options.Value;
} }
public async Task CreateIndexIfNeededAsync(CancellationToken cancellationToken = default) public virtual async Task CreateIndexIfNeededAsync(CancellationToken cancellationToken = default)
{ {
ValidateElasticSearchEnabled(); ValidateElasticSearchEnabled();
@ -54,7 +54,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
} }
} }
public async Task AddOrUpdateAsync(Document document, CancellationToken cancellationToken = default) public virtual async Task AddOrUpdateAsync(Document document, CancellationToken cancellationToken = default)
{ {
ValidateElasticSearchEnabled(); ValidateElasticSearchEnabled();
@ -67,13 +67,13 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
var esDocument = new EsDocument var esDocument = new EsDocument
{ {
Id = document.Id, Id = NormalizeField(document.Id),
ProjectId = document.ProjectId, ProjectId = NormalizeField(document.ProjectId),
Name = document.Name, Name = document.Name,
FileName = document.FileName, FileName = document.FileName,
Content = document.Content, Content = document.Content,
LanguageCode = ConvertLanguageCode(document.LanguageCode), LanguageCode = NormalizeField(document.LanguageCode),
Version = ConvertVersion(document.Version) Version = NormalizeField(document.Version)
}; };
if (!existsResponse.Exists) if (!existsResponse.Exists)
@ -89,7 +89,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
} }
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default) public virtual async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
{ {
ValidateElasticSearchEnabled(); ValidateElasticSearchEnabled();
@ -97,7 +97,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
.DeleteAsync(DocumentPath<Document>.Id(id), x => x.Index(_options.IndexName), cancellationToken)); .DeleteAsync(DocumentPath<Document>.Id(id), x => x.Index(_options.IndexName), cancellationToken));
} }
public async Task DeleteAllAsync(CancellationToken cancellationToken = default) public virtual async Task DeleteAllAsync(CancellationToken cancellationToken = default)
{ {
ValidateElasticSearchEnabled(); ValidateElasticSearchEnabled();
@ -110,7 +110,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
.DeleteByQueryAsync(request, cancellationToken)); .DeleteByQueryAsync(request, cancellationToken));
} }
public async Task DeleteAllByProjectIdAsync(Guid projectId, CancellationToken cancellationToken = default) public virtual async Task DeleteAllByProjectIdAsync(Guid projectId, CancellationToken cancellationToken = default)
{ {
ValidateElasticSearchEnabled(); ValidateElasticSearchEnabled();
@ -127,7 +127,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
new TermQuery new TermQuery
{ {
Field = "projectId", Field = "projectId",
Value = projectId Value = NormalizeField(projectId)
} }
} }
} }
@ -139,7 +139,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
.DeleteByQueryAsync(request, cancellationToken)); .DeleteByQueryAsync(request, cancellationToken));
} }
public async Task<List<EsDocument>> SearchAsync(string context, Guid projectId, string languageCode, public virtual async Task<List<EsDocument>> SearchAsync(string context, Guid projectId, string languageCode,
string version, int? skipCount = null, int? maxResultCount = null, string version, int? skipCount = null, int? maxResultCount = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
@ -168,17 +168,17 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
new TermQuery new TermQuery
{ {
Field = "projectId", Field = "projectId",
Value = projectId Value = NormalizeField(projectId)
}, },
new TermQuery new TermQuery
{ {
Field = "version", Field = "version",
Value = ConvertVersion(version) Value = NormalizeField(version)
}, },
new TermQuery new TermQuery
{ {
Field = "languageCode", Field = "languageCode",
Value = ConvertLanguageCode(languageCode) Value = NormalizeField(languageCode)
} }
} }
} }
@ -216,7 +216,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
return docs; return docs;
} }
protected void HandleError(IElasticsearchResponse response) protected virtual void HandleError(IElasticsearchResponse response)
{ {
if (!response.ApiCall.Success) if (!response.ApiCall.Success)
{ {
@ -225,7 +225,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
} }
} }
protected void ValidateElasticSearchEnabled() protected virtual void ValidateElasticSearchEnabled()
{ {
if (!_options.Enable) if (!_options.Enable)
{ {
@ -233,14 +233,14 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
} }
} }
protected string ConvertLanguageCode(string languageCode) protected virtual string NormalizeField(Guid field)
{ {
return languageCode.Replace("-", "").ToLower(); return NormalizeField(field.ToString("N"));
} }
protected string ConvertVersion(string version) protected virtual string NormalizeField(string field)
{ {
return version.Replace("-", "").ToLower(); return field.Replace("-", "").ToLower();
} }
} }
} }

6
modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocument.cs

@ -10,9 +10,9 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
Highlight = new List<string>(); Highlight = new List<string>();
} }
public Guid Id { get; set; } public string Id { get; set; }
public Guid ProjectId { get; set; } public string ProjectId { get; set; }
public string Name { get; set; } public string Name { get; set; }
@ -26,4 +26,4 @@ namespace Volo.Docs.Documents.FullSearch.Elastic
public List<string> Highlight { get; set; } public List<string> Highlight { get; set; }
} }
} }

Loading…
Cancel
Save