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
@@ -0,0 +1,32 @@
using Algolia.Search.Clients;
using SImpl.CQRS.Commands;
using SImpl.SearchModule.Abstraction.Commands;
using SImpl.SearchModule.Abstraction.Models;
using SImpl.SearchModule.Algolia.Configuration;

namespace SImpl.SearchModule.Algolia.Application.CommandHandlers
{
public class IndexCommandHandler : ICommandHandler<IndexCommand>
{
private readonly ISearchClient _algoliaSearchClient;
private readonly AlgoliaSearchConfiguration _configuration;
private readonly ILogger<IndexCommandHandler> _logger;


public IndexCommandHandler(ISearchClient algoliaSearchClient, AlgoliaSearchConfiguration configuration,
ILogger<IndexCommandHandler> logger)
{
_algoliaSearchClient = algoliaSearchClient;
_configuration = configuration;
_logger = logger;
}

public async Task HandleAsync(IndexCommand command)
{
var indexName = _configuration.IndexPrefixName + command.Index.ToLowerInvariant();
var index= _algoliaSearchClient.InitIndex(indexName);
var result = await index.SaveObjectsAsync(command.Models);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using SImpl.CQRS.Commands;
using SImpl.SearchModule.Abstraction.Commands;
using SImpl.SearchModule.Abstraction.Models;
using SImpl.SearchModule.Algolia.Configuration;

namespace SImpl.SearchModule.Algolia.Application.CommandHandlers
{
public class ReIndexCommandHandler : ICommandHandler<ReIndexCommand>
{
private readonly IExamineManager _examineManager;
private readonly ExamineSearchConfiguration _configuration;
private readonly ILogger<IndexCommandHandler> _logger;


public ReIndexCommandHandler(IExamineManager examineManager, ExamineSearchConfiguration configuration,
ILogger<IndexCommandHandler> logger)
{
_examineManager = examineManager;
_configuration = configuration;
_logger = logger;
}

public async Task HandleAsync(ReIndexCommand command)
{
var indexName = _configuration.IndexPrefixName + command.Index.ToLowerInvariant();
_examineManager.TryGetIndex(indexName,
out IIndex examineIndex);
if (examineIndex == null)
{
_logger.LogError($"Examine index not found {indexName}");
return;
}

try
{

examineIndex.CreateIndex();


examineIndex.IndexItems(TranslateModel(command.Models));
}
catch (Exception e)
{
_logger.LogError($"Indexing for {indexName} failed", e);
}
}

private IEnumerable<ValueSet> TranslateModel(List<ISearchModel> commandModels)
{
var modelList = new List<ValueSet>();
foreach (var model in commandModels)
{
var translatedModel = ValueSet.FromObject(model.Id, "search", model.ContentType, model);

modelList.Add(translatedModel);
}

return modelList;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using SImpl.CQRS.Commands;
using SImpl.SearchModule.Abstraction.Commands;
using SImpl.SearchModule.Algolia.Configuration;

namespace SImpl.SearchModule.Algolia.Application.CommandHandlers
{
public class RemoveCommandCommandHandler : ICommandHandler<RemoveCommand>
{
private readonly IExamineManager _examineManager;
private readonly ExamineSearchConfiguration _configuration;
private readonly ILogger<IndexCommandHandler> _logger;

public RemoveCommandCommandHandler(IExamineManager examineManager, ExamineSearchConfiguration configuration,
ILogger<IndexCommandHandler> logger)
{
_examineManager = examineManager;
_configuration = configuration;
_logger = logger;
}

public async Task HandleAsync(RemoveCommand command)
{
var indexName = _configuration.IndexPrefixName + command.Index.ToLowerInvariant();
_examineManager.TryGetIndex(indexName,
out IIndex examineIndex);
if (examineIndex == null)
{
_logger.LogError($"Examine index not found {indexName}");
return;
}

if (command.Models.Any())
{
try
{
_logger.LogInformation($"Deleted items {string.Join(", ",command.Models.Select(x=>x.Id.ToString()).ToList())}");
examineIndex.DeleteFromIndex(command.Models.Select(x=>x.Id));

}
catch (Exception e)
{
_logger.LogError($"remove from index {indexName} failed");
}
}
else if (command.ModelsIds.Any())
{
try
{ _logger.LogInformation($"Deleted items {string.Join(", ",command.ModelsIds)}");

examineIndex.DeleteFromIndex(command.ModelsIds.Select(x=>x.ToString()));

}
catch (Exception e)
{
_logger.LogError($"remove from index {indexName} failed");
}

}else if (command.ModelsKeys.Any())
{
try
{ _logger.LogInformation($"Deleted items {string.Join(", ",command.ModelsKeys)}");

examineIndex.DeleteFromIndex(command.ModelsKeys);

}
catch (Exception e)
{
_logger.LogError($"remove from index {indexName} failed");
}

}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Globalization;
using SImpl.SearchModule.Abstraction.Models;
using SImpl.SearchModule.Algolia.Models;

namespace SImpl.SearchModule.Algolia.Application
{
public class ElasticSearchModelMapper
{
public static string MapToSimpleTypeName(Type type)
{
return $"{type.FullName}, {type.Assembly.GetName().Name}";
}

public static ElasticSearchModel Map(ISearchModel model)
{
return new ElasticSearchModel
{
Id = model.ContentKey,
AdditionalKeys = model.AdditionalKeys,
Culture = model.Culture.IetfLanguageTag.ToLower(),
Content = model.Content,
Facet = model.Facet,
ContentType = model.ContentType,
Tags = model.Tags,
IndexedAt = model.IndexedAt,
ViewModelType = MapToSimpleTypeName(model.ViewModelType),
CustomProperties = model.CustomProperties,
};
}

public static ISearchModel Map(ElasticSearchModel model)
{
return new BaseSearchModel
{
ContentKey = model.Id,
Content = model.Content,
AdditionalKeys = model.AdditionalKeys,
ContentType = model.ContentType,
Facet = model.Facet,
Culture = new CultureInfo(model.Culture),
IndexedAt = model.IndexedAt,
Tags = model.Tags?.ToList(),
ViewModelType = Type.GetType(model.ViewModelType),
CustomProperties = model.CustomProperties,
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Extensions.DependencyInjection;
using SImpl.SearchModule.Algolia.Configuration;

namespace SImpl.SearchModule.Algolia.Application.Factories
{
public class ConfigurationEnabledDirectoryFactory : DirectoryFactoryBase
{
private readonly IServiceProvider _services;
private readonly ExamineSearchConfiguration _examineSearchConfiguration;
private readonly IApplicationRoot _applicationRoot;
private IDirectoryFactory _directoryFactory;

public ConfigurationEnabledDirectoryFactory(
IServiceProvider services,
ExamineSearchConfiguration examineSearchConfiguration,
IApplicationRoot applicationRoot)
{
_services = services;
_examineSearchConfiguration = examineSearchConfiguration;
_applicationRoot = applicationRoot;
}

protected override Lucene.Net.Store.Directory CreateDirectory(
LuceneIndex luceneIndex,
bool forceUnlock)
{
_directoryFactory = CreateFactory();
return _directoryFactory.CreateDirectory(luceneIndex, forceUnlock);
}

/// <summary>
/// Creates a directory factory based on the configured value and ensures that
/// </summary>
private IDirectoryFactory CreateFactory()
{
DirectoryInfo applicationRoot = _applicationRoot.ApplicationRoot;
if (!applicationRoot.Exists)
System.IO.Directory.CreateDirectory(applicationRoot.FullName);

return (IDirectoryFactory) this._services.GetRequiredService(_examineSearchConfiguration
.LuceneDirectoryFactory);
}
}
}
Loading