Search code examples
c#lucenesitecoresitecore7sitecore7.2

Search empty fields


I'm trying to exclude search results that have a field that stores the ID of an item is empty. This field, for example, is called 'type'. I haven't been able to do this using LINQ. Here is my code example.

 public class SearchItem : SearchResultItem
 {
    [IndexField("type")]
    public string Type{ get; set; }    
 }

 public class Search
 {
    public static IEnumerable<Item> GetItems()
    {
        List<Item> items = new List<Item>();
        var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));

        using (var context = index.CreateSearchContext())
        {
            var indexItems = context.GetQueryable<SearchResultItem>()
                .Where(x => !string.IsNullOrEmpty(x.Type))
                .OrderByDescending(x => x.ReleaseDate);
             
            foreach(var indexItem in indexItems)
            {
                var tempItem = indexItem.GetItem();
                items.Add(tempItem);
            }
        }
        return items;
    }
 }

The empty string comparison isn't working and it the items collection contains items that have empty strings for Type field. I'm using out of the box settings for Lucene.

Also, please poke holes in my code if you see something not right. This is my first time with Sitecore 7 Search.


Solution

  • Not sure if the string.IsnullOrEmpty is supported by Sitecore Linq, try var indexItems = context.GetQueryable() .Where(x => x.Type != null) .OrderByDescending(x => x.ReleaseDate);