Birden çok koşul içeren Search API

Oct 09 2020

Sitecore 9.3

Şimdiye kadar sahip olduğum şey:

Öğeleri arama bağlamını kullanarak bir şablona göre filtreleyebiliyorum.

var unversionedImageTemplateId = ID.Parse(Constants.UnversionedImageTemplateId);
var jpegTemplateId = ID.Parse(Constants.Jpeg);

var results = context.GetQueryable<SearchResultItem>()
  .Where(x => x.TemplateId == unversionedImageTemplateId || x.TemplateId == jpegTemplateId)

Yapmaya çalıştığım şey:

Bir alanın değerine bağlı olarak bir koşul daha koymak istiyorum, bir onay kutusu.

Ne denedim:

  1. Sahip olduğum .Wherekoddan sonra kullanmayı denedim .
  2. &&Aynı Wherekomutta kullanarak koşulu uygulamayı denedim .

Ama her zaman şu hatayı alıyorum.

Exception: System.NotSupportedException
Message: The method 'get_Item' is not supported.
Source: Sitecore.ContentSearch.Linq

Bununla nasıl ilerleyeceğiniz konusunda bir fikriniz var mı?

Yanıtlar

2 VladIobagiu Oct 08 2020 at 23:45

Sorgunuzu oluşturmak için Predicate oluşturucuyu kullanmalısınız. Şunun gibi bir şeye sahip olacaksınız:

      var predicate = PredicateBuilder.True<SearchResultItem>();
      predicate = predicate.And<SearchResultItem>(i => i.TemplateId == [Template ID]);
     #Get items under specific node or Items from specific location in Sitecore –
     predicate = predicate.And<SearchResultItem>(i => i.Paths.Contains([Location Item ID]));
     #Get items for context language only  –
     predicate = predicate.And<SearchResultItem>(item => item.Language == Context.Language.Name);
     # get items which have a field value
     predicate = predicate.And<SearchResultItem>(item => item["fieldname"] == "Sitecore")