Sitecore 自定义索引配置语言

Sitecore custom index configure languages

我在 Sitecore 8.1 环境中创建了一个自定义 Lucene 索引,如下所示:

<?xml version="1.0"?>
<configuration xmlns:x="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
        <indexes hint="list:AddIndex">
          <index id="Products_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider">
            <param desc="name">$(id)</param>
            <param desc="folder">$(id)</param>
            <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
            <configuration ref="contentSearch/indexConfigurations/ProductIndexConfiguration" />
            <strategies hint="list:AddStrategy">
              <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync" />
            </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/General/Product Repository</Root>
              </crawler>
            </locations>
            <enableItemLanguageFallback>false</enableItemLanguageFallback>
            <enableFieldLanguageFallback>false</enableFieldLanguageFallback>
          </index>
        </indexes>
      </configuration>

      <indexConfigurations>
        <ProductIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
          <initializeOnAdd>true</initializeOnAdd>
          <analyzer ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/analyzer" />
          <documentBuilderType>Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilder, Sitecore.ContentSearch.LuceneProvider</documentBuilderType>
          <fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
            <fieldNames hint="raw:AddFieldByFieldName">
              <field fieldName="_uniqueid" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
                <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
              </field>
              <field fieldName="key" storageType="YES" indexType="UNTOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider"/>
            </fieldNames>
          </fieldMap>
          <documentOptions type="Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilderOptions, Sitecore.ContentSearch.LuceneProvider">
            <indexAllFields>true</indexAllFields>
            <include hint="list:AddIncludedTemplate">
              <Product>{843B9598-318D-4AFA-B8C8-07E3DF5C6738}</Product>
            </include>
          </documentOptions>
          <fieldReaders ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/fieldReaders"/>
          <indexFieldStorageValueFormatter ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/indexFieldStorageValueFormatter"/>
          <indexDocumentPropertyMapper ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/indexDocumentPropertyMapper"/>
        </ProductIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

实际上是一个指向根的非常简单的索引,包括一个模板并存储一个字段。一切正常。但我需要一些额外的东西:

我知道这也可以在查询中轻松触发,但我想让我的索引更小,以便它们(重新)构建得更快。

第一个可以通过索引 web 而不是 master 来解决,但在这种情况下我实际上也想要未发布的。我很确定我需要为此定制一个爬虫。

第二个(限制语言)实际上更重要,因为我在其他索引中也需要它。简单的答案可能也是使用自定义爬虫,但我希望有一种方法可以在没有自定义代码的情况下进行配置。是否可以将自定义索引配置为仅包含一组已定义的语言(一种或多种)而不是全部?

您可以轻松创建自定义处理器,让您过滤所需的内容:

 namespace Sitecore.Custom
 {
    public class CustomInboundIndexFilter : InboundIndexFilterProcessor
    {
        public override void Process(InboundIndexFilterArgs args)
        {
            var item = args.IndexableToIndex as SitecoreIndexableItem;
            var language = Sitecore.Data.Managers.LanguageManager.GetLanguage("en");

            if (item != null && (!item.Item.Versions.IsLatestVersion() || item.Item.Language == language))
            {
                args.IsExcluded = true;
            }
        }
    }
 }

并将此处理器添加到配置中:

<pipelines>
   <indexing.filterIndex.inbound>
     <processor type="Sitecore.Custom.CustomInboundIndexFilter, Sitecore.Custom"></processor>
   </indexing.filterIndex.inbound>
</pipelines>

经过一些研究,我得出结论,要满足所有要求,自定义爬虫是唯一的解决方案。博文代码:http://ggullentops.blogspot.be/2016/10/custom-sitecore-index-crawler.html

主要原因是我们确实必须能够针对每个索引进行设置——这对于过滤器方法来说是不可能的。此外,当我们添加新版本时,必须从索引中删除以前的版本 - 如果没有爬虫也是不可能的...

有很多代码,所以我不会在这里全部复制,但我们必须覆盖 SitecoreItemCrawler 的 DoAdd 和 DoUpdate 方法,对于语言,我们还必须对 Update 方法进行一个小的覆盖。