2021-09-01 23:09:22 +02:00
|
|
|
using Raven.Client.Documents;
|
2021-09-02 22:44:20 +02:00
|
|
|
using Raven.Client.Documents.Queries;
|
2021-09-01 23:09:22 +02:00
|
|
|
using Raven.Client.Documents.Session;
|
|
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
namespace zero.Api.Endpoints.Search;
|
2021-11-22 14:29:22 +01:00
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
public class SearchService : ISearchService
|
2021-09-01 23:09:22 +02:00
|
|
|
{
|
2021-11-22 14:29:22 +01:00
|
|
|
protected IZeroStore Store { get; private set; }
|
2021-09-01 23:09:22 +02:00
|
|
|
|
2021-11-22 14:29:22 +01:00
|
|
|
protected IZeroOptions Options { get; private set; }
|
2021-09-03 00:23:39 +02:00
|
|
|
|
|
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
public SearchService(IZeroStore store, IZeroOptions options)
|
2021-11-22 14:29:22 +01:00
|
|
|
{
|
|
|
|
|
Store = store;
|
|
|
|
|
Options = options;
|
2021-09-01 23:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-22 14:29:22 +01:00
|
|
|
|
2021-11-23 15:43:21 +01:00
|
|
|
public async Task<Paged<SearchResult>> Query(string searchTerm)
|
2021-09-01 23:09:22 +02:00
|
|
|
{
|
2021-11-22 14:29:22 +01:00
|
|
|
string[] searchParts = searchTerm.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x =>
|
|
|
|
|
{
|
|
|
|
|
return "*" + x + "*";
|
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
|
|
List<ZeroEntity> results = await Store.Session().Query<SearchIndexResult, zero_Backoffice_Search>()
|
|
|
|
|
.Statistics(out QueryStatistics stats)
|
|
|
|
|
.Search(x => x.Name, searchParts, 2, @operator: SearchOperator.And)
|
|
|
|
|
.Search(x => x.Fields, searchParts, 1, Raven.Client.Documents.SearchOptions.Or, @operator: SearchOperator.And)
|
|
|
|
|
.Paging(1, 20)
|
|
|
|
|
.As<ZeroEntity>()
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
List<SearchResult> items = new();
|
|
|
|
|
|
2021-11-27 16:33:05 +01:00
|
|
|
IEnumerable<SearchIndexMap> maps = Options.For<SearchOptions>();
|
2021-11-22 14:29:22 +01:00
|
|
|
|
|
|
|
|
foreach (ZeroEntity result in results)
|
|
|
|
|
{
|
|
|
|
|
Type type = result.GetType();
|
|
|
|
|
SearchIndexMap map = maps.FirstOrDefault(x => x.CanModify(type));
|
|
|
|
|
|
|
|
|
|
SearchResult searchResult = new()
|
|
|
|
|
{
|
|
|
|
|
Id = result.Id,
|
|
|
|
|
Icon = map._Icon.Or("fth-folder"),
|
|
|
|
|
Name = result.Name,
|
|
|
|
|
IsActive = result.IsActive,
|
|
|
|
|
Group = GetGroupName(map.Type),
|
|
|
|
|
Url = "/"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await map.Modify(result, searchResult, Options);
|
|
|
|
|
|
|
|
|
|
items.Add(searchResult);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 15:43:21 +01:00
|
|
|
return new Paged<SearchResult>(items, stats.TotalResults, 1, 20);
|
2021-11-22 14:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected string GetGroupName(Type type)
|
|
|
|
|
{
|
|
|
|
|
return "@search.collection." + type.Name.ToCamelCase();
|
2021-09-01 23:09:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-22 14:29:22 +01:00
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
public interface ISearchService
|
2021-11-22 14:29:22 +01:00
|
|
|
{
|
2021-11-23 15:43:21 +01:00
|
|
|
Task<Paged<SearchResult>> Query(string searchTerm);
|
2021-11-22 14:29:22 +01:00
|
|
|
}
|