NEST查询精确文本匹配

时间:2015-08-25 17:44:54

标签: 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))))

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

我的搜索结果总是返回包含&#34;部分搜索&#34;串。

因此,如果我将搜索字符串提供为&#34; ter&#34;,我仍然可以获得所有3行。 ter@gmail.com

ter@hotmail.com

terrance@hotmail.com

如果搜索字符串是&#34; ter&#34;我希望看到没有返回任何行。如果搜索字符串是&#34; ter @ hotmail.com&#34;那么我只想看到&#34; ter@hotmail.com"。

不确定我做错了什么。

2 个答案:

答案 0 :(得分:3)

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

使用Elasticsearch的{​​{3}}可以看出标准分析器对给定字符串输入的影响:

curl -XPOST "http://localhost:9200/_analyze?analyzer=standard&text=ter%40gmail.com

文本输入需要进行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
      }
   ]
}

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

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

然后,默认情况下,将来自此匹配查询分析的结果标记组合到布尔或查询中,以使包含字段的倒排索引中的任何一个标记的任何文档都匹配。例如

文字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!

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

运行Match query会导致对 的术语查询的输入进行分析,即它是与术语输入完全匹配的查询,但是在索引时分析的字段上运行此操作可能是一个问题;由于该字段的值已经过分析,但是术语查询的输入没有,因此您将获得返回的结果,该结果与索引时发生的分析结果完全匹配术语输入。例如

// 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!

这不是我们想要的!

我们可能想要对此字段执行的操作在映射定义中将其设置为not_analyzed

putMappingDescriptor
    .MapFromAttributes()
    .Properties(p => p
        .String(s => s.Name(n => n.FieldName).Index(FieldIndexOption.NotAnalyzed)
    );

有了这个,我们可以使用Term query

Term filter搜索完全匹配
// 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")
            )
        )
    )
);

将产生以下查询DSL

{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "fieldName": "ter@gmail.com"
        }
      }
    }
  }
}

答案 1 :(得分:1)

您还可以执行MatchPhrasePrefix查询,以执行“精确”匹配。

.MatchPhrasePrefix(ma => ma.Field(field).Query(query))
相关问题