Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public partial class ElasticSearch8Provider : ISearchProvider, ISupportIndexSwap
protected Uri ServerUrl { get; }

[GeneratedRegex("[/+_=]", RegexOptions.Compiled)]
private static partial Regex SpecialSymbols();
protected static partial Regex SpecialSymbols();

// prefixes for index aliases
public const string ActiveIndexAlias = "active";
public const string BackupIndexAlias = "backup";
public virtual string ActiveIndexAlias => "active";
public virtual string BackupIndexAlias => "backup";

public ElasticSearch8Provider(
IOptions<SearchOptions> searchOptions,
Expand Down Expand Up @@ -100,7 +100,7 @@ ILogger<ElasticSearch8Provider> logger
}
}

public async Task<SearchResponse> SearchAsync(string documentType, SearchModule.Core.Model.SearchRequest request)
public virtual async Task<SearchResponse> SearchAsync(string documentType, SearchModule.Core.Model.SearchRequest request)
{
CheckClientCreated();

Expand All @@ -124,17 +124,18 @@ public async Task<SearchResponse> SearchAsync(string documentType, SearchModule.
}

var result = _searchResponseBuilder.ToSearchResponse(providerResponse, request);

return result;
}

public Task<IndexingResult> IndexAsync(string documentType, IList<IndexDocument> documents)
public virtual Task<IndexingResult> IndexAsync(string documentType, IList<IndexDocument> documents)
{
CheckClientCreated();

return InternalIndexAsync(documentType, documents, new IndexingParameters());
}

public async Task DeleteIndexAsync(string documentType)
public virtual async Task DeleteIndexAsync(string documentType)
{
CheckClientCreated();

Expand All @@ -161,7 +162,7 @@ public async Task DeleteIndexAsync(string documentType)
}
}

