根据问题中提供的信息,听起来好像包含电子邮件地址的字段已用 Standard
Analyzer 索引,如果未指定其他分析器或未将字段标记为,则默认的分析器应用于字符串字段
not_analyzed。
通过使用Elasticsearch 的 Analyze
API ,可以看到标准分析器对给定字符串输入的影响:
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 } ]}我们可以看到标准分析器为输入
ter和生成了两个标记
gmail.com,这将存储在字段的倒排索引中。
现在,运行 匹配
查询将导致对
匹配
查询的输入进行分析,默认情况下,使用与在其中应用匹配查询的字段的映射定义中找到的分析器相同的分析器。
然后,默认情况下,将从此匹配查询分析得到的标记组合到 布尔值或 查询中,这样,包含该字段的反向索引中包含任何标记的任何文档都是匹配项。举个例子
text
ter@gmail.com,这意味着与该字段匹配
ter或匹配的任何文档
gmail.com都会被点击
// Indexinginput: ter@gmail.com -> standard analyzer -> ter,gmail.com in inverted index// Queryinginput: ter@gmail.com -> match query -> docs with ter or gmail.com are a hit!
显然,对于完全匹配,这根本不是我们想要的!
运行 术语
查询将导致 不
分析术语查询的输入,即,它是对与术语输入完全匹配的查询,但是在索引时间已分析的字段上运行此查询可能会成为问题;由于已对该字段的值进行了分析,但尚未对术语查询进行输入,因此您将获得返回的结果,该结果与在索引时间发生的分析结果完全匹配术语输入。例如
// Indexinginput: ter@gmail.com -> standard analyzer -> ter,gmail.com in inverted index// Queryinginput: ter@gmail.com -> term query -> No exact matches for ter@gmail.cominput: 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) );
有了此功能后,我们可以使用
过滤
查询通过
术语
过滤器搜索 完全匹配
****
****
// change dynamic to your typevar 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" } } } }}


