我正在尝试MultiMatch
搜索条件。我知道过去曾问过类似的问题,但使用NEST
却没有。这是我目前的搜索,它适用于整个文档
var response = await this.client.SearchAsync<EmployeeDocument>(
x => x.Index(this.settings.IndexName).Type("employee").From(page * pageSize)
.Size(pageSize).Query(
q => q.MultiMatch(m => m
.Query(query)
.Type(TextQueryType.MostFields)
.MinimumShouldMatch("90%")
.Fields(f => f
.Field(_ => _.FullName, 3)
.Field(_ => _.Number, 3)
.Field(_ => _.Email)
.Field(_ => _.CompanyName, 2)
.Field(_ => _.JobNumber)
.Field(_ => _.Description))))
.Highlight(
h => h.PreTags("<mark>").PostTags("</mark>")
.Fields(
f => f.Field(p => p.FullName),
f => f.Field(p => p.CompanyName),
f => f.Field(p => p.JobNumber),
f => f.Field(p => p.Number),
f => f.Field(p => p.Email),
f => f.Field(p => p.Description))));
如何添加条件只搜索特定字段? (CompanyId)
例如,上述代码执行员工搜索,但只需在特定公司内搜索。
这就是我试过的
var response = await this.client.SearchAsync<EmployeeDocument>(
x => x.Index(this.settings.IndexName).Type("employee").From(page * pageSize)
.Size(pageSize).Query(
q => q.MultiMatch(m => m
.Query(query)
.Type(TextQueryType.MostFields)
.MinimumShouldMatch("90%")
.Fields(f => f
.Field(_ => _.FullName, 3)
.Field(_ => _.Number, 3)
.Field(_ => _.Email)
.Field(_ => _.CompanyName, 2)
.Field(_ => _.JobNumber)
.Field(_ => _.Description))))
.Query(_ => _.Bool(b => b.Must(descriptor =>
descriptor.Term(t =>
t.Field(document => document.CompanyGroupId).Value(4213))))) // I ADDED THIS SECTION HERE
.Highlight(
h => h.PreTags("<mark>").PostTags("</mark>")
.Fields(
f => f.Field(p => p.FullName),
f => f.Field(p => p.CompanyName),
f => f.Field(p => p.JobNumber),
f => f.Field(p => p.Number),
f => f.Field(p => p.Email),
f => f.Field(p => p.Description))));
我添加了额外的查询以执行bool
条件,但我似乎没有返回正确的输出。但我确定我的NEST
查询错误
答案 0 :(得分:1)
您希望使用bool
和must
子句构造filter
查询。在查询上使用operator overloading,可以更简洁地表达
var query = "query";
var page = 1;
var pageSize = 10;
var searchResponse = client.Search<EmployeeDocument>(x => x
.Index("indexName")
.Type("employee")
.From(page * pageSize)
.Size(pageSize)
.Query(q => q
.MultiMatch(m => m
.Query(query)
.Type(TextQueryType.MostFields)
.MinimumShouldMatch("90%")
.Fields(f => f
.Field(ff => ff.FullName, 3)
.Field(ff => ff.Number, 3)
.Field(ff => ff.Email)
.Field(ff => ff.CompanyName, 2)
.Field(ff => ff.JobNumber)
.Field(ff => ff.Description)
)
) && +q
.Term(f => f.CompanyGroupId, 4213)
)
.Highlight(h => h
.PreTags("<mark>")
.PostTags("</mark>")
.Fields(
f => f.Field(p => p.FullName),
f => f.Field(p => p.CompanyName),
f => f.Field(p => p.JobNumber),
f => f.Field(p => p.Number),
f => f.Field(p => p.Email),
f => f.Field(p => p.Description)
)
)
);
产生以下请求
{
"from": 10,
"size": 10,
"highlight": {
"pre_tags": [
"<mark>"
],
"post_tags": [
"</mark>"
],
"fields": {
"fullName": {},
"companyName": {},
"jobNumber": {},
"number": {},
"email": {},
"description": {}
}
},
"query": {
"bool": {
"must": [
{
"multi_match": {
"type": "most_fields",
"query": "query",
"minimum_should_match": "90%",
"fields": [
"fullName^3",
"number^3",
"email",
"companyName^2",
"jobNumber",
"description"
]
}
}
],
"filter": [
{
"term": {
"companyGroupId": {
"value": 4213
}
}
}
]
}
}
}