I am using content search API in Sitecore 9.1.1. However, it is not fetching result if i am using search with any of the template field. The below code fetching result based on template id but when we are trying to fetch result based on any template field then it is not showing any result.

Sharing Code:-

Template :-

ID         Single-Line Text
Name       Single-Line Text
Version    Number

Model :-

public class ModelSearchResult : SearchResultItem
            public string ID { get; set; }
            public string Name { get; set; }
            public int Version { get; set; }

Code :

ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index");

                using (IProviderSearchContext context = index.CreateSearchContext())

                    var predicate = PredicateBuilder.True<ModelSearchResult>();
                    predicate = predicate.And(x => x.TemplateId == ID.Parse("{----}"));
                    var keywordPredicate = PredicateBuilder.False<ModelSearchResult>();
                    keywordPredicate = keywordPredicate.Or(x => x.ID == ID.Parse(item.ID));
                    keywordPredicate = keywordPredicate.Or(x => x.Version == item.Version.Number);
                    predicate = predicate.And(keywordPredicate);
                    var queryable = context.GetQueryable<ModelSearchResult>();
                    queryable = queryable.Where(predicate);
                    var result = queryable.GetResults();
  • Hi Sonu, looks like in 2nd predicate you are using different class - var keywordPredicate = PredicateBuilder.False<DataSourceSearchResult>(); so the name and version field that you are using, those are OOTB in searchResultItem class Jul 13, 2020 at 8:59
  • I have updated the code. I am using the same class.
    – Ashish
    Jul 13, 2020 at 10:06
  • Can you please check with some other field name as well, because above fields seem OOTB as well, take another field like title and check with that Jul 13, 2020 at 10:20
  • I have check with all the fields but it is not working. Is there any setting for the field index?
    – Ashish
    Jul 13, 2020 at 10:23
  • can you see solr dashboard and try to run your search query there and see if you are getting result there? Jul 13, 2020 at 10:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.