问题描述
我正在尝试使用NEST构建动态查询,如下所示:
I am trying to build a dynamic query using NEST which is as under
string product = "goldpgl";
string agencyid = "1123";
ISearchResponse <ProductModel> res = client().Search<ProductModel>(s => s
.Index("proddata")
.From(0)
.Size(100)
.Query(q =>
+q.Term(p => p.product, product) &&
+q.Term(p => p.agencyid, agencyid)));
如果我通过了product value = "GoldPGL"
[索引中的N.B.〜实际值],我将找不到结果.
If I pass, product value = "GoldPGL"
[ N.B.~ Real value in the index ], I am not able to find the result.
但是,如果我以小写形式(例如"goldpgl")传递值,那么它将起作用.
However, if I pass the value in lowercase like "goldpgl", it works.
此外,它不适用于"Gold-PGL"或"SOME OTHER LOAN"之类的值.
Also, it does not work for values like "Gold - PGL" or "SOME OTHER LOAN".
我的POCO如下
public class ProductModel
{
public string product { get; set; }
public string agencyid { get; set; }
}
怎么了?如何纠正?
推荐答案
由于您尚未提供映射和搜索查询,因此我假设发生了这种情况,因为您使用的是术语查询不是匹配查询.
As you have not provided the mapping and search query, I am assuming its happening because you are using the term query not the match query.
不分析术语查询,这意味着您在搜索查询中输入的任何内容都将与索引中的标记匹配.默认情况下,Elasticsearch中的所有文本字段均使用标准分析器,它将令牌转换为小写.因此GoldPGL
不匹配,而goldpgl
在您的字词查询中匹配.
Term queries are not analyzed means whatever you entered in your search query would be matched against the tokens in the index. And by default, all the text fields in Elasticsearch use the standard analyzer which converts tokens to lowercase. hence GoldPGL
doesn't match while goldpgl
matches in your term query.
在解释match
查询时,对官方文档进行了分析查询,并且对检索词使用了相同的分析器,该检索词在索引时间应用,因此GoldPGL
以及goldpgl
转换为goldpgl
,并且这两个查询都与文档匹配,并且与Gold - PGL
相同,该文档也由我匹配并验证.
While match
query as explained the official document is analyzed query and the same analyzer is applied on the search term, which is applied at index time, hence GoldPGL
as well as goldpgl
converted to goldpgl
and both the query matches the documents and same is with Gold - PGL
which also matches and verifies by me.
分析API 非常方便为了解决这些类型的问题,搜索查询与索引的令牌不匹配,并且显示了如何分析GOLDPGL
的一个示例,如下所示:
Analyze API comes very handy to troubleshoot these types of issue, where search query doesn't match the indexed tokens and one example of how GOLDPGL
would be analyzed is shown below:
POST/_analyze
POST /_analyze
{
"text": "GOLDPGL",
"analyzer" : "standard"
}
{ "token": "goldpgl",}
{
"text": "GOLD - PGL",
"analyzer" : "standard"
}
{
"token": "gold",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "pgl",
"start_offset": 7,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 1
}
我转载了您的问题,并且由于我不熟悉NEST,因此使用REST API展示了您的示例.
I reproduced your issue and as I am not familiar with NEST, showing your example using the REST API.
POST/
{
"mappings": {
"properties": {
"product": {
"type": "text"
}
}
}
}
为某些文档编制索引
POST//_ doc/1
POST //_doc/1
{
"product": "GoldPGL"
}
索引第二个文档
{
"product": "Gold - PGL"
}
现在使用词条查询的搜索查询(如您的示例所示),不返回任何结果(使用GoldPGL
时)
Now search query using term query(as shown in your example), doesn't return any result (when GoldPGL
is used)
{
"query": {
"term": {
"product": {
"value": "GoldPGL"
}
}
}
}
使用goldgpl
时,它会给出结果
When used goldgpl
, it gives result
{
"query": {
"term": {
"product": {
"value": "goldpgl"
}
}
}
}
结果
"hits": [
{
"_index": "so-term-nest",
"_type": "_doc",
"_id": "1",
"_score": 0.8025915,
"_source": {
"product": "GoldPGL"
}
}
]
解决方案(使用匹配查询)
{
"query": {
"match" : {
"product" : {
"query" : "GoldPGL"
}
}
}
}
这将返回结果
"hits": [
{
"_index": "so-term-nest",
"_type": "_doc",
"_id": "1",
"_score": 0.8025915,
"_source": {
"product": "GoldPGL"
}
}
]
这篇关于NEST未返回精确搜索的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!