3
votes

i'm trying to create a query in NEST that would be like:

get * where CityName!='' and StreetName!=''

Using the existing and missing filters is not helping because the fields are not null. I also tried to use the queryString with the wildcard (*) but i get all the documents back in the result. I also tried a script filter like this :

qq.Filtered(ft => ft.Filter(f => f.Script(s => s.Script("doc['cityName'].value.length() > 0"))));

Also not working. The fields are indexed with standard tokenizer and also using ICU folding. Any solution ?

UPDATE !!!!

I managed to do this stuff by using the Regexp filter. so it's something like this :

 QueryContainer notNullQuery = null;

                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.StdCityName).Value("[a-z0-9]+[\\s]?"))));
                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.StdStreetCode).Value("[0-9]+"))));
                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.StdStreetPostCode).Value("[a-z0-9]+[\\s]?"))));
                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.LastName1).Value("[a-z0-9]+[\\s]?"))));
                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.FirstName1).Value("[a-z0-9]+[\\s]?"))));
                notNullQuery &= qq.Filtered(ft => ft.Filter(f => f.Regexp(r => r.OnField(ff => ff.SourceDisplayPhone).Value("[0-9]+"))));

                return notNullQuery;

LATER UPDATE: For NEST 5.x index the field as Keyword then do a query search on term. Don't forget to put Verbatim() or the query will not be sent to server.

qq.Bool(b => b.MustNot(mn => mn.Term(t => t.Field(f => f.Email).Value("").Verbatim())));
1

1 Answers

0
votes

By default NEST uses a feature called conditionless queries to rewrite queries without you having to write a lot of if statements to build up the query/filter. See http://nest.azurewebsites.net/nest/writing-queries.html

If you want to turn this off you can at any point in the graph call .Verbatim(bool verbatim = true) this flag is carried over to children and will cause them to be written to the resulting query/filter even if they are conditionless.

Similarly setting .Strict(bool strict = true) will throw an exception when a query is conditionless but is never allowed to be.