1

I think I may be missing an important piece of information, but I am currently trying to get a computed field to show up in my Solr index. I did not have any issues doing this in Sitecore 7.5 with Lucene, so I could just be missing an important part.

I created a new custom solr index configuration that is a near-copy of the default solar index configuration specific to this index.

<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 or ContentDelivery" search:require="solr">
        <contentSearch>
            <!-- Configuration sections for indexes -->
            <indexConfigurations>

                <!-- If an index has no configuration specified, it will use the configuration below. The configuration is not merged if the index also has
             configuration, it is either this configuration or the index configuration. -->
                <customSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
                    <!-- Should index Initialize() method be called as soon as the index is added or wait for an external trigger -->
                    <!-- For Solr Initialize() needs to be called after the IOC container has fired up -->
                    <initializeOnAdd>false</initializeOnAdd>
...

I've created a new index specific to this custom search configuration:

<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="custom_web_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/customSolrIndexConfiguration">
              <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
                <indexAllFields>true</indexAllFields>

                <!-- Included fields -->
                <!--<include hint="list:AddIncludedField">
                  <ProductName>{E676F36E-B0E0-4BE5-998A-329A8F9055FD}</ProductName>
                  <LongDescription>{8A978A2E-0E7A-4415-9163-2F4ECF85A3AB}</LongDescription>
                </include>-->

                <!-- Included templates -->
                <include hint="list:AddIncludedTemplate">

and I've added my computed field type (and removed the previous default computed fields):

...

                       <!-- COMPUTED INDEX FIELDS 
               This setting allows you to add fields to the index that contain values that are computed for the item that is being indexed.
               You can specify the storageType and indextype for each computed index field in the <fieldMap><fieldNames> section.
            -->
                        <fields hint="raw:AddComputedIndexField">
                            <field fieldName="computed_tags" returnType="string" indexType="untokenized" storageType="no">my_namespace, my_dll</field>
                        </fields>
                    </documentOptions>

My computed field code is below:

namespace Namespace.Models.Search
{
    public class ComputedTagsField : AbstractComputedIndexField
    {
        public ComputedTagsField() : base()
        {
            using (var sw = System.IO.File.AppendText(@"C:\Temp\test.txt"))
            {
                sw.WriteLine("Constructor()");
            }
            Log.Info("ComputedTagsField() - Constructor", this);

        }

        ~ComputedTagsField()
        {
            Log.Info("~ComputedTagsField() - Deconstructor", this);

        }

        public override object ComputeFieldValue(IIndexable indexable)
        {
            using (var sw = System.IO.File.AppendText(@"C:\Temp\test.txt"))
            {
                sw.WriteLine("ComputeFieldValue()");
            }
            Log.Info("ComputeFieldValue - start", this);

            Item item = (Item)(indexable as SitecoreIndexableItem);
            try {
                Log.Info("ComputeFieldValue - Item = " + item.Name, this);

                // do some stuff and save it to a StuffIComputed string variable.

                return (object)("[" + StuffIComputed + "]");
            } catch (Exception e) {
                Log.Info("ComputeFieldValue - ComputedTagsField Exception - " + e.Message + System.Environment.NewLine + e.StackTrace, this);

                return (object)"";

            }
        }
    }
}

I have a lot of logging in there mainly because I am trying to diagnose why this function is not being called.

After rebuilding the index manually through the sitecore control panel, I can see that the solr core was modified "less than a minute ago", and my logs say the following:

...

3160 14:48:40 INFO  ComputeFieldValue - Constructor!!

...

8132 14:48:46 INFO  ComputeFieldValue - Deconstructor!!

...

9524 14:49:15 INFO  AUDIT (sitecore\admin): Rebuild indexes from Indexing Manager: custom_web_index
ManagedPoolThread #0 14:49:15 INFO  Job started: Index_Update_IndexName=custom_web_index
ManagedPoolThread #0 14:49:16 INFO  Job ended: Index_Update_IndexName=custom_web_index (units processed: 247)
ManagedPoolThread #0 14:49:41 INFO  Job started: Sitecore.ContentSearch.SolrProvider.Agents.IndexingStateSwitcher
ManagedPoolThread #0 14:49:41 INFO  Job ended: Sitecore.ContentSearch.SolrProvider.Agents.IndexingStateSwitcher (units processed: )

and finally, in my low-effort, text file log (C:\temp\test.txt), I only find:

Constructor()

(there should be atleast one more line from the ComputeFieldValue() method.)

As far as I can tell, no exceptions are thrown.

The field does not exist in solr's queries or schema, and the functions are not being executed. Does anyone know what I missed?

Update - 3/26/2020

I updated my ComputedField code from my previous section, that basically mimic'd my Sitecore 7.5 site to see if that would change anything. Unfortunately nothing changed. I am leaning towards this being a configuration issue somehow. My updated code is below:

public abstract class ComputedField : IComputedIndexField
{
    public abstract object ComputeFieldValue(IIndexable indexable);
    public string FieldName { get; set; }
    public string ReturnType { get; set; }
    private readonly Lazy<Database> _webDatabase = new Lazy<Database>(() => Database.GetDatabase("web"));
    private readonly Lazy<Database> _masterDatabase = new Lazy<Database>(() => Database.GetDatabase("master"));
    protected Database WebDatabase
    {
        get { return _webDatabase.Value; }
    }
    protected Database MasterDatabase
    {
        get { return _masterDatabase.Value; }
    }
}

public class ComputedTagsField : ComputedField
{

    public ComputedTagsField() : base()
    {
        using (var sw = System.IO.File.AppendText(@"C:\Temp\test.txt"))
        {
            sw.WriteLine("Constructor()");
        }
        Log.Info("ComputedTagsField() - Constructor", this);

    }

    ~ComputedTagsField()
    {
        Log.Info("~ComputedTagsField() - Deconstructor", this);

    }

    public override object ComputeFieldValue(IIndexable indexable)
    {
        using (var sw = System.IO.File.AppendText(@"C:\Temp\test.txt"))
        {
            sw.WriteLine("ComputeFieldValue()");
        }
        Log.Info("ComputeFieldValue - start", this);

        Item item = (Item)(indexable as SitecoreIndexableItem);
        try {
            Log.Info("ComputeFieldValue - Item = " + item.Name, this);

            String computedStuff = "";
            // Do some computed stuff and save it to the above variable

            Log.Info("ComputeFieldValue - Completed doing stuff", this);

            return (object)("[" + computedStuff + "]");
        } catch (Exception e) {
            Log.Info("ComputeFieldValue - ComputedTagsField Exception - " + e.Message + System.Environment.NewLine + e.StackTrace, this);

            return (object)"";

        }
    }
}
2

After working through this issue with sitecore support, it was determined that it was the documentOptions line that was the issue:

  <configuration ref="contentSearch/indexConfigurations/customSolrIndexConfiguration">
          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">

Not only do you need to change the configuration ref, but you also need to change the documentOptions type to a ref:

<documentOptions ref="contentSearch/indexConfigurations/customSolrIndexConfiguration/documentOptions">

to match the custom configuration file/documentOptions tag that was created.

| improve this answer | |

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.