Queries for null or empty strings

Current version: 9.0

The nature of analytics data means that you sometimes want to run queries for null or empty string values.

Sitecore supports this in the LINQ layer and in the indexing layer by storing physical values of EMPTYVALUE and NULLVALUE. Sitecore translates this in the LINQ layer, so all you need to do is write LINQ queries as usual.

You must specify which fields you want to have an empty string and null support for in the FieldMap. You should not store empty strings and nulls in your index because they waste space.

However, there are some cases where you need to do this. For example, when you want users to be able to search for all customers that have not specified a gender and to contact these customers to determine this. To make this possible, you must set up the FieldMap to store empty strings in the Gender field.

It is possible to use custom values for nullValue and emptyString parameters. The only limitation is that the value should produce a single term after passing through the analyzer.

It is a good idea to use unique replacement tokens that cannot be confused with an actual field value.

Configuring the FieldMap to store empty strings and nulls in Lucene

For Lucene, you do this in the App_Config\Include\Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file in the <fieldNames hint="raw:AddFieldByFieldName"> section.

For example, to enable searching for items where the gender field is empty:

RequestResponse
<field fieldName="gender" storageType="NO"  indexType="TOKENIZED"    vectorType="NO" boost="1f" type="System.String" nullValue="NULLVALUE" emptyString="EMPTYVALUE" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

Configuring the FieldMap to store empty strings and nulls in Solr

For Solr, you do this in the App_Config\Include\Sitecore.ContentSearch.Solr.Indexes.config file in the <fieldNames hint="raw:AddFieldByFieldName"> section.

For example, to enable searching for items where the gender field is empty:

RequestResponse
<fieldType fieldName="gender" returnType="text" nullValue="NULLVALUE" emptyString="EMPTYVALUE"/>

Using code to search for empty strings

After you implement the configuration as described above, when you run the following query, it maps "" to EMPTYVALUE and null to NULLVALUE:

RequestResponse
return context.GetQueryable<Contact>().Where(i => i.Gender == “”).Take(10).ToList();

Using the Sitecore UI to search for empty strings

After you have configured fields to store empty strings, users can search for empty strings or null values from the Sitecore UI by using the replacement token as a value.

For example, after you implement the configuration described above, you can find items with an empty Gender field by entering gender: EMPTYVALUE in the Search field.

Search for an empty string

Do you have some feedback for us?

If you have suggestions for improving this article,