Files
mixtape/zero.Core/Search/Models/SearchIndexMap.cs
T

121 lines
2.9 KiB
C#
Raw Normal View History

2021-11-19 14:59:24 +01:00
using Raven.Client.Documents;
2022-02-04 15:59:35 +01:00
namespace zero.Search;
2021-11-19 14:59:24 +01:00
public class SearchIndexMap
{
internal Type Type;
internal string _Icon;
protected string _group;
protected string[] _fields;
2022-02-05 01:52:48 +01:00
protected float _boost = 0;
2022-08-29 14:01:59 +02:00
protected Func<ZeroEntity, SearchResult, IZeroContext, Task> _modify;
2021-11-19 14:59:24 +01:00
const string mapTemplate = @"map('{collection}', function (x) {
return {
Id: x.Id,
Group: '{group}',
2022-02-05 01:52:48 +01:00
Name: boost(x.Name, {boost}),
2021-11-19 14:59:24 +01:00
IsActive: x.IsActive,
Fields: [{fields}]
};
});";
internal SearchIndexMap(Type type, string icon = null)
{
Type = type;
_group = "__TODO";
_Icon = icon;
}
internal string BuildInstruction(IDocumentStore store)
{
return TokenReplacement.Apply(mapTemplate, new()
{
{ "collection", store.Conventions.GetCollectionName(Type) },
{ "group", _group },
2022-02-05 01:52:48 +01:00
{ "fields", BuildFieldArray(_fields) },
{ "boost", _boost.ToString() }
2021-11-19 14:59:24 +01:00
});
}
internal string BuildFieldArray(string[] fields)
{
if (fields == null || !fields.Any())
{
return String.Empty;
}
2022-02-05 01:52:48 +01:00
return String.Join(", ", fields.Select(x => $"x.{x}")); //$"boost(x.{x}, {_boost})"));
2021-11-19 14:59:24 +01:00
}
internal bool CanModify(Type type)
{
return Type.IsAssignableFrom(type);
}
2022-08-29 14:01:59 +02:00
internal async Task Modify(ZeroEntity entity, SearchResult result, IZeroContext context)
2021-11-19 14:59:24 +01:00
{
if (_modify != null)
{
2022-08-29 14:01:59 +02:00
await _modify(entity, result, context);
2021-11-19 14:59:24 +01:00
}
}
}
public class SearchIndexMap<T> : SearchIndexMap where T : ZeroEntity
{
2021-11-22 14:29:22 +01:00
internal SearchIndexMap(string icon = null) : base(typeof(T), icon) { }
2021-11-19 14:59:24 +01:00
2022-02-04 15:59:35 +01:00
public virtual SearchIndexMap<T> Icon(string icon)
2021-11-19 14:59:24 +01:00
{
_Icon = icon;
return this;
}
public SearchIndexMap<T> Display(Action<T, SearchResult> modify = null)
{
_modify = (x, res, opts) =>
{
modify?.Invoke(x as T, res);
return Task.CompletedTask;
};
return this;
}
public SearchIndexMap<T> Display(Func<T, SearchResult, Task> modify = null)
{
2022-08-29 14:01:59 +02:00
_modify = (x, res, ctx) => modify?.Invoke(x as T, res);
2021-11-19 14:59:24 +01:00
return this;
}
2022-08-29 14:01:59 +02:00
public SearchIndexMap<T> Display(Func<T, SearchResult, IZeroContext, Task> modify = null)
2021-11-19 14:59:24 +01:00
{
2022-08-29 14:01:59 +02:00
_modify = (x, res, ctx) => modify?.Invoke(x as T, res, ctx);
return this;
}
public SearchIndexMap<T> Display(Action<T, SearchResult, IZeroContext> modify = null)
{
_modify = (x, res, ctx) =>
2021-11-19 14:59:24 +01:00
{
2022-08-29 14:01:59 +02:00
modify?.Invoke(x as T, res, ctx);
2021-11-19 14:59:24 +01:00
return Task.CompletedTask;
};
return this;
}
public SearchIndexMap<T> Fields(params string[] fieldNames)
{
_fields = fieldNames;
return this;
}
2022-02-05 01:52:48 +01:00
public SearchIndexMap<T> Boost(float boostValue)
{
_boost = boostValue;
return this;
}
2021-11-19 14:59:24 +01:00
}