NEST查询精确文本匹配

CSC*_*CSC 5 c# elasticsearch nest

我正在尝试编写一个NEST查询,该查询应该根据完全字符串匹配返回结果.我已经在网上进行过研究,并且有关于使用Term,Match,MatchPhrase的建议.我尝试了所有这些,但我的搜索返回的结果包含搜索字符串的一部分.例如,在我的数据库中,我有以下几行电子邮件地址:

ter@gmail.com

ter@hotmail.com

terrance@hotmail.com

无论我是否使用:

client.Search<Emails>(s => s.From(0)
                        .Size(MaximumSearchResultsSize)
                        .Query(q => q.Term( p=> p.OnField(fielname).Value(fieldValue))))
Run Code Online (Sandbox Code Playgroud)

要么

  client.Search<Emails>(s => s.From(0).
                              Size(MaximumPaymentSearchResults).
                              Query(q=>q.Match(p=>p.OnField(fieldName).Query(fieldValue))));                                              
Run Code Online (Sandbox Code Playgroud)

我的搜索结果总是返回包含"部分搜索"字符串的行.

所以,如果我提供搜索字符串为"ter",我仍然得到所有3行.ter@gmail.com

ter@hotmail.com

terrance@hotmail.com

如果搜索字符串是"ter",我希望看不到返回的行.如果搜索字符串是"ter@hotmail.com",那么我只想看到"ter@hotmail.com".

不知道我做错了什么.

Rus*_*Cam 8

根据您在问题中提供的信息,听起来包含电子邮件地址的字段已使用Standard Analyzer编制索引,如果未指定其他分析器或该字段未标记为,则默认分析器应用于字符串字段not_analyzed.

使用Elasticsearch的分析 API可以看到标准分析器对给定字符串输入的影响:

curl -XPOST "http://localhost:9200/_analyze?analyzer=standard&text=ter%40gmail.com
Run Code Online (Sandbox Code Playgroud)

文本输入需要进行 url 编码,如此处使用 @ 符号所示。运行此查询的结果是

{
   "tokens": [
      {
         "token": "ter",
         "start_offset": 0,
         "end_offset": 3,
         "type": "<ALPHANUM>",
         "position": 1
      },
      {
         "token": "gmail.com",
         "start_offset": 4,
         "end_offset": 13,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我们可以看到标准分析器为输入ter和生成两个标记gmail.com,这将存储在该字段的倒排索引中。

现在,运行匹配查询将导致对匹配查询的输入进行分析,默认情况下,使用与在应用匹配查询的字段的映射定义中找到的分析器相同的分析器。

来自该匹配查询分析的结果标记然后默认组合成一个布尔值或查询,这样任何包含该字段倒排索引中任何一个标记的文档都将是匹配的。例如

text ter@gmail.com,这意味着任何与该字段tergmail.com该字段匹配的文档都将被命中

// Indexing
input: ter@gmail.com -> standard analyzer -> ter,gmail.com in inverted index

// Querying
input: ter@gmail.com -> match query -> docs with ter or gmail.com are a hit!
Run Code Online (Sandbox Code Playgroud)

显然,对于完全匹配,这根本不是我们想要的!

运行Term查询将导致分析term 查询的输入,即它是一个与 term 输入完全匹配的查询,但在索引时已分析过的字段上运行它可能会出现问题;由于该字段的值已经过分析,但词条查询的输入没有经过分析,因此作为索引时发生的分析的结果,您将获得与词条输入完全匹配的结果。例如

// Indexing
input: ter@gmail.com -> standard analyzer -> ter,gmail.com in inverted index

// Querying
input: ter@gmail.com -> term query -> No exact matches for ter@gmail.com

input: ter -> term query -> docs with ter in inverted index are a hit!
Run Code Online (Sandbox Code Playgroud)

这也不是我们想要的!

我们可能想要对这个字段做的是将它设置not_analyzed在映射定义中

putMappingDescriptor
    .MapFromAttributes()
    .Properties(p => p
        .String(s => s.Name(n => n.FieldName).Index(FieldIndexOption.NotAnalyzed)
    );
Run Code Online (Sandbox Code Playgroud)

有了这个,我们可以使用Filtered查询Term过滤器搜索完全匹配

// change dynamic to your type
var docs = client.Search<dynamic>(b => b
    .Query(q => q
        .Filtered(fq => fq
            .Filter(f => f
                .Term("fieldName", "ter@gmail.com")
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

这将产生以下查询 DSL

{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "fieldName": "ter@gmail.com"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)