【问题标题】:NEST is not returning values for an exact searchNEST 未返回精确搜索的值
【发布时间】:2020-06-22 06:09:36
【问题描述】:

我正在尝试使用 NEST 构建动态查询,如下所示

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.~ 索引中的真实值 ],我无法找到结果。

但是,如果我以“goldpgl”之类的小写形式传递值,它会起作用。

此外,它不适用于“Gold - PGL”或“SOME OTHER LOAN”等值。

我的 POCO 如下

public class ProductModel
    {
        public string product { get; set; }
        public string agencyid { get; set; }

    }

出了什么问题以及如何纠正?

【问题讨论】:

    标签: match nest elasticsearch-query term-query


    【解决方案1】:

    由于您没有提供映射和搜索查询,我假设它正在发生,因为您使用的是 term query 而不是 match query

    不分析术语查询意味着您在搜索查询中输入的任何内容都将与索引中的标记匹配。默认情况下,Elasticsearch 中的所有文本字段都使用standard analyzer,它将标记转换为小写。因此GoldPGL 不匹配,而goldpgl 在您的术语查询中匹配。

    虽然match 查询解释了官方文档是分析查询,并且相同的分析器应用于搜索词,该分析器在索引时应用,因此GoldPGL 以及goldpgl 转换为goldpgl 和查询都与文档匹配,Gold - PGL 也是如此,它也匹配并由我验证。

    Analyze API 非常方便地解决这些类型的问题,其中搜索查询与索引标记不匹配,下面显示了如何分析 GOLDPGL 的一个示例:

    POST /_分析

    {
        "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 展示了您的示例。

    索引定义

    发布/

    {
        "mappings": {
            "properties": {
                "product": {
                    "type": "text"
                }
            }
        }
    }
    

    索引一些文档


    POST //_doc/1

    {
        "product": "GoldPGL"
    }
    

    索引第二个文档

    {
        "product": "Gold - PGL"
    }
    

    现在使用术语查询搜索查询(如您的示例所示),不返回任何结果(使用GoldPGL 时)

    {
        "query": {
            "term": {
                "product": {
                    "value": "GoldPGL"
    
                }
            }
        }
    }
    

    当使用goldgpl时,它会给出结果

    {
        "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"
                    }
                }
            ]
    

    【讨论】:

    • 它在某种程度上起作用了。但如果我通过 "SOME OTHER LOAN" ,它匹配那个以及 "CAR LOAN" 。意味着正在发生类似搜索而不是精确搜索 - 如何解决这个问题?
    • 好的 - 解决了。我们需要使用 MatchPhrasePrefix 代替 Match。谢谢
    • @priyanka.sarkar,很高兴它有帮助 :-)
    • 嗨,你能帮我解决另一个问题吗stackoverflow.com/questions/60740162/…
    • @priyanka.sarkar,一定让我看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多