Elasticsearch NEST 查询的行为与 Kibana Dev Tools 中的等效查询不同
Elasticsearch NEST query not behaving like the equivalent query in Kibana Dev Tools
我正在尝试针对我的索引构建一个查询,该查询将使用 function_score
来提升字段具有特定值的记录。在 Kibana Dev Tools 中,我有以下查询 returns 3 按预期命中:
GET /myindex/_search
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "somename",
"fields": ["licenseName", "businessName"]
}
}
}
}
}
但是当我尝试用 NEST
重现它时,没有过滤发生,它只是 returns 索引中的所有记录。对我来说这看起来是一样的,但我一定遗漏了一些东西:
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
.Index("myindex")
.Query(qcd => qcd
.FunctionScore(fsqd => fsqd
.Query(fsqcd => fsqcd
.MultiMatch(mmqd => mmqd
.Query(message.Name)
.Fields(fd => fd
.Field(f => f.LicenseName)
.Field(f => f.BusinessName)
)
)
)
)
)
);
ETA:这是 DebugInformation
输出:
Valid NEST response built from a successful (200) low level call on POST: /myindex/_search?pretty=true&error_trace=true&typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://192.168.100.2:9200/ Took: 00:00:00.3650673
# Request:
{}
# Response:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : 1.0,
"hits" : [
// 10 records here
]
}
}
# TCP states:
Established: 78
CloseWait: 14
TimeWait: 12
# ThreadPool statistics:
Worker:
Busy: 1
Free: 32766
Min: 8
Max: 32767
IOCP:
Busy: 1
Free: 999
Min: 8
Max: 1000
对于我在 NEST
查询中遗漏的内容有什么想法可以使它像 Dev Tools 中的原始查询一样运行吗?
我想我找到了答案。来自 function_score 的文档:
To use function_score, the user has to define a query and one or more functions, that compute a new score for each document returned by the query.
因此,与我在 Dev Tools 中的手动查询不同,我向 NEST
查询添加了一个函数:
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
.Index("myindex")
.Query(qcd => qcd
.FunctionScore(fsqd => fsqd
.Query(fsqcd => fsqcd
.MultiMatch(mmqd => mmqd
.Query(message.Name)
.Fields(fd => fd
.Field(f => f.LicenseName)
.Field(f => f.BusinessName)
)
)
)
.Functions(sfd => sfd
.Weight(wfd => wfd
.Filter(wfqcd => wfqcd
.Term(tqd => tqd.Field(tqd => tqd.AccountId).Value(accountId))
)
.Weight(100)
)
)
)
)
);
我得到了我期望的查询和结果:
Valid NEST response built from a successful (200) low level call on POST: /myindex/_search?pretty=true&error_trace=true&typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://192.168.100.2:9200/ Took: 00:00:00.1412548
# Request:
{"query":{"function_score":{"functions":[{"filter":{"term":{"accountId":{"value":"9978f652-700a-4f63-8c71-ea4a11e6ddc9"}}},"weight":100.0}],"query":{"multi_match":{"fields":["licenseName","businessName"],"query":"somename"}}}}}
# Response:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 10.921473,
"hits" : [
// 3 records
]
}
}
# TCP states:
Established: 82
CloseWait: 14
TimeWait: 5
# ThreadPool statistics:
Worker:
Busy: 1
Free: 32766
Min: 8
Max: 32767
IOCP:
Busy: 1
Free: 999
Min: 8
Max: 1000
我正在尝试针对我的索引构建一个查询,该查询将使用 function_score
来提升字段具有特定值的记录。在 Kibana Dev Tools 中,我有以下查询 returns 3 按预期命中:
GET /myindex/_search
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "somename",
"fields": ["licenseName", "businessName"]
}
}
}
}
}
但是当我尝试用 NEST
重现它时,没有过滤发生,它只是 returns 索引中的所有记录。对我来说这看起来是一样的,但我一定遗漏了一些东西:
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
.Index("myindex")
.Query(qcd => qcd
.FunctionScore(fsqd => fsqd
.Query(fsqcd => fsqcd
.MultiMatch(mmqd => mmqd
.Query(message.Name)
.Fields(fd => fd
.Field(f => f.LicenseName)
.Field(f => f.BusinessName)
)
)
)
)
)
);
ETA:这是 DebugInformation
输出:
Valid NEST response built from a successful (200) low level call on POST: /myindex/_search?pretty=true&error_trace=true&typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://192.168.100.2:9200/ Took: 00:00:00.3650673
# Request:
{}
# Response:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : 1.0,
"hits" : [
// 10 records here
]
}
}
# TCP states:
Established: 78
CloseWait: 14
TimeWait: 12
# ThreadPool statistics:
Worker:
Busy: 1
Free: 32766
Min: 8
Max: 32767
IOCP:
Busy: 1
Free: 999
Min: 8
Max: 1000
对于我在 NEST
查询中遗漏的内容有什么想法可以使它像 Dev Tools 中的原始查询一样运行吗?
我想我找到了答案。来自 function_score 的文档:
To use function_score, the user has to define a query and one or more functions, that compute a new score for each document returned by the query.
因此,与我在 Dev Tools 中的手动查询不同,我向 NEST
查询添加了一个函数:
var byNameSearchResult = await _elasticClient.SearchAsync<MyModel>(sr => sr
.Index("myindex")
.Query(qcd => qcd
.FunctionScore(fsqd => fsqd
.Query(fsqcd => fsqcd
.MultiMatch(mmqd => mmqd
.Query(message.Name)
.Fields(fd => fd
.Field(f => f.LicenseName)
.Field(f => f.BusinessName)
)
)
)
.Functions(sfd => sfd
.Weight(wfd => wfd
.Filter(wfqcd => wfqcd
.Term(tqd => tqd.Field(tqd => tqd.AccountId).Value(accountId))
)
.Weight(100)
)
)
)
)
);
我得到了我期望的查询和结果:
Valid NEST response built from a successful (200) low level call on POST: /myindex/_search?pretty=true&error_trace=true&typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://192.168.100.2:9200/ Took: 00:00:00.1412548
# Request:
{"query":{"function_score":{"functions":[{"filter":{"term":{"accountId":{"value":"9978f652-700a-4f63-8c71-ea4a11e6ddc9"}}},"weight":100.0}],"query":{"multi_match":{"fields":["licenseName","businessName"],"query":"somename"}}}}}
# Response:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 10.921473,
"hits" : [
// 3 records
]
}
}
# TCP states:
Established: 82
CloseWait: 14
TimeWait: 5
# ThreadPool statistics:
Worker:
Busy: 1
Free: 32766
Min: 8
Max: 32767
IOCP:
Busy: 1
Free: 999
Min: 8
Max: 1000