自定义Sitecore Solr索引计算字段返回错误的值

时间:2016-10-31 20:15:50

标签: indexing solr sitecore

我有一个名为Terms的计算字段。此字段读取Sitecore将多列表字段存储为guid值的列表,并将其作为逗号分隔的值字符串。当我触发索引重建并设置断点时,我可以看到代码按预期执行任务,但由于某种原因,在索引中,值仍然存储为guid字符串数组。我不知道断开连接的位置。

检查Solr并将查询配置为返回xml,我可以看到条件字段的存储方式如下:

<arr name="terms_t">
  <str>b7ba58ef002b4554808a1d423ca574d8</str>
  <str>95680bf346d142aeb0d8f189300ea3f2</str>
</arr>

下面您可以看到用于填充计算字段的类

namespace ASHPEngine.ComputedFields
{
    public class Terms : Sitecore.ContentSearch.ComputedFields.IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            Assert.ArgumentNotNull(indexable, "indexable");
            var scIndexable = indexable as Sitecore.ContentSearch.SitecoreIndexableItem;
            if (scIndexable == null)
            {
                return false;
            }
            if (String.Compare(scIndexable.Item.Database.Name, "core", System.StringComparison.OrdinalIgnoreCase) == 0)
            {
            return false;
        }
        if (!scIndexable.Item.InheritsFrom(IBaseArticleConstants.TemplateName)) return String.Empty;
        var termsField = scIndexable.GetFieldByName(INewsConstants.TermsFieldName);
        {
            var retval = new StringBuilder();
            var terms = termsField.Value.ToString().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var id in terms)
            {
                var termItem = scIndexable.Item.Database.GetItem(new ID(id));
                retval.Append(termItem.Name + ", ");
            }

            return retval.Length > 0 ? retval.Remove(retval.Length-2, 2).ToString() : string.Empty;
        }
    }

    public string FieldName { get; set; }
    public string ReturnType { get; set; }
}

配置:

<configuration>
 <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">

    <indexes hint="list:AddIndex">
      <index id="news" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
        <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/defaultSolrIndexConfiguration">


          <fieldMap ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/fieldMap">

            <fieldNames hint="raw:AddFieldByFieldName">
              <field fieldName="_uniqueid" returnType="string" />
              <field fieldName="article" returnType="string" />
              <field fieldName="author" returnType="string"/>
              <field fieldName="author credentials" returnType="string" />
              <field fieldName="date" returnType="datetime" format="yyyy-MM-dd'T'HH:mm:ss'Z'" />
              <field fieldName="degree" returnType="string" />
              <field fieldName="graduation year" returnType="string" />
              <field fieldName="location" returnType="string" />
              <field fieldName="navigation title" returnType="string" />
              <field fieldName="page abstract" returnType="string" />
              <field fieldName="school" returnType="string" />
              <field fieldName="text" returnType="string" />
              <field fieldName="value" returnType="string" />
            </fieldNames>
          </fieldMap>
          <documentOptions ref="contentSearch/indexConfigurations/defaultSolrIndexConfiguration/documentOptions">

            <fields hint="raw:AddComputedIndexField">
              <field fieldName="external_link" returnType="text">mydll.ComputedFields.ExternalLink, mydll</field>
              <field fieldName="image" returnType="text">mydll.ComputedFields.Image, mydll</field>
              <field fieldName="terms" returnType="text">mydll.ComputedFields.Terms, mydll</field>
              <field fieldName="url" returnType="text">mydll.ComputedFields.Url, mydll</field>
              <field fieldName="parentname" returnType="text">mydll.ComputedFields.ParentName, mydll</field>
            </fields>
          </documentOptions>

        </configuration>

        <strategies hint="list:AddStrategy">
          <!-- NOTE: order of these is controls the execution order -->
          <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync" />
        </strategies>

        <locations hint="list:AddCrawler">
          <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
            <Database>web</Database>
            <Root>/sitecore/content</Root>
          </crawler>
        </locations>

        <enableItemLanguageFallback>false</enableItemLanguageFallback>
        <enableFieldLanguageFallback>false</enableFieldLanguageFallback>

      </index>
    </indexes>
  </configuration>
  </contentSearch>
  </sitecore>
</configuration>

2 个答案:

答案 0 :(得分:2)

我能够通过完全删除返回类型来解决这个问题:

<field fieldName="terms">mydll.ComputedFields.Terms, mydll</field>

答案 1 :(得分:1)

尝试将计算字段的返回类型更改为字符串而不是文本:

<field fieldName="terms" returnType="string">mydll.ComputedFields.Terms, mydll</field>