0

We want to build a search interface that pulls results both from media library and different content paths, to include both pdf and standard content items. What is the best way to structure the predicate statements? Our current code looks like this:

var predicate = PredicateBuilder.True<SearchResultItem>();
predicate = predicate.And(i => i.Paths.Contains(Paths.PdfsFolderID));
predicate = predicate.And(i => i.Content.Contains(Key));
predicate = predicate.And(i => i.TemplateName.Equals("pdf"));
IQueryable<SearchResultItem> queryable = context.GetQueryable<SearchResultItem>().Where(predicate);
                queryable.Count();

This brings in the media library pdf files that we want included. How do we go about adding in the content items? We have tried predicate.or to include alternate paths and templates, but so far, no luck. In case it is pertinent, we use Lucene, and are on on SC 7.2.

Thanks in advance!

  • How about creating two search predicates with two search results and then merging them with Union oe something? – Peter Procházka Jun 2 '18 at 17:10
  • 1
    Unnecessary to perform the search twice. You can combine multiple predicates however. Add a new predicate with a OR operator on the item path. – jrap Jun 2 '18 at 20:41
1

There is alot of good blog posts about this:

E.g. https://www.geekhive.com/buzz/post/2017/08/predicate-builder-advanced-search-queries/

And as jrap commented, you should just combined multiple predicates.

So Something like:

 var templateQuery = PredicateBuilder.False<ContentSearchResultItem>();
   templateQuery= templateQuery.Or(item => item.TemplateName.Equals("pdf"));
   templateQuery= templateQuery.Or(item = item.TemplateName.Equals("content item template"));

var pathQuery= PredicateBuilder.False<ContentSearchResultItem>();
   pathQuery= templateQuery.Or(item => i.Paths.Contains(Paths.PdfsFolderID));
   pathQuery= templateQuery.Or(item = i.Paths.Contains(Paths.ContentFolderID));

var predicate = PredicateBuilder.True<SearchResultItem>();
predicate = predicate.And(pathQuery);
predicate = predicate.And(templateQuery);
predicate = predicate.And(i => i.Content.Contains(Key));

Usually i use false, when creating OR statements because that makes most sense in my head (false OR predicateA OR predicateB), and true, when creating AND statements (true AND predicateA AND predicateB) Im not 100% sure on how sitecore translates this into queries, but i think its ignored.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.