我在过滤搜索时遇到问题,试图匹配值列表。
这是我的代码
List<string> DepartmentIds = new List<string>(){"d1","d2","d3","d4"};
term = "*" + "akshay" + "*"
var searchResults = client.Search<Employee>(c => c
.Index(index)
.Type(type)
.Query(+q.Term(f => f.DeptId.ToLower(), DepartmentIds)) // Facing Issue in Matching list of Department IDs
&& ((q.Wildcard(wc => wc.Field(f => f.FirstName).Value(term)))
|| (q.Wildcard(wc => wc.Field(f => f.LastName).Value(term)))
));
假设有10个部门,我想从部门D1,D2,D3和D4获取结果。我基本上应该看看索引文档中的部门ID是否与4个部门中的任何一个匹配。
由于部门ID及其值的数量将是动态的。我想基本上在NEST Client中找到运算符中sql的等价物,这样我就可以适当地过滤我的搜索。
答案 0 :(得分:1)
您可以使用 Terms
query 作为此
List<string> departmentIds = new List<string>() { "d1", "d2", "d3", "d4" };
var term = "*" + "akshay" + "*";
var index = "index";
var type = "type";
var searchResults = client.Search<Employee>(c => c
.Index(index)
.Type(type)
.Query(q =>
+q.Terms(t => t
.Field(f => f.DeptId)
.Terms<string>(departmentIds)
) && (
q.Wildcard(wc => wc.Field(f => f.FirstName).Value(term)) ||
q.Wildcard(wc => wc.Field(f => f.LastName).Value(term))
)
)
);
将生成以下查询
{
"query": {
"bool": {
"must": [
{
"bool": {
"filter": [
{
"terms": {
"deptId": [
"d1",
"d2",
"d3",
"d4"
]
}
}
]
}
},
{
"bool": {
"should": [
{
"wildcard": {
"firstName": {
"value": "*akshay*"
}
}
},
{
"wildcard": {
"lastName": {
"value": "*akshay*"
}
}
}
]
}
}
]
}
}
}
请记住wildcard queries can be slow and it is recommended not to start them with the *
wilcard。
您可能希望查看 completion suggesters 以了解您的工作