我無法通過特定術語搜索文檔。每次我都會得到零結果。通過使用NEST客戶端的術語查詢進行搜索以進行彈性搜索
這裏是一個代碼示例:使用這個指標,我可以使用查詢字符串過濾器
var queryByQueryString = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.QueryString(qs => qs.Query("Smith").OnField("surname"))));
查詢客戶與史密斯的名字,但如果我試圖尋找客戶
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);
}
使用術語過濾器,我得到結果爲零
var queryByTerm = _elasticClient.Search<SampleCustomer>(s =>
s.From(0).Size(10).Type("SampleCustomers")
.Query(q => q.Term(p => p.surname, "Smith")));
我不知道我在做什麼錯?在上面的例子中,我想確保我的查詢只返回姓氏正好等於「Smith」的結果,並且如果某人有一個雙管姓例如「Smith Jones」,他們就不會出現在結果中。
謝謝是的小寫工作。我不能讓它與史密斯一起工作。我確實添加了not_analysed屬性到Sample customer類的surname屬性,但它沒有改變任何東西。 例如 [ElasticProperty(Index = FieldIndexOption.not_analyzed)] public string surname {get;組; } – Steve
您是否使用新映射重新編制索引?你確定它有效嗎?你可以發佈映射嗎?沒有看到映射,很難知道什麼是錯的。我對Nest並不熟悉,但只需點擊http:// [endpoint]/[index_name]/_mapping即可獲得映射 –