Elasticsearch搜索
nuget安装WorkflowCore.Providers.Elasticsearch
设置
using Nest;
services.AddWorkflow(cfg =>
{
...
cfg.UseElasticsearch(new ConnectionSettings(new Uri("http://localhost:9200")), "index_name");
});
使用
将ISearchIndex
服务注入,并调用Search方法
Search(string terms, int skip, int take, params SearchFilter[] filters)
- terms
以空格分隔的搜索字符串,空字符串会匹配到所有内容。该字符串会对工作流的描述、引用、状态、工作流定义进行搜索。
如果自定义数据对象实现了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
};
}
}
searchIndex.Search("puppies", 0, 10);
查询所有包含puppies字段
- skip/take
跳过多少,取多少
- filters
用于搜索的筛选器列表,可以用来刷选标量、日期范围、数字范围、状态
案例:
- 通过引用过滤
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<MyData>(x => x.Value1, "blue moon"));
searchIndex.Search("", 0, 10, NumericRangeFilter.LessThan<MyData>(x => x.Value2, 5))