C# 使用IProviderSearchContext中的GetQueryable搜索空多列表
在C# 使用IProviderSearchContext中的GetQueryable搜索空多列表,c#,lucene,sitecore,lucene.net,sitecore7,C#,Lucene,Sitecore,Lucene.net,Sitecore7,在Sitecore中,我们有包含multilist字段的项。字段被存储和索引,因此我们可以使用此语法查询它们 using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) { var templateId = new ID(Config.NewsTemplate); var q = context.GetQueryable<NewsSearchR
Sitecore
中,我们有包含multilist
字段的项。字段被存储和索引,因此我们可以使用此语法查询它们
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; }
}
这是不允许的:
q = q.Where(x => x.AppIdOr == null);
有什么想法吗?Lucene不知道如何搜索空值,因为没有可供Lucene搜索的索引值。 我建议在索引时,如果字段为空/null(使用自定义或计算字段),则添加一个默认值,例如(ID.null),然后在搜索查询中搜索ID.null。 我以前没有尝试过,但我认为它会起作用
q = q.Where(x => x.AppIdOr == null);