Using Sitecore 10.2
with Solr 8.4.0
.
I have Product
pages which inherit from a template - Product Details
.
This template has the following fields:
Title - Rich Text
Description - Rich Text
Category - Single Line
Tags - Treelist
I would like to perform a search for a term that can exist in Title
or Description
or Category
.
In this Article which provides the basic steps to get started with Search, I see that it uses the Content
field to search for the term.
But in my code, the Content
property is null.
What should be configured & where, so that the Content
property will include all text from Title
& Description
& Category
I have created a custom index and here is how the results are fetched.
public List<T> GetSearchResults<T>(string searchIndexName, string templateName, string searchTerm) where T : SearchResultItem
{
///get the index
ISearchIndex searchIndex = ContentSearchManager.GetIndex(searchIndexName);
///create a search context
using (IProviderSearchContext context = searchIndex.CreateSearchContext())
{
try
{
///build the query
var query = PredicateBuilder.True<T>();
if (!string.IsNullOrWhiteSpace(templateName))
{
query = query.And(x => x.TemplateName.Equals(templateName, StringComparison.InvariantCultureIgnoreCase));
}
if (!string.IsNullOrWhiteSpace(searchTerm))
{
query = query.And(x => x.Content.Contains(searchTerm));
}
/// get results from this index based on query
IQueryable<T> results = context.GetQueryable<T>().Where(query);
return results != null ? results.ToList() : null;
}
catch (Exception)
{
// ignored
}
return null;
}
}
custom index:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore role:require="Standalone or ContentDelivery or ContentManagement" search:require="solr">
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="custom_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="name">$(id)</param>
<param desc="core">custom_index</param>
<param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
<configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration" />
<strategies hint="list:AddStrategy">
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual" role:require="(ContentManagement and !Indexing) or (ContentDelivery and !Indexing)" />
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance" role:require="Standalone or (ContentManagement and Indexing) or (ContentDelivery and Indexing)" />
</strategies>
<locations hint="list:AddCrawler">
<crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
<Database>master</Database>
<Root>/sitecore/content/tenant/stratum/home/products</Root>
</crawler>
</locations>
<enableItemLanguageFallback>false</enableItemLanguageFallback>
<enableFieldLanguageFallback>false</enableFieldLanguageFallback>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>