2013-07-09 131 views
3

我運行了一個返回10個結果的查詢。我的文檔中有一個屬性叫做Type。該屬性對於某些記錄的值是空字符串,對於其他一些記錄是「AudioAlbum」或「AudioRington」。ElasticSearch和Nest過濾器不起作用

我想做兩件事:1-排除其Type屬性沒有來自搜索結果的值的文檔。 2-僅獲取AudioAlbums(作爲不同的搜索)。

用於獲取AudioAlbums我的搜索代碼是這樣的:

var docs = client.Search<content>(
       b => b.Type("content") 
       .Query(q => q.Fuzzy(fz => fz 
       .OnField("title").Value(keyWord) 
       .OnField("artists.name"))) 
       .Filter(x => x.Term("type", "AudioRingtone"))) 
       .Documents.ToList(); 

沒有過濾器擴展方法,我得到10條(其中包括兩個AudioAlbums)。當我添加.Filter方法時,我得到零記錄。

另外我想排除其Type屬性沒有值的記錄。再次我的代碼(下面給出)不記錄任何結果:

BaseFilter notFilter = Filter.Not(x => Filter.Term("Type", string.Empty)); 
var docs = client.Search<content>(
       b => 
       b.Type("content") 
       .Query(q => q.Fuzzy(fz =>fz.OnField("title") 
       .Value(keyWord) 
       .OnField("artists.name"))) 
       .Filter(notFilter)).Documents.ToList(); 

我的代碼有什麼問題?從elasticsearch用戶列表

+0

嗨阿拉夫我也回答了郵件列表上的這個問題是否解決了這個問題? –

+0

是的Martijn非常感謝 – Aref

回答

5

複製粘貼的回答

在你的第一個例子中,你在球場上的「類型」,並在「類型」第二過濾器,我想你需要改變第一個「類型」。

根據您對「類型」字段的分析,您可能也需要小寫「AudioRingtone」。

在第二個例子中,你使用了錯誤的查詢:

http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter/

其中在NEST你可以做這樣的:

https://github.com/elasticsearch/elasticsearch-net/blob/master/src/Tests/Nest.Tests.Unit/Search/Filter/Singles/MissingFilterJson.cs

如果發出空項過濾器/查詢你點擊NEST無條件查詢邏輯,並且nest甚至不會發送過濾器。

請參閱http://nest.azurewebsites.net/nest/writing-queries.html以獲得使用查詢dsl的幫助。