How I can use sitecore query fields to filter and not bring results if a specific field is empty?

I have tried something like this:

location:{41c52cce-69cc-444d-b505-044addf0d9e1};-custom:title|''

I have also tried double quotes but I get the same result

thanks

  • 1
    are you using Sitecore Query or is this a Lucene/Solr query - it doesn't look like Sitecore Query to me, – Richard Seal Sep 19 '16 at 20:49
  • I am using Sitecore query builder. It is the query build field type available on Sitecore 7. It is a field where you can build queries – Diego Sep 19 '16 at 21:12
up vote 7 down vote accepted

Typically empty + null values are not stored in the Lucene index, it's really designed for queries that are finding data rather than the absence of it. If you searched for +fieldName:* then that should find documents which have an entry (and therefore value) for fieldName.

I'm not entirely sure how the query-builder maps the query, it looks like judging by your example what you'd want is +custom:fieldName|*

If this doesn't work, Sitecore does have a mechanism of allowing you to specify a string value that takes the place of empty / null when indexing, therefore giving you something to search for. To do this you will need to add the field to the index manually in your indexing configuration, specifying the string to use as the "empty" value:

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

You should then be able to filter these documents out of a search using -custom:title|NULLVALUE -custom:title|EMPTYVALUE.

A drawback with this approach is that you want these two strings to be values that your field value will never genuinely be!

More information can be found here - https://doc.sitecore.net/sitecore_experience_platform/developing/developing_with_sitecore/search_and_item_buckets/queries_for_null_or_empty_strings

  • Using +custom:title|* works. Apparently just specifying the field with the * char it will tell lucene to bring only those with values set. using the opposite works also for bringing only the ones with no values: -custom:title|* – Diego Sep 20 '16 at 14:33
  • I tried to use this solution with a multivalue field and it didn't work. Apparently, the multilistfield reader always returns a list, even if it is empty preventing this mechanism to work. I have replaced it with my own implementation where I return an empty string when the list is empty, and now Sitecore is storing my "emptyString" value in the index as expected. link – Vicent Galiana Jun 13 '17 at 7:14
  • @VicentGaliana : Shouldn't your class override from Sc.ContentSearch.FieldReaders.MultiListFieldReader instead of Sc.ContentSearch.FieldReaders.FieldReader? – Jorrit Schippers Dec 20 '17 at 13:10

If using solr it would be -custom:* but I am actually not sure if that works in Lucene

  • thanks Klaus. how would that work in this case? -custom:* is a wildcarn isn't it? how can I specify for that specific field only? – Diego Sep 19 '16 at 20:33
  • The -custom:* is a wildcard for the custom field only - it actually says to remove all documents with a field custom that has terms matching * (which of course would be any term) – Klaus Petersen Oct 31 '16 at 21:47

The Sitecore query field is basically building up a Solr/Lucene query to pass through to the index.

In your example you have location:{41c52cce-69cc-444d-b505-044addf0d9e1}; which filters by the location and then you add your custom field as a not, so:

-custom:title|[* TO *]

an alternative would be

-custom:title|["" TO *]

What you are doing here is telling the index that the field must exist, but not where it contains any data.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.