2

I am trying to create a commuted index field in sitecore 9 but can't get the configuration to work. I have a local environment which is running Solr and also environment is Azure.

I have added the following configuration to my indexing patch config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration>
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="icon_services" returnType="string">Sitecore.Foundation.Indexing.Infrastructure.Fields.ImageURLComputedField, Sitecore.Foundation.Indexing</field>
          </fields>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

This configuration throws the following error:

Server Error in '/' Application. Could not find add method: AddComputedIndexField (type: Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Sitecore.Exceptions.RequiredObjectIsNullException: Could not find add method: AddComputedIndexField (type: Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration)

My code for computed index:

public class ImageURLComputedField : IComputedIndexField
  {
    public string FieldName { get; set; }
    public string ReturnType { get; set; }

    public object ComputeFieldValue(IIndexable indexable)
    {
      Assert.ArgumentNotNull(indexable, "indexable");
      string url = null;
      try
      {
        Item item = indexable as SitecoreIndexableItem;

        // This field only works for items uder /sitecore/content that have a layout
        if (item == null
            || item.Visualization.Layout == null
            || !item.Paths.FullPath.StartsWith(
                    Sitecore.Constants.ContentPath,
                    System.StringComparison.InvariantCultureIgnoreCase))
        {
          return null;
        }
        ImageField img = item.Fields["Icon"];

        return img == null || img.MediaItem == null ? null : MediaManager.GetMediaUrl(img.MediaItem);
      }
      catch (WebException webExc)
      {
        Log.Warn(string.Format("Failed to html index {0} ({1}): {2}", indexable.Id, url, webExc.Message), webExc, this);
      }
      catch (Exception exc)
      {
        Log.Error(string.Format("An error occurred when indexing {0}: {1}", indexable.Id, exc.Message), exc, this);
      }
      return null;
    }
  }
5

In Sitecore 8.2 there was one additional xml tag in the config, documentOptions. I don't have Sitecore 9 instance running at the moment, but I expect it's the same:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration>
          <documentOptions> <!-- MISSING -->
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="icon_services" returnType="string">Sitecore.Foundation.Indexing.Infrastructure.Fields.ImageURLComputedField, Sitecore.Foundation.Indexing</field>
            </fields>
          </documentOptions> <!-- MISSING -->
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>
0

I added the following patch file for Azure search to work

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultCloudIndexConfiguration>
          <documentOptions>
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="icon_services" returnType="string">Sitecore.Foundation.Indexing.Infrastructure.Fields.ImageURLComputedField, Sitecore.Foundation.Indexing</field>
            </fields>
          </documentOptions>
        </defaultCloudIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>
  • 1
    Arun, your answer looks exactly the same as the accepted answer. Is there any reason you decided to post it as a separate answer? – Dmytro Shevchenko Feb 12 '18 at 7:14
0

I needed to add the following configuration to make custom field shown in the solr Index.I am trying to add it to a custom index(not default indexes).Adding FieldMap tag will map the field and index it.

 <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <include hint="list:AddIncludedTemplate">
              <CustomDetails>{B713DC5E-1A10-40D5-A9C0-3DFF1431D05B}</CustomDetails>
            </include>
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="FacetCategory" returnType="string">Foundation.Location.Repository.FacetsComputedField, Foundation.Location</field>
            </fields>
          </documentOptions>
         <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">
           <fieldNames hint="raw:AddFieldByFieldName">
                        <fieldType fieldName="FacetCategory" returnType="string" />
          </fieldNames>
        </fieldMap>

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.