Friday 30 November 2018

Create custom Solr index configuration file


Hello Devs,

If you want to create a custom Solr index file in your solution and having trouble in doing so (we too had trouble), you are at right place.

Earlier we used custom Lucene indexes in our solution but due to performance issues we decided to move all the custom indexes to SOLR (Read more about using Solr or Lucene).

If you are moving your custom Lucene indexes to Solr you would need to change few configurations to make it working.

Below I have created a custom Solr index file (e.g. employeecustomindex)

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="employeecustomindex" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
            <param desc="name">$(id)</param>
            <param desc="folder">$(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/employeeCustomIndexConfiguration" />

            <strategies hint="list:AddStrategy">
              <!-- NOTE: order of these controls the execution order -->
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/rebuildAfterFullPublish" />
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync" />
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/remoteRebuild" />
            </strategies>

            <commitPolicyExecutor type="Sitecore.ContentSearch.CommitPolicyExecutor, Sitecore.ContentSearch">
              <policies hint="list:AddCommitPolicy">
                <policy type="Sitecore.ContentSearch.TimeIntervalCommitPolicy, Sitecore.ContentSearch" />
              </policies>
            </commitPolicyExecutor>

            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>web</Database>
                <Root>/sitecore/content/Home/Employee/</Root>
              </crawler>
            </locations>

          </index>
        </indexes>
      </configuration>

      <indexConfigurations>
        <employeeCustomIndexConfiguration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
          <indexAllFields>false</indexAllFields>

          <include hint="list:IncludeTemplate">
            <Employee>{AB47C5E1-000E-4DF1-B74F-0FC226FB19BB}</Employee>
          </include>

          <include hint="list:IncludeField">
            <EmployeeName>{6244D370-D460-41B6-A6D1-34040C828523}</EmployeeName>
            <EmployeeCity>{8B8BDA9E-C728-44D3-8859-204448812B3F}</EmployeeCity>
            <DoNotDisplay>{6F2DAA38-8467-43F1-85DD-5B5A14BB1719}</DoNotDisplay>
          </include>

          <exclude hint="list:ExcludeField">
            <EmployeeDOB>{E16B5AEE-3E7A-4CEB-A743-9CEB1CCE636F}</EmployeeDOB>
            <EmployeeBio>{6B50DE85-DCC3-4592-9746-6170E1657321}</EmployeeBio>
          </exclude>

          <fieldReaders ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldReaders"/>
          <indexFieldStorageValueFormatter ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/indexFieldStorageValueFormatter"/>
          <indexDocumentPropertyMapper ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/indexDocumentPropertyMapper"/>
        </employeeCustomIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>


     1. First, we need to change the reference to (e.g.    contentSearch/indexConfigurations/employeeCustomIndexConfiguration).

  <configuration ref="contentSearch/indexConfigurations/employeeCustomIndexConfiguration" />

2. If we want to index only specific fields, set the indexAllFields property to false.

  <indexAllFields>false</indexAllFields>

3.  Set templates which we want to index in below configuration section.
  
   <include hint="list:IncludeTemplate">
       <Employee>{AB47C5E1-000E-4DF1-B74F-0FC226FB19BB}</Employee>
  </include>

4.  Set fields we want to add in index in our case we have 
    (Employee Name,Employee City,EmployeeDOB,EmployeeBio,DoNotDisplay) in Employee template

 <include hint="list:IncludeField">
   <EmployeeName>{6244D370-D460-41B6-A6D1-34040C828523}</EmployeeName>
   <EmployeeCity>{8B8BDA9E-C728-44D3-8859-204448812B3F}</EmployeeCity>
   <DoNotDisplay>{6F2DAA38-8467-43F1-85DD-5B5A14BB1719}</DoNotDisplay>
 </include>

   Set fields we don't want to add in index.

 <exclude hint="list:ExcludeField">
  <EmployeeDOB>{E16B5AEE-3E7A-4CEB-A743-9CEB1CCE636F}</EmployeeDOB>
  <EmployeeBio>{6B50DE85-DCC3-4592-9746-6170E1657321}</EmployeeBio>
 </exclude>

 5.  At last we need to add references for

    <fieldReaders   ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldReaders"/>

    <indexFieldStorageValueFormatter    ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/indexFieldStorageValueFormatter"/>

    <indexDocumentPropertyMapper ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/indexDocumentPropertyMapper"/>  

which references to defaultSolrIndexConfiguration file.

After making all the changes restart your Solr service, re-build index and preview your index in Solr admin panel.

Happy Solr Indexing!!! 😊