インデックス作成中にカスタムフィールドを作成する

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

コンテンツ検索でインデックスが作成される新しい計算フィールドを追加します。

次の前提条件を満たします。

  • 既存のカタログをSitecoreSiteに関連付ける

  • カスタム カタログ プロパティでSitecoreファセットを作成するには、『 アイテム バケットと検索の開発者ガイド』の手順に従います。また、検索インデックスには、カスタムカタログプロパティのフィールドが含まれている必要があります。

  • 製品とカテゴリの定義を含む1つ以上の仮想カタログまたは基本カタログがあります。

  • 検索コンポーネントが構成されます。

Visual Studioソリューションのセットアップで、次の操作を行います。

  1. フィールドの値を計算するためのIComputedIndexFieldインターフェイスを実装する新しいクラスを作成します。

    基本クラスSitecore.Commerce.Search.ComputedFields.BaseCommerceComputedField、このインターフェイスのこのメソッドのほとんどを実装します。あなたはそれを見つけることができますconfiguration/sitecore/contentSearch/configuration/defaultIndexConfiguration/fields

    namespace My.Commerce.Overrides
    {
        using System.Collections.Generic;
        using Sitecore.Data;
        using Sitecore.Data.Items;
        using Sitecore.Commerce;
        using Sitecore.Commerce.Search.ComputedFields;
        using Sitecore.ContentSearch;
    
        public class PriceRangeComputedField : BaseCommerceComputedField
        {
            private static List<ID> _validBaseTemplates = new List<ID>
            {
                CommerceConstants.KnownTemplateIds.CommerceProductTemplate
            };
    
            protected override IEnumerable<ID> ValidTemplates
            {
                get { return _validBaseTemplates; }
            }
    
            public override object ComputeValue(IIndexable itemToIndex)
            {
                var validatedItem = GetValidatedItem(itemToIndex);
                if (validatedItem == null)
                {
                    return CommerceSearchResultItemType.Unknown;
                }
    
                if (!validatedItem.HasChildren)
                {
                    return validatedItem["ListPrice"];
                }
    
                decimal currentPrice, minPrice = decimal.MaxValue, maxPrice = decimal.MinValue;
                foreach (Item variantItem in validatedItem.Children)
                {
                    currentPrice = decimal.Parse(variantItem["ListPrice"]);
                    minPrice = (currentPrice < minPrice) ? currentPrice : minPrice;
                    maxPrice = (currentPrice > maxPrice) ? currentPrice : maxPrice;
                }
    
                if (minPrice != maxPrice)
                {
                    return string.Format("{0} - {1}", minPrice, maxPrice);
                }
    
                return string.Format("{0}", minPrice);
            }
        }
    }
  2. 計算フィールドを適切な検索プロバイダーに登録します。

    SitecoreサイトのWebsite\App_Config\Includeディレクトリに新しい .configファイルを作成し、編集用に開きます。

    メモ

    システムは、ファイルを英数字順に適用します。オーバーライドを含むファイルに、CommerceServer.configの後に実行するように名前が付けられていることを確認します。たとえば、ZMy.CommerceServer.Overrides.configZCommerceServer.Index.Lucene.config

    このサンプルでは、Lucene検索に登録します。

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <contentSearch>
          <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
              <fields hint="raw:AddComputedIndexField">
                <field fieldName="PriceRange" returnType="string">Sitecore.Commerce.Search.ComputedFields.PriceRangeComputedField, Sitecore.Commerce</field>
              </fields>
            </defaultIndexConfiguration>
          </configuration>
        </contentSearch>
      </sitecore>
    </configuration>
  3. インデックスを再構築します。

    1. Sitecoreデスクトップを開きます。

    2. インデックス マネージャーを開きます。

      SitecoreControl PanelIndexingIndexing Manager → をクリックします

    3. 再構築するインデックスにチェックを入れ、Rebuild

計算された値はインデックス化され、検索クエリで使用できます。

この記事を改善するための提案がある場合は、 お知らせください!