0

I've made a computed index field in my solution. The class returns List<string> from the ComputeFieldValue method and the config patch includes the field within the fieldMap and documentOptions sections of the defaultSolrIndexConfiguration.

I'm following the answer provided here: Solr tokenization to split on semicolon character

My indexer class is as follows:

public class SemicolonDelimitedField : AbstractComputedIndexField
{
 public override object ComputeFieldValue(IIndexable indexable)
 {
  Item item = indexable as SitecoreIndexableItem;
  if (item == null)
  {
    return null;
  }

  if (item.TemplateID != Templates.Category.ID
      && item.TemplateID != Templates.CommerceProduct.ID)
  {
    return null;
  }

  var fieldValue = item[FieldName];
  var values = fieldValue?.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries);
  if (values == null || values.Any() == false)
  {
    return null;
  }

  var returnValue = values.Select(v => v.Trim()).ToList();
  return returnValue;
 }
}

And the config patch is as follows

<?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 search:require="solr">
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider" >
          <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
            <fieldNames hint="raw:AddFieldByFieldName">
              <field fieldName="myField" returnType="stringCollection" />
            </fieldNames>
          </fieldMap>

          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="myField" storageType="NO" indexType="UNTOKENIZED">MyNamespace.SemicolonDelimitedField, MyAssembly</field>
            </fields>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

What I am seeing in my Solr index is that the computed field does not seem to be breaking up into separate entities in the array e.g.

"myField_sm":["Low level;High level"],

what I was expecting was

"myField_sm":["Low level", "High level"],

I attached the debugger to be 100% sure that a List<string> with two elements was really being returned. What part of the puzzle am I missing?

  • 1
    It doesn't look for me like indexing issue. It looks rather like the field is indexed directly from field value rather than from your custom code. Can you try use completely new name for your custom field in the config and see if that helps? – Marek Musielak Oct 4 '20 at 14:59
  • @MarekMusielak that was indeed the issue. If you want to post that suggestion as an answer, I'll be happy to accept it. – Matthew Dresser Oct 5 '20 at 11:39
1

You wrote in your question that the value which is indexed is

"myField_sm":["Low level;High level"],

It doesn't look like indexing issue. I've never seen Sitecore or Solr adding ";" in document fields.

This looks rather like the field is indexed directly from item field value rather than from your custom code.

Try to use completely new name for your custom field in the config and see if that 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.