Sitecore Search, and before it Lucene indexes are great to use within Sitecore implementations when you have a large list of items that you need to query. The new namespace Sitecore.Search allows you to query specific fields for values, and through the use of the QueryOccurance enumerator, gives you the option to choose whether the value you’re looking for must equal that of the item (Must), cannot equal that of the item (MustNot), or can equal that item (Should). The ability to set a MustNot QueryOccurance cannot be understated, as it allows you the ability to weed out any items with a specific value.
For instance, I’ve had the case where I used an “Inactive” checkbox field on an item and I wanted my query to only return items that are active (thus the checkbox is not checked), so I set up a MustNot query on that field with the value 1. (The value 1 indicates that the checkbox is checked.) Unfortunately, there is a little bit of a caveat if this is the only query you need – because it seems that a lone MUSTNOT query will not return results. Don’t fret though, there’s a pretty easy way around this that will still give you the results you’re looking for.
Check out this blog post for the quick technical details on how to utilize a lone MustNot query.