Ste*_*eve 1 elasticsearch nest
我在按特定术语搜索文档时遇到困难.我每次都得到零结果.
这是一个代码示例:
var customers = new List<SampleCustomer>();
customers.Add(new SampleCustomer(){id=1,firstname="John", surname="Smith", country = "UK", sex = "Male", age=30});
customers.Add(new SampleCustomer(){id=2,firstname="Steve", surname="Jones", country ="UK", sex = "Male", age=22});
customers.Add(new SampleCustomer(){id=3,firstname="Kate", surname="Smith", country ="UK", sex = "Female", age=50});
customers.Add(new SampleCustomer(){id=4,firstname="Mark", surname="Jones", country ="USA", sex = "Male", age=45});
customers.Add(new SampleCustomer(){id=5,firstname="Emma", surname="Jonson", country ="USA", sex = "Female", age=25});
customers.Add(new SampleCustomer(){id=6,firstname="Tom", surname="Jones", country ="France", sex = "Male", age=30});
customers.Add(new SampleCustomer(){id=7,firstname="Liz", surname="Web", country ="France", sex = "Female", age=45});
foreach (var customer in customers)
{
_elasticClient.DeleteById("sample", "SampleCustomers",customer.id);
_elasticClient.Index(customer, "sample", "SampleCustomers" , customer.id);
}
Run Code Online (Sandbox Code Playgroud)
使用此索引,我可以使用查询字符串过滤器查询名为smith的客户
var queryByQueryString = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.QueryString(qs => qs.Query("Smith").OnField("surname"))));
Run Code Online (Sandbox Code Playgroud)
但如果我尝试使用术语filer搜索客户,我得到零结果
var queryByTerm = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.Term(p => p.surname, "Smith")));
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么?在上面的例子中,我想确保我的查询只返回姓氏与"史密斯"完全相等的结果,如果某人有一个双管姓氏,如"史密斯琼斯",他们就不会出现在结果中.
如果没有看到您的映射,很难确定,但您的问题可能只是区分大小写.如果该"surname"
字段使用默认standard
分析器(除非您在映射中指定了一个),否则将令牌修改为小写.所以会有一个"smith"
令牌,但没有"Smith"
.当您使用查询字符串查询时,将分析您的查询文本(使用standard
分析器除非您提供一个),因此搜索文本将被修改为"smith"
与令牌匹配.但是term
过滤器不进行任何分析,并且过滤器文本"Smith"
与任何标记都不匹配,因此不会返回任何结果.
如果这确实是你的问题,那么这应该返回结果:
var queryByTerm = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.Term(p => p.surname, "smith")));
Run Code Online (Sandbox Code Playgroud)
或者,您可以在映射中将"surname"
字段设置为"index": "not_analyzed"
(将需要重新索引),因此标记不会低位,并且带有文本的术语过滤器"Smith"
将匹配.
归档时间: |
|
查看次数: |
2982 次 |
最近记录: |