我正在使用Sitecore ContentSearch API。我已经在Item中索引了一组字段。是否可以同时查询多个字段?
public static List<MovieSearchResultItem> SearchResults(string txtQuery)
{
using (var context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
var query = context.GetQueryable<MovieSearchResultItem>().Where(result => result.Body.Contains(txtQuery)).ToList();
return query;
}
}
在上面的查询中,我只是使用Body field.how来包含多个字段。我的搜索数据可能是标题或正文或其他字段,所以我想检查所有三个字段。
答案 0 :(得分:1)
尝试在Where
子句中添加另一个条件:
var query = context.GetQueryable<MovieSearchResultItem>()
.Where(result => result.Body.Contains(txtQuery)
|| result.Title.Contains(txtQuery)
|| result.OtherField.Contains(txtQuery)).ToList();
答案 1 :(得分:1)
很简单,你可以像这样添加表达式:
var query = context.GetQueryable<MovieSearchResultItem>().Where(result => result.Body.Contains(txtQuery) || result.Title.Contains(txtQuery))
或者你可以在这里使用PredicateBuilder,更多信息: http://www.sitecore.net/Learn/Blogs/Technical-Blogs/Sitecore-7-Development-Team/Posts/2013/05/Sitecore-7-Predicate-Builder.aspx
答案 2 :(得分:1)
我可以推荐PredicateBuilder。例如:
Expression<Func<SearchResultItem, bool>> predicate = PredicateBuilder.True<SearchResultItem>();
predicate = predicate.And(p => p.TemplateName.Equals("News"));
predicate = predicate.And(p => p.Language == Context.Language.Name);
List<SearchResultItem> results = context
.GetQueryable<SearchResultItem>()
.Where(predicate)
.ToList();