7

I'm currently working in Sitecore 8.1. I would like to add a custom computed field to a specific index, and only that index. I have been able to add the field globally to Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config, but I had to reference a dll that isn't specifically meant to be applied to all indexes.

I have the following index:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="custom_master_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
            <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/defaultLuceneIndexConfiguration" />
            <strategies hint="list:AddStrategy">
              <!-- NOTE: order of these is controls the execution order -->
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/syncMaster" />
            </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>master</Database>
                <Root>/sitecore/content/Home</Root>
              </crawler>
            </locations>
            <enableItemLanguageFallback>false</enableItemLanguageFallback>
            <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
          </index>
        </indexes>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

I have tried adding

<fields hint="raw:AddComputedIndexField">
    <field fieldName="SearchDate">Project.Web.Search.ComputedFields.SearchDateField, Project.Web</field>
</fields>

to the <configuration> element, but that didn't work. I also tried adding the above to a few other places under this index, but I haven't been able to get it to work anywhere.

Is it possible to do what I'm looking to do? If so, how?

6

In the section of your index (<configuration ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration" />) you should point towards a custom config instead of the default one. You need to create this config though, and there you can add the computed field.

Here's a blog post describing this: http://ggullentops.blogspot.be/2015/11/sitecore-indexes.html. Depending on your Sitecore version, there might be some changes needed. It comes down to creating your own version of the default so you can take as guidance.

After adding the computedField in a section <fields hint="raw:AddComputedIndexField">.. </fields>, you might need to add it to the fieldMap as well. That is if you need to get your field untokenized or want to define an analyzer on it.

6

First, you need to define your own index configuration. That's where you can define the fields that will only be used by your custom indexes.

Create a file named e.g. Sitecore.ContentSearch.Lucene.Index.CustomIndexConfiguration.config under /App_Config/Include with the following content:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <customIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
          <indexAllFields>true</indexAllFields>

          <initializeOnAdd>true</initializeOnAdd>

          <analyzer ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/analyzer" />

          <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
            <fieldNames hint="raw:AddFieldByFieldName">
              <!-- Here you configure the way your computed field will be stored. -->
              <field fieldName="SearchDate"
                     storageType="YES"
                     indexType="TOKENIZED"
                     vectorType="NO"
                     boost="1f"
                     type="System.String"
                     settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
              </field>
            </fieldNames>
          </fieldMap>

          <fields hint="raw:AddComputedIndexField">
              <!-- Here you register the computed field. -->
              <field fieldName="SearchDate">Project.Web.Search.ComputedFields.SearchDateField, Project.Web</field>
          </fields>

          <include hint="list:IncludeTemplate">
          </include>

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

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

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

          <documentBuilderType>Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder, Sitecore.ContentSearch.LuceneProvider</documentBuilderType>
        </customIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

Note that I included your field SearchDate in two places: under <fields> and under <fieldMap><fieldNames>. This will make sure that your custom field settings (such as tokenization) are applied.

Then in your index definition, you should reference your custom index configuration instead of the default one:

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

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.