Sitecore + Lucene Search FieldQuery with and empty string

爱⌒轻易说出口 提交于 2019-12-12 12:19:17

问题


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.



来源:https://stackoverflow.com/questions/13869427/sitecore-lucene-search-fieldquery-with-and-empty-string

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