Domanda

In Sitecore we have items that contain a multilist field. The field is stored and indexed, so we can query them using this syntax.

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
    var templateId = new ID(Config.NewsTemplate);
    var q = context.GetQueryable<NewsSearchResultItem>().Where(x => x.Language == searchContext.Language && x.TemplateId == templateId);

        var appIdPrd = PredicateBuilder.True<NewsSearchResultItem>();
        foreach (var t in searchContext.AppIds)
        {
            var id = GetId(t);
            appIdPrd = appIdPrd.Or(p => p.AppIdOr.Contains(id));
        }
        q = q.Where(appIdPrd);

   List<NewsItem> items = new List<NewsItem>(q.Count());
}

class NewsSearchResultItem : SearchResultItem
{
    public string Title { get; set; }
    public string Body { get; set; }
    [IndexField("apps_or")]
    public List<ID> AppIdOr { get; set; }
}

It works when the item HAS one or more values in the multilist field apps_or. But I also want to search for items that have no items in the multilist field. this never returns items:

q = q.Where(x => x.AppIdOr.Count == 0); 

and this is not allowed:

q = q.Where(x => x.AppIdOr == null); 

any ideas?

È stato utile?

Soluzione

Lucene does not know how to search for null values, as there is no value indexed for lucene to search for. I would suggest at the time of indexing, you add a default value if the field was empty/null (using custom or computed field), for example (ID.Null), then in your search query you could search for ID.Null. Have not tried this before, but i think it would work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top