ElasticSearch and Nest filtering does not work

旧时模样 提交于 2019-12-22 10:30:30

问题


I run a query that returns 10 results. There is a property in my document called Type. The value of this property for some records is an empty string and for some other records is either "AudioAlbum" or "AudioRington".

I want to do two things: 1- Exclude the documents that their Type property does not have a value from the search result. 2- Get AudioAlbums only (as a different search).

My search code for getting AudioAlbums is this:

    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();

Without the Filter extension method I get 10 records (including two AudioAlbums). when I add the .Filter method I get zero records.

Also I want to exclude the records whose Type property does not have a value. Again my code (given below) does not record any results:

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();

What's wrong with my code?


回答1:


Copy Pasted Answer from the elasticsearch user list

In your first example you filter on the field "type" and in the second on the "Type" I suppose you need to change the first to "Type".

Depending on your analysis for the "Type" field you might also need to lowercase "AudioRingtone" too.

In the second example you are using the wrong query:

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

Which in NEST you can do as such:

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

If you issue an empty term filter/query you hit NEST conditionless query logica, and nest will infact not even send the filter at all.

See http://nest.azurewebsites.net/nest/writing-queries.html for help using the query dsl.



来源:https://stackoverflow.com/questions/17540509/elasticsearch-and-nest-filtering-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!