我很难按特定术语搜索文档。每次我都得到零结果。
这是一个代码示例:
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);
}
使用此索引,我可以使用查询字符串过滤器查询名字为 smith 的客户
var queryByQueryString = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.QueryString(qs => qs.Query("Smith").OnField("surname"))));
但如果我尝试使用术语文件管理器搜索客户,我得到零结果
var queryByTerm = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.Term(p => p.surname, "Smith")));
我不知道我做错了什么?在上面的示例中,我想确保我的查询只返回姓氏完全等于“Smith”的结果,并且如果有人有一个双管姓氏,例如“Smith Jones”,他们就不会出现在结果中。