Sitecore + Lucene Search FieldQuery with and empty

2019-06-25 12:56发布

问题:

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

So... this works:

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

but this does not:

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

I have reflected through both the Sitecore.Ecommerce assembly and the Lucene.Net assembly as well but I have not found any obvious issues. 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 incorrect... but maybe it is correct and I just don't have the correct field indexes setup... I'm not sure to be honest.

Any help is greatly appreciated.

Thanks!

回答1:

Lucene does not really support searching for null/empty values. There is nothing indexed for it to find, after all. Lucene uses an inverted index, which makes certain kinds of queries, including pure negation queries and searching for nulls, difficult or even impossible.

If you need to search for documents in which certain fields are null, you should store a default value in the field (for instance "NULL") which you can search for.

That said, you could create

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

Which constructs a range query with open upper and lower bounds, so it matches anything that has a value.

Not a good way to do it, but neither is querying with only a negation.