3

I'm using Sitecore 10.2.

I want to define a custom computed field but I need it to be added to a specific index only.

I was able to add the computed field globally as below, but I need to specify it for one index only in SOLR, is there a way to do so?

<defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider" >
      <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
        <fields hint="raw:AddComputedIndexField">
          <field fieldName="mycustomfield" returnType="textCollection">Project.Web.Search.ComputedFields.MyCustomField, Project.Web</field>
        </fields>
      </documentOptions>
    </defaultSolrIndexConfiguration>

3 Answers 3

3

The defaultSolrIndexConfiguration node is located under: .\App_Config\Sitecore\ContentSearch\Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config

You could create a custom index configuration node that is referenced by your custom index.

Warning: Don't do this for any of the out-of-the-box indexes since Sitecore relies on those index configurations to run properly.

Example

If I were to create a custom index for blog articles named sitecore_blog_master_index, my index configuration would look something like this (pay attention to the configuration/sitecore/contentSearch/configuration/indexes/index/configuration node):

<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" search:require="solr">
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="sitecore_blog_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
            <param desc="name">$(id)</param>
            <param desc="core">$(id)</param>
            <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
            <configuration ref="contentSearch/indexConfigurations/blogSolrIndexConfiguration">
              <enableReadAccessIndexing>true</enableReadAccessIndexing>
            </configuration>
            <strategies hint="list:AddStrategy">
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual" role:require="ContentManagement and !Indexing" />
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/intervalAsyncMaster" role:require="Standalone or (ContentManagement and Indexing)" />
            </strategies>
            <locations hint="list:AddCrawler">
              <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
                <Database>master</Database>
                <Root>/sitecore</Root>
              </crawler>
            </locations>
            <enableItemLanguageFallback>false</enableItemLanguageFallback>
            <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

You'd need to make sure to define your blogSolrIndexConfiguration element. You've got two options for this.

Option 1

The easiest thing to do is copy the defaultSolrIndexConfiguration element that I referenced above into your custom index config file, changing the element name to blogSolrIndexConfiguration. This results in a lot of duplicated configuration that makes your index harder to support in the future.

Option 2

Another solution would be to use the ref attribute to reference the subelements from the defaultSolrIndexConfiguration in your blogSolrIndexConfiguration. It would look something like this:

<sc.variable name="defaultSolrIndexConfiguration" 
             value="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/" />

<blogSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
    <initializeOnAdd ref="$(defaultSolrIndexConfiguration)initializeOnAdd" />
    <fieldMap ref="$(defaultSolrIndexConfiguration)fieldMap"/>
    <mediaIndexing ref="$(defaultSolrIndexConfiguration)mediaIndexing"/>
    <virtualFields ref="$(defaultSolrIndexConfiguration)virtualFields"/>
    <fieldReaders ref="$(defaultSolrIndexConfiguration)fieldReaders"/>
    <indexFieldStorageValueFormatter ref="$(defaultSolrIndexConfiguration)indexFieldStorageValueFormatter"/>
    <indexDocumentPropertyMapper ref="$(defaultSolrIndexConfiguration)indexDocumentPropertyMapper"/>
    <documentBuilderType ref="$(defaultSolrIndexConfiguration)documentBuilderType"/>
    <defaultSearchSecurityOption ref="contentSearch/indexConfigurations/defaultSearchSecurityOption" />
    <enableReadAccessIndexing ref="contentSearch/indexConfigurations/enableReadAccessIndexing" />
    <searchOptions ref="$(defaultSolrIndexConfiguration)searchOptions"/>
    <highlightOptions ref="$(defaultSolrIndexConfiguration)highlightOptions"/>

    <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
        {{Custom Configuration Goes Here}}
    </documentOptions>
</blogSolrIndexConfiguration>

This minimizes the duplication of configuration, allowing you to define specifically what's custom to your solution.

2
  • thank you so much, that worked for me, the computed field is added to my custom index only now. But i noticed that the sxacontent field has disappeared from the indexed items in SOLR, do you have any idea why did it disappear?
    – AME
    Aug 22, 2022 at 11:28
  • 1
    The SXA content is defined in a separate patch file. If you add a ref to the default documentOptions or the entire defaultSolrIndexConfiguration as Sumit referenced below, you’ll get everything that’s patched by other files. Aug 24, 2022 at 12:16
2

Use this simple configuration and extend as per your needs.

<indexes hint="list:AddIndex">
    <index id="your_index_name" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
        <param desc="name">$(id)</param>
        <param desc="folder">$(id)</param>
        <configuration ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration">
            <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">
                <fields hint="raw:AddComputedIndexField">
                    <!-- Your Computed Index -->
                    <field fieldName="external_link" returnType="text">mydll.ComputedFields.ExternalLink, mydll</field>
                </fields>
            </documentOptions>
        </configuration>
      <enableItemLanguageFallback>false</enableItemLanguageFallback>
      <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
    </index>
</indexes>

Hope this will help.

1
  • I like Sumit’s simple solution. Aug 21, 2022 at 22:21
0

Assume you want to create a new separate index file for the Course then first need to develop a new index file, you can then make a replica of existing one: Sitecore.Foundation.CoursesMaster.Indexing.config

In the configuration section provide the new name:

<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
    <indexes hint="list:AddIndex">
      <!--index name-->
      <index id="course_custom_master_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
        <param desc="name">$(id)</param>
        <!--index name-->
        <param desc="core">course_custom_master_index</param>
        <!--other parameters-->
        ...
        <!--new custom configuration name-->
        <configuration ref="contentSearch/indexConfigurations/courseSolrIndexConfigurationMaster" />
        <strategies hint="list:AddStrategy">
          <strategy..../>
          <strategy .../>
        </strategies>
        <locations hint="list:AddCrawler">
          ....
        </locations>
      </index>
    </indexes>
  </configuration>

Here is your Id of the index 'course_custom_master_index' and instead of defaultSolrIndexConfiguration new indexConfigurations is courseSolrIndexConfigurationMaster.

Now your custom indexconfiguration will be:

<!--your custom index configuration-->
<courseSolrIndexConfigurationMaster type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider" >
  <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
    <fields hint="raw:AddComputedIndexField">
      <!--custom field-->
      <field fieldName="courseCustomfield" returnType="textCollection">Project.Web.Search.ComputedFields.MyCustomField, Project.Web</field>
    </fields>
  </documentOptions>
</courseSolrIndexConfigurationMaster>

Rest, you can define your custom field and the implementation same. I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.