gpt4 book ai didi

elasticsearch - Elasticwhere NEST条件为

转载 作者:行者123 更新时间:2023-12-03 01:40:54 26 4
gpt4 key购买 nike

我正在尝试使用条件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查询是错误的

最佳答案

您要使用boolmust子句构造一个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
}
}
}
]
}
}
}

关于elasticsearch - Elasticwhere NEST条件为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47669984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com