A search index plugin for Workflow Core backed by Elasticsearch, enabling you to index your workflows and search against the data and state of them.
安装
安装 NuGet 包 "WorkflowCore.Providers.Elasticsearch"
使用 Nuget 包命令
PM> Install-Package WorkflowCore.Providers.Elasticsearch
使用 .NET CLI
dotnet add package WorkflowCore.Providers.Elasticsearch
配置
当在创建service provider的时候,在IServiceCollection中
使用 .UseElasticsearch
扩展方法,
using Nest;
...
services.AddWorkflow(cfg =>
{
...
cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://localhost:9200")), "index_name");
});
使用
注入 ISearchIndex
service 到你的类中,然后使用 Search
方法.
Search(string terms, int skip, int take, params SearchFilter[] filters)
terms
一个字符串,如果是搜索多个内容,使用空格分隔表示;如果是空,则表示查找全部。程序将进行全文搜索,搜索的默认字符段包括* Reference * Description * Status * Workflow。
在这里,你也可以通过继承ISearchable实现自定义搜索类,如下所示。
using WorkflowCore.Interfaces;
...
public class MyData : ISearchable
{
public string StrValue1 { get; set; }
public string StrValue2 { get; set; }
public IEnumerable<string> GetSearchTokens()
{
return new List<string>()
{
StrValue1,
StrValue2
};
}
}
例子
在所有字段中搜索"puppies"
searchIndex.Search("puppies", 0, 10);
skip & take
使用 skip
和 take
分页获取你的查询结果, 其中,skip是从哪里开始,take是每一页的行数.
filters
You can also supply a list of filters to apply to the search, these can be applied to both the standard fields as well as any field within your custom data objects. There is no need to implement ISearchable
on your data object in order to use filters against it.
可用的过滤类型包括: * ScalarFilter * DateRangeFilter * NumericRangeFilter * StatusFilter
他们所在的命名空间: WorkflowCore.Models.Search
.
例子
Filtering by reference
using WorkflowCore.Models.Search; ...
searchIndex.Search("", 0, 10, ScalarFilter.Equals(x => x.Reference, "My Reference"));
过滤从某个时间开始后的流程
searchIndex.Search("", 0, 10, DateRangeFilter.After(x => x.CreateTime, startDate));
过滤在某个时间段内完成的流程
searchIndex.Search("", 0, 10, DateRangeFilter.Between(x => x.CompleteTime, startDate, endDate));
过滤状态为已完成的流程
searchIndex.Search("", 0, 10, StatusFilter.Equals(WorkflowStatus.Complete));
过滤自定义类数据的流程
class MyData
{
public string Value1 { get; set; }
public int Value2 { get; set; }
}
searchIndex.Search("", 0, 10, ScalarFilter.Equals(x => x.Value1, "blue moon"));
searchIndex.Search("", 0, 10, NumericRangeFilter.LessThan(x => x.Value2, 5))