public async Task<IndexingResult> RemoveAsync(string documentType, IList<IndexDocument> documents)
public virtual async Task<IndexingResult> RemoveAsync(string documentType, IList<IndexDocument> documents)
{
CheckClientCreated();

Expand All @@ -186,17 +187,19 @@ public async Task<IndexingResult> RemoveAsync(string documentType, IList<IndexDo
return result;
}

public Task<IndexingResult> IndexWithBackupAsync(string documentType, IList<IndexDocument> documents)
public virtual Task<IndexingResult> IndexWithBackupAsync(string documentType, IList<IndexDocument> documents)
{
CheckClientCreated();

return InternalIndexAsync(documentType, documents, new IndexingParameters() { Reindex = true });
}

public async Task SwapIndexAsync(string documentType)
public virtual async Task SwapIndexAsync(string documentType)
{
ArgumentNullException.ThrowIfNull(documentType);

CheckClientCreated();

// get active index and alias
var activeIndexAlias = GetIndexAlias(ActiveIndexAlias, documentType);

Expand Down Expand Up @@ -258,8 +261,10 @@ public async Task SwapIndexAsync(string documentType)
/// <summary>
/// Puts an active alias on a default index (if exists)
/// </summary>
public async Task AddActiveAlias(IEnumerable<string> documentTypes)
public virtual async Task AddActiveAlias(IEnumerable<string> documentTypes)
{
CheckClientCreated();

try
{
foreach (var documentType in documentTypes)
Expand Down Expand Up @@ -289,9 +294,11 @@ public async Task AddActiveAlias(IEnumerable<string> documentTypes)
}
}

public Task CreateIndexAsync(string documentType, IndexDocument schema)
public virtual Task CreateIndexAsync(string documentType, IndexDocument schema)
{
return InternalCreateIndexAsync(documentType, new[] { schema }, new IndexingParameters { Reindex = true });
CheckClientCreated();

return InternalCreateIndexAsync(documentType, [schema], new IndexingParameters { Reindex = true });
}

protected virtual async Task<IndexingResult> InternalIndexAsync(string documentType, IList<IndexDocument> documents, IndexingParameters parameters)
Expand Down Expand Up @@ -345,7 +352,7 @@ protected virtual async Task<IndexingResult> InternalIndexAsync(string documentT
return result;
}

private async Task CheckMLPipeline(string pipelineName)
protected async Task CheckMLPipeline(string pipelineName)
{
var getPipelineRequest = new GetPipelineRequest(pipelineName)
{
Expand All @@ -360,7 +367,7 @@ private async Task CheckMLPipeline(string pipelineName)
}
}

private async Task CreateMLField(string indexName)
protected async Task CreateMLField(string indexName)
{
var indexMappings = await GetMappingAsync(indexName);

Expand Down Expand Up @@ -406,7 +413,7 @@ private async Task CreateMLField(string indexName)
}
}

private static void CreateBulkIndexRequest(string indexName, IList<SearchDocument> documents, BulkRequestDescriptor descriptor, List<string> pipelines)
protected static void CreateBulkIndexRequest(string indexName, IList<SearchDocument> documents, BulkRequestDescriptor descriptor, List<string> pipelines)
{
descriptor
.Index((IndexName)indexName)
Expand Down Expand Up @@ -538,7 +545,7 @@ protected virtual SearchDocument ConvertToProviderDocument(IndexDocument documen
return result;
}

private static object GetFieldValue(IProperty property, IndexDocumentField field)
protected static object GetFieldValue(IProperty property, IndexDocumentField field)
{
var isCollection = field.IsCollection || field.Values.Count > 1;
object result;
Expand Down Expand Up @@ -611,12 +618,12 @@ protected virtual void ConfigureNormalizers(NormalizersDescriptor descriptor)
descriptor.Custom("lowercase", ConfigureLowerCaseNormalizer);
}

private void ConfigureNGramFilter(NGramTokenFilterDescriptor descriptor)
protected void ConfigureNGramFilter(NGramTokenFilterDescriptor descriptor)
{
descriptor.MinGram(_settingsManager.GetMinGram()).MaxGram(_settingsManager.GetMaxGram());
}

private void ConfigureEdgeNGramFilter(EdgeNGramTokenFilterDescriptor descriptor)
protected void ConfigureEdgeNGramFilter(EdgeNGramTokenFilterDescriptor descriptor)
{
descriptor.MinGram(_settingsManager.GetMinGram()).MaxGram(_settingsManager.GetMaxGram());
}
Expand Down Expand Up @@ -690,14 +697,14 @@ protected virtual void ThrowException(string message, Exception innerException)
throw new SearchException($"{message}. URL:{ServerUrl}, Scope: {_searchOptions.Scope}", innerException);
}

private static void CreateBulkDeleteRequest(string indexName, IList<SearchDocument> documents, BulkRequestDescriptor descriptor)
protected static void CreateBulkDeleteRequest(string indexName, IList<SearchDocument> documents, BulkRequestDescriptor descriptor)
{
var ids = documents.Select(x => new Id(x.Id));

descriptor.DeleteMany(indexName, ids);
}

private async Task<IndexName> GetIndexNameAsync(string indexAlias)
protected async Task<IndexName> GetIndexNameAsync(string indexAlias)
{
var activeIndexResponse = await Client.Indices.GetAsync(new GetIndexRequest(indexAlias));
if (!activeIndexResponse.IsValidResponse && activeIndexResponse.ApiCallDetails.HttpStatusCode != (int)HttpStatusCode.NotFound)
Expand Down Expand Up @@ -751,7 +758,7 @@ protected virtual void RemoveMappingFromCache(string indexName)
_mappings.TryRemove(indexName, out _);
}

private void CheckClientCreated()
protected void CheckClientCreated()
{
if (Client == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Elastic.Clients.Elasticsearch.Core.Search;
using VirtoCommerce.ElasticSearch8.Core.Services;
using VirtoCommerce.ElasticSearch8.Data.Extensions;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.SearchModule.Core.Extensions;
using VirtoCommerce.SearchModule.Core.Model;
using VirtoCommerceSearchRequest = VirtoCommerce.SearchModule.Core.Model.SearchRequest;
Expand All @@ -17,7 +18,7 @@ public class ElasticSearchResponseBuilder : IElasticSearchResponseBuilder
{
public virtual SearchResponse ToSearchResponse(SearchResponse<SearchDocument> response, VirtoCommerceSearchRequest request)
{
var result = new SearchResponse();
var result = AbstractTypeFactory<SearchResponse>.TryCreateInstance();

if (response.Total > 0)
{
Expand Down
Loading