Friday, November 3, 2017

Sitecore 9 SOLR custom index configuration

I always find it handy to have a clean example of a custom index configuration ready to use during project development. Below is an example of a custom SOLR index configuration ready to use with Sitecore 9. As with any index I create, it contains sections for templates/fields to be added to the index along with computed index fields.

The standard database and root for crawling are of course included as well.
<?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="sitecore_test_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
            <param desc="name">$(id)</param>
            <param desc="core">sc9test__test_index</param>
            <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
              <configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
    <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
      <include hint="list:AddIncludedTemplate">
     <Page>{49E6B85F-02F4-405F-923F-66761933E80F}</Page>
      </include>
      <fields hint="raw:AddComputedIndexField">
     <field fieldName="MyComputedField" returnType="string">Sitecore.Base.Search.MyComputedImageField, Sitecore.Base</field>
     <field fieldName="ItemUrl" returnType="string">Sitecore.ContentSearch.ComputedFields.UrlLink,Sitecore.ContentSearch</field>
      </fields>
    </documentOptions>
    <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
      <fieldNames hint="raw:AddFieldByFieldName">
     <field fieldName="title" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
     <field fieldName="description" returnType="text" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
      </fieldNames>
    </fieldMap>
     </configuration>
            <strategies hint="list:AddStrategy">
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsyncSingleInstance"/>
            </strategies>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>web</Database>
                <Root>/sitecore/content/Home</Root>
              </crawler>
            </locations>
            <enableItemLanguageFallback>false</enableItemLanguageFallback>
            <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>
This index is named sitecore_test_index and uses the SOLR core sc9test__test_index, don't forget to change these!

The URL of the item is a computed index field called ItemUrl and actually refers to a Sitecore code definition to get the URL.

No comments:

Post a Comment