Sitecore ContentSearch - Filtering by null and empty strings

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

By default Sitecore will not save null and empty strings in your indexes as it wastes space.
However, you might need to do something like this:
using (var context = ContentSearchManager.GetIndex("MyIndex").CreateSearchContext())
{
var query = context.GetQueryable<SearchResultItem>().Where(x => x.Name != null);
var results = query.GetResults();
}
But that will result in an exception:
System.NotSupportedException: Comparison of null values is not supported.
Comparing with an empty string will not throw an exception, but it won’t return the expected results either, as the query will be translated into -MatchNoDocsQuery[] +*:* (Lucence).
Luckily there is a solution. It is possible to overwrite this functionality on a per field basis.
You can add a nullValue and emptyString parameter to the needed fields in your index configuration under the <fieldNames hint="raw:AddFieldByFieldName"> section:
<field
fieldName="name" storageType="NO" indexType="TOKENIZED"
vectorType="NO" boost="1f" type="System.String"
nullValue="NULL" emptyString="EMPTY"
settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration,
Sitecore.ContentSearch.LuceneProvider" />
This will map nulls and empty strings to the string values "NULL" and "EMPTY" respectively. You will now be able to do queries on nulls and empty strings without getting exceptions and also get the expected results.
You should be aware that this will increase the size of your index slightly. In most cases I don’t think it will have any noticable impact.
You can read a little more about this subject in Sitecore’s own post about it.