我需要使用具有多个相似条件的NEST客户端从ES获取文档。
我的查询如下:
SELECT * FROM Customer WHERE CustomerName LIKE '%john%' OR CustomerName Like '%george%'
我的弹性搜索NEST查询(对于单个操作)如下:
var customers= ElasticSearchHelper.ElasticClient.SearchAsync<Customer>(body => body
.Take(100000)
.Filter(f => f
.And
(
fs=> fs.Query(q=> .QueryString(qs => qs
.Query("*" + SearchText + "*")
.OnField(new string[] { "FirstName"})
.DefaultOperator(Operator.or)
.MinimumShouldMatchPercentage(100)
.AnalyzeWildcard(true)))
)));
return customers.Documents;
如何在单个字段上执行多个类似操作?请指导我做错了什么。
答案 0 :(得分:2)
您需要使用的是OR过滤器与Regex过滤器结合使用:
SearchDescriptor<T> searchDescriptor = new SearchDescriptor<T>();
FilterDescriptor<T> filterDescriptor = new FilterDescriptor<T>();
FilterContainer filterContainer1 = new FilterContainer();
filterContainer1 = filterDescriptor.Regexp(rg =>
rg.OnField("CustomerName").Value(".*" + "john" + ".*"));
FilterContainer filterContainer2 = new FilterContainer();
filterContainer2 = filterDescriptor.Regexp(rg =>
rg.OnField("CustomerName").Value(".*" + "george" + ".*"));
searchDescriptor.Filter(flt =>
flt.Or(filterContainer1, filterContainer2));
var resulet = this.ElasticClient().Search<T>(body => searchDescriptor);
将生成以下查询(T是您的文档类型):
{
"filter": {
"or": {
"filters": [
{
"regexp": {
"CustomerName": {
"value": ".*john.*"
}
}
},
{
"regexp": {
"CustomerName": {
"value": ".*doe.*"
}
}
}
]
}
}
}