Sitecore + Lucene Search FieldQuery with an empty string

I am creating a Sitecore.Ecommerce.Search.Query file using FieldQuery objects. I then convert the Sitecore query to Lucene.Net.Search.Query using the LuceneQueryBuilder class. Everything with the query works fine except for the fields where I am trying to match an empty string.

So ... this works:

new FieldQuery(FieldName, "1", MatchVariant.NotEquals)

but this is not so:

new FieldQuery(FieldName, string.Empty, MatchVariant.NotEquals)

I reflected both the Sitecore.Ecommerce assembly and the Lucene.Net assembly, but I did not find any obvious problems. But, when I look at the term that is created and used in the Lucene query, it looks like this:

-FieldName:

which, I believe, is wrong ... but maybe it’s right, and I just don’t have the correct setting of the field indexes ... I’m not sure what is honest.

Any help is greatly appreciated.

Thanks!

+4
source share
1 answer

Lucene does not support searching for null / empty values. After all, there is nothing indexed to find it. Lucene uses an inverted index that specifies certain types of queries, including queries for purely negation and searching for zeros, difficult or even impossible.

If you need to search for documents in which certain fields are equal to zero, you must save the default value in the field (for example, "NULL") that you can search.

However, you can create

 new RangeQuery(FieldName, null, null, true, true); 

Creates a range query with open upper and lower bounds, so it matches everything that matters.

Not the best way to do this, but not one of them asks for denial only.

+6
source

All Articles