DFS 11.1 - Custom content search index
Out of the box the assets synchronized into Sitecore as items, goes into the default index sitecore_master_index
and sitecore_web_index
. In some cases it make sense to create a custom search index for all the digizuite items. For instance if the users can search for dynamic digizuite content on the website. In this guide we will show an example of an custom search index which will index all digizuite items. We will also control which fields that goes into the index. In this example we use Sitecore 10.2 and Solr v.8.8.2. This guide assume that you know how to install solr and create cores.
Sitecore configuration
Here we create the configuration for the custom index dfs_contentsearch_master_index
. In this example we only create an index for the database Master and limit it to only add the items from this path /sitecore/media library/Digizuite
. Remember to create the core for the new index in solr.
DFS.ContentSearch.Solr.Index.Master.config
<?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 ContentManagement or Processing" search:require="solr">
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="dfs_contentsearch_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="name">$(id)</param>
<param desc="core">$(id)</param>
<!-- This initializes index property store. Id has to be set to the index id -->
<param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
<configuration ref="contentSearch/indexConfigurations/DFS.ContentSearch.Solr.IndexConfiguration" />
<strategies hint="list:AddStrategy">
<!--NOTE: order of these is controls the execution order-->
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/syncMaster" />
</strategies>
<locations hint="list:AddCrawler">
<crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
<Database>master</Database>
<Root>/sitecore/media library/Digizuite</Root>
</crawler>
</locations>
<enableItemLanguageFallback>false</enableItemLanguageFallback>
<enableFieldLanguageFallback>false</enableFieldLanguageFallback>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>
DFS.ContentSearch.Solr.IndexConfiguration.config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore search:require="solr">
<contentSearch>
<indexConfigurations>
<!-- If no configuration is specified for an index, it uses the default configuration. The configurations are not merged if the index also has a
configuration. The system uses either the default configuration or the index configuration. -->
<DFS.ContentSearch.Solr.IndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
<documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
<indexAllFields>true</indexAllFields>
<include hint="list:AddIncludedTemplate">
<DFSRootTemplateId>{316978B1-1C41-4F4F-82D5-82863665EDE6}</DFSRootTemplateId>
<AssetsSiloTemplateId>{4133449F-6EC1-4D10-A930-3EC828650FA6}</AssetsSiloTemplateId>
<AssetTemplateId>{2D63B0B3-89AA-4A87-9A32-D822C71B5BD8}</AssetTemplateId>
<MappedEntityTemplateId>{B8879FB6-ADE9-4971-B580-CF0073FC182A}</MappedEntityTemplateId>
<DigizuiteEntityTemplateId>{801EC0D4-6D99-4609-B042-96388D314BFD}</DigizuiteEntityTemplateId>
</include>
<fields hint="raw:AddComputedIndexField">
<field fieldName="mfTitle" metafieldGuid="5eb3eefc-a043-410f-89b0-29ed3ef37078" returnType="string">DFS.Client.Services.ContentSearch.ComputedFields.DynamicMetadata, DFS.Client.Services</field>
<field fieldName="mfPublishTo" metafieldGuid="74a5a102-a310-4bb7-9e84-0b14c36436b2" returnType="stringCollection">DFS.Client.Services.ContentSearch.ComputedFields.DynamicMetadata, DFS.Client.Services</field>
<field fieldName="thumbHtmlTag" returnType="string">DFS.Client.Services.ContentSearch.ComputedFields.ThumbHtmlTag, DFS.Client.Services</field>
<field fieldName="derivedFrom" returnType="string">DFS.Client.Services.ContentSearch.ComputedFields.DerivedFrom, DFS.Client.Services</field>
</fields>
</documentOptions>
</DFS.ContentSearch.Solr.IndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
As you can see we added AddComputedIndexField
to show how to add custom fields to the index, that runs c# code to generate the values. Lets focus on the computedField DynamicMetadata
. With this one we can extract value for a specific metafield. Which metafield, is defined by the attribute metafieldGuid
.
Her is an example of the data in solr:
When adding a custom search index and be able to do dynamic search from the website. This requires that all the assets are synchronized into Sitecore. You can read about how to do a full synchronization here: DFS 11.1 - Synchronization.
If you want to trigger automatically rebuild of the new index, when the synchronization is done, then look at the pipeline DFS.BatchSync
in App_Config\Include\damforsitecore\DFS.Pipelines.config. Change the indexName to your new index.
Example of searching
Here is an example of how to search the index.
Give me all entries where assetId is not empty, no crop assets, only assets of assettype 4 (images) and last we freetext search the title.
var index = ContentSearchManager.GetIndex("dfs_contentsearch_master_index");
using (var searchContext = index.CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
{
var queryable = searchContext.GetQueryable<AssetResultItem>().Where(q => q.AssetId != string.Empty);
queryable = queryable.Where(q => q.Derivedfrom == "0");
queryable = queryable.Where(q => q.Assettype == "4");
queryable = queryable.Where(q => q.Title.Contains(freeText));
var res = queryable.GetResults();
foreach (var searchResult in res)
{
renderList.assets.Add(new asset()
{
htmlTag = searchResult.Document.Thumbhtmltag,
title = searchResult.Document.Title
});
}
}
Example of a website using the search.
You can download all the configuration and c# files here: