How to perform search for multiple terms in Sitecore 7 ContentSearch API?

♀尐吖头ヾ 提交于 2019-12-03 07:50:16

All of the predicate builders I tried didn't work, however, Sitecore 7 ships with a PredicateBuilder of it's own that did the trick.

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.Utilities;
// Sitecore 7 (Update 1+): using Sitecore.ContentSearch.Linq.Utilities;

...

var index = ContentSearchManager.GetIndex("sitecore_web_index");
using (var context = index.CreateSearchContext())
{
    var predicate = PredicateBuilder.True<SearchResultItem>();
    foreach (var t in term.Split(' '))
    {
        var tempTerm = t;
        predicate = predicate.Or(p => p.Content.Contains(tempTerm));
    }

    var results = context.GetQueryable<SearchResultItem>().Where(predicate).GetResults();

    ...
}
Bartłomiej Mucha

I think this is related to linq not to sitecore.

I don't test this but have a look at this article http://www.albahari.com/nutshell/predicatebuilder.aspx

You can also look at this documentation http://sdn.sitecore.net/upload/sitecore7/70/developer's_guide_to_item_buckets_and%20search_sc7-a4.pdf

I was able to use PredicateBuilder with Solr and implement queries including the OR operator. See http://www.nttdatasitecore.com/en/Blog/2013/November/Building-Facet-Queries-with-PredicateBuilder.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!