---
title: "インデックスにプロパティフィールドを追加する"
slug: "adding-a-property-field-to-an-index"
abstract: "新しいプロパティフィールドをインデックスに設定で追加し、新しいカスタムプロパティをキーワードとしてSitecoreでCommerceアイテムを検索できるようにする方法。"
---
# インデックスにプロパティフィールドを追加する

[Sitecoreインデックス再構築](https://doc.sitecore.com/xp/en/developers/102/platform-administration-and-architecture/rebuild-search-indexes.html)が呼び出すと、Commerce Engineのインデックスミニオンがインデックスされたフィールドをsitecore\_web\_indexとsitecore\_master\_indexに追加し、SitecoreコンテンツエディターやSXAストアフロントでHabitatカタログコンテンツアイテムなどの検索を可能にします。

Commerce Engineは、例えばカタログエンティティフィールドをインデックスフィールドタイプにマッピングするために必要な設定プロパティを定義する [複数のデフォルトインデックスフィールドハンドラ](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/catalog-search-field-handlers.html "Catalog search field handlers") を実装しています。検索インデックスに新しいカタログプロパティフィールドを導入するには、PlugIn.Search.Solr.PolicySet.\*.jsonファイルに適切なフィールドハンドラ設定を追加する必要があります。新しいフィールドを既存のカタログフィールドハンドラのいずれかにマッピングできるなら、コードを書いたり変更したりする必要はありません。既存のフィールドハンドラを使ってカスタムカタログアイテムフィールドをインデックスできない場合は、それをサポートする [カスタムフィールドハンドラーを作成できます](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/walkthrough--create-a-custom-index-field-handler.html "Walkthrough: Create a custom index field handler") 。

## 検索ポリシーセットファイルにおけるインデックスフィールドマッピング定義の概要

新しいフィールドをインデックスにマッピングする際に考慮すべきアプローチは2つあります。新しいインデックスフィールドを直接Commerceエンティティプロパティにマッピングするか、Solrの動的フィールドなどの計算フィールドをフィールドハンドラー実装で使用する方法と似ています。これはSitecoreが[Solrフィールド名解決](https://doc.sitecore.com/xp/en/developers/102/platform-administration-and-architecture/using-solr-field-name-resolution.html)で計算フィールドを使う方法に似ています。

新しいCommerceエンティティプロパティをインデックスに追加するには、PlugIn.Search.Solr.PolicySet.\*.jsonファイルのカスタムエンティティプロパティにインデックスフィールドタイプをマッピングします。このポリシーセットファイルは、デプロイメントでインデックスロールをホストしているCommerce Engineのインスタンス(例: C:\\inetpub\\wwwroot\\CommerceMinions\_Sc\\wwwroot\\data\\Environments))に存在します。

ポリシーセットファイルでは、ItemIndexablePolicyポリシーがインデックスフィールドマッピングを2つの主要な構成セクション、すなわち "FieldTypeMappers" セクションと "Fields" セクションに分けて定義しています。

### FieldTypeMappersの設定

"FieldTypeMappers"セクションでは、異なるCommerce Engineのデータ型がSorlインデックスのフィールドタイプにどのようにマッピングされるかを定義するIndexFieldTypeMapper構成の集合を詳述しています。以下のPlugIn.Search.Solr.PolicySet.\*.jsonファイルのサンプルは、ウェブインデックスのデフォルト"FieldTypeMappers"定義の一部を示しています("IndexName": "sitecore\_web\_index"):

"$type": "Sitecore.Commerce.Core.PolicySet, Sitecore.Commerce.Core",
 "Id": "Entity-PolicySet-SolrSearchPolicySet",
 "Version": 1,
 "IsPersisted": false,
 "Name": "SolrSearchPolicySet",
 "Policies": {
 "$type": "System.Collections.Generic.List\`1Sitecore.Commerce.Core.Policy, Sitecore.Commerce.Core, mscorlib",
 "$values": 
 ...
 {

 "$type": "Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy, Sitecore.Commerce.Plugin.Search",
 "IndexName": "sitecore\_web\_index",
 "FieldTypeMappers": 
 {
 "TypeName": "stringCollection",
 "Type": "System.Collections.Generic.List\`1System.String",
 "NameFormat": "{0}\_sm",
 "MultiValued": true
 },
 {
 "TypeName": "textCollection",
 "Type": "System.Collections.Generic.List\`1System.String",
 "NameFormat": "{0}\_txm",
 "MultiValued": true
 },
 {
 "TypeName": "text",
 "Type": "System.String",
 "NameFormat": "{0}\_t",
 "CultureFormat": "\_{1}"

#### FieldTypeMappersプロパティ

FieldTypeMappersの構成特性は以下の通りです:

性質

概要

"TypeName"

コマースエンジンで使用されているフィールドタイプの名称です。.

"Type"

システム型を実装するクラス名を指定します。

"NameFormat"

は型名のフォーマットを定義します。

"CultureFormat"

該当する場合、ローカライズのための文化を指定する形式を定義します。

"MultiValued"

このフィールドタイプに対して、単一の文書に複数の値が含まれているかどうかを示します。

### フィールド構成

SolrIndexFieldConfigurationクラスは"Fields"構成のプロパティを定義します。.jsonファイルの"Fields"セクションでは、CommerceエンティティやコンポーネントプロパティをSolrインデックスフィールドにマッピングする構成プロパティの集合を詳細に説明しています。以下はPlugIn.Search.Solr.PolicySet.\*.jsonファイルの"Fields"セクションからのデフォルトのマッピングの一例を示しています。

"$type": "Sitecore.Commerce.Core.PolicySet, Sitecore.Commerce.Core",
 "Id": "Entity-PolicySet-SolrSearchPolicySet",
 "Version": 1,
 "IsPersisted": false,
 "Name": "SolrSearchPolicySet",
 "Policies": {
 "$type": "System.Collections.Generic.List\`1Sitecore.Commerce.Core.Policy, Sitecore.Commerce.Core, mscorlib",
 "$values": 
 {
 "FieldTypeMappers": 
 ...

 "Fields": 
 {
 "$type": "Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration, Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "sxacontent",
 "Type": "System.Collections.Generic.List\`1System.String",
 "TypeHint": "textCollection",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.SxaContentFieldHandler, Sitecore.Commerce.Plugin.Catalog"
 }
 },

#### 場の性質

以下の表は の性質を一覧と定義Fields:

財産

概要

"$type"

フィールド型を実装するクラス名を参照します。

"Name"

インデックスフィールド名を指定します。

"Type"

フィールドの種類。

"TypeHint"

パス可能な値の種類を定義します。

"Handler"

フィールドハンドラを定義するクラスへの参照を指定します。

"ValueSource"

SitecoreのCommerceプロパティの名前("PropertyName")を指定し、インデックスフィールド名にマッピングします。また、該当する場合はプロパティを定義するコンポーネントクラスを指定する "PropertyPath" 値も取ります。

"Localizable"

場が局所化可能な文字列かどうかを指定します。

## インデックスフィールドマッピングへのカタログエンティティプロパティの例

Commerceエンティティプロパティをインデックスフィールドにマッピングする方法は多岐にわたります。このセクションでは、フィールドマッピングのさまざまな構成例を提供します。

マッピング構成はSolrポリシーセットファイルPlugIn.Search.Solr.PolicySet.\*.jsonで定義されています。

### インデックスフィールド名とエンティティプロパティが同じ1:1マッピングの例

以下は、インデックスフィールド名("DisplayName")とCommerceプロパティ名が同じである例を示しています。Commerceプロパティ名を定義するために "ValueSource" を指定する必要も、ハンドラーを指定する必要もありません。

{
 "$type":"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
 Sitecore.Commerce.Plugin.Search.Solr",
 "Name":"DisplayName",
 "Type":"System.String"
}

### インデックスフィールド名とエンティティプロパティ名が異なるマッピングの例

インデックスフィールド名が静的なCommerceエンティティプロパティ名に直接マッピングされないケースもあります。以下の例では、インデックスフィールド名"CatalogEntityId""ValueSource"で指定されたCommerceエンティティプロパティ名にマッピングされます:

{
 "$type":"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration, Sitecore.Commerce.Plugin.Search.Solr",
 "Name":"CatalogEntityId",
 "Type":"System.String",
 "ValueSource":{
 "PropertyName":"Id",
 "PropertyPath":""
 }
}

### 実際のプロパティ名とは異なるフィールド名を使ったコンポーネントからのプロパティマッピングの例

以下は、インデックスフィールドDisplayPropertiesColorが "ValueSource".

{
 "$type":"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name":"DisplayPropertiesColor",
 "Type":"System.String",
 "TypeHint":"string",
 "ValueSource": {
 "PropertyName":"Color",
 "PropertyPath":"DisplayPropertiesComponent"
 }
}

### カスタムフィールドハンドラを用いたマッピングの例

以下はカスタムインデックスフィールドハンドラを使用した例の構成を示します。

{
 "$type":"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name":"\_UniqueId",
 "Type":"System.String",
 "Handler":{
 "$type":"Sitecore.Commerce.Plugin.Catalog.UniqueIdFieldHandler,
Sitecore.Commerce.Plugin.Catalog"
 }
}

## Composerが定義したカタログ項目プロパティをインデックスに追加する

[Composerテンプレートを使ってカタログエンティティのカスタムプロパティを定義するビューを追加する](https://doc.sitecore.com/xp/en/users/102/sitecore-experience-commerce/work-with-custom-child-views.html)場合(つまり、テンプレートをカタログ、カテゴリ、またはセレアブルアイテムエンティティにリンクする場合)、ComposerFieldHandlerを使って新しいプロパティフィールドをインデックスに追加します。

> !注リッチテキスト値を使用するカスタムプロパティをインデックス化する場合、すべての書式情報は削除されます。テキストベースの検索サポートのために、生のテキスト値のみがインデックス化されます。

作曲家フィールドハンドラーの設定をPlugIn.Search.Solr.PolicySet.\*.jsonファイルに追加します。

### 作曲家フィールドハンドラー

[Catalogプラグイン](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/catalog-search-field-handlers.html "Catalog search field handlers")は、Composerを使って定義したカスタムエンティティプロパティをインデックスに追加できるComposerFieldHandlerクラスを提供します。Composerフィールドハンドラには、Composerテンプレート内のビュー名とインデックスフィールドにマッピングするカスタムプロパティの名前を指定する "ComposerSource" セクションが含まれています。

"Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler,
Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "StringPropertyName"
 }

#### ComposerFieldHandlerインデックスフィールドハンドラのプロパティ

Composerフィールドハンドには、Composerビューの名前とインデックスするカスタムプロパティの名前を指定するプロパティが含まれ "ComposerSource" 。以下の表はComposerFieldHandlerの構成プロパティを一覧および説明しています:

財産

概要

"$type"

以下を実装するクラス名を指定します。 ComposerFieldHandler

"Viewname"

Composerのカスタムエンティティプロパティを含む子ビュー名を指定します。

"PropertyName"

インデックスフィールドにマッピングされるカスタムComposerエンティティプロパティの名前を指定します。

> !注ViewnameとPropertyNameは [、カタログエンティティの子ビュー](https://doc.sitecore.com/xp/en/users/102/sitecore-experience-commerce/work-with-custom-child-views.html)で定義されているものです。

### Composer文字列プロパティのマッピングの例

以下は、カスタムカタログエンティティプロパティを文字列データ型として定義したものを、適切なインデックスフィールドタイプにマッピングする設定例を示しています。

{
 "$type": "Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "StringPropertyIndexName",
 "Type": "System.String",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler,Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "StringPropertyName"
 }
 }
}

### Composer整数性質の写像の例

以下は、整数データ型として定義されたカスタムカタログエンティティプロパティを適切なインデックスフィールドタイプにマッピングする構成例を示します。

{
 "$type":
"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "IntegerPropertyIndexName",
 "Type": "System.Int64",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler, Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "IntegerPropertyName"
 }
 }
}

### Composerの十進性質の写像の例

以下は、カスタムカタログエンティティプロパティを10進データ型として定義したものを、適切なインデックスフィールドタイプにマッピングするための構成例を示しています。

{
 "$type":
"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "DecimalPropertyIndexName",
 "Type": "System.Decimal",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler, Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "DecimalPropertyName"
 }
 }
}

### Composer DateTimeOffSetプロパティのマッピング例

以下は、DateTimeOffSetデータ型として定義されたカスタムカタログエンティティプロパティを適切なインデックスフィールドタイプにマッピングする構成例を示しています。

{
 "$type":
"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "DateTimeOffsetPropertyIndexName",
 "Type": "System.DateTimeOffset",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler,Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "DateTimeOffsetPropertyName"
 }
 }
}

### Composer Booleanプロパティの写像の例

以下は、ブール型として定義されたカスタムカタログエンティティプロパティを適切なインデックスフィールドタイプにマッピングする構成例を示しています。

{
 "$type":
"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration,
Sitecore.Commerce.Plugin.Search.Solr",
 "Name": "BooleanPropertyIndexName",
 "Type": "System.Boolean",
 "Handler": {
 "$type": "Sitecore.Commerce.Plugin.Catalog.ComposerFieldHandler, Sitecore.Commerce.Plugin.Catalog",
 "ComposerSource": {
 "ViewName": "MyViewName",
 "PropertyName": "BooleanPropertyName"
 }
 }

## Sitecore専用のカタログ項目フィールドをインデックスに追加する

Sitecoreで [販売可能なアイテムテンプレートにカスタムフィールドを追加](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/add-new-fields-to-a-catalog-item-template.html "Add new fields to a catalog item template") したり、既存のSitecore固有アイテムフィールドの値を設定した場合、これらのカスタムSitecoreアイテムフィールドはSitecoreのみ存在します。Commerce Engine側では、これらのSitecore固有フィールドはExternalSettingsComponentsとして永続します。Sitecore XCはExternalSettingsFieldHandlerハンドラーを実装しており、Commerce EngineがインデックスにSitecore固有のカタログアイテムフィールドを追加できるように設定できます。

### 外部設定フィールドハンドラ

Commerce Engine [Catalogプラグイン](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/catalog-search-field-handlers.html "Catalog search field handlers") は、Sitecore固有のカタログ項目プロパティを検索インデックスに追加できるExternalSettingsFieldHandlerクラスを提供します。

以下はPlugIn.Search.Solr.PolicySet.\*.jsonに加えるExternalSettingsFieldHandler構成の例です:

{
 "$type": "Sitecore.Commerce.Plugin.Search.ItemIndexablePolicy, Sitecore.Commerce.Plugin.Search",
 "IndexName": "sitecore\_master\_index",
 "FieldTypeMappers":
 ...
 "Fields": 
 {
 "$type":"Sitecore.Commerce.Plugin.Search.Solr.SolrIndexFieldConfiguration, Sitecore.Commerce.Plugin.Search.Solr",
 "Name":"ExampleExternalSettingProperty",
 "Type":"System.String",
 "Handler":{
 "$type":"Sitecore.Commerce.Plugin.Catalog.ExternalSettingsFieldHandler, Sitecore.Commerce.Plugin.Catalog",
 "ExternalSettingsPropertyName":"Items Per Page"
 },
 "Localizable":false

#### ExternalSettingsFieldHandlerインデックスフィールドハンドラのプロパティ

以下の表は、ExternalSettingsFieldHandlerハンドラの設定特性を一覧かつ説明しています。

財産

概要

"ExternalSettingsPropertyName"

インデックスする値を定義するカスタムSitecoreカタログのアイテムフィールドの名前です。

> !注外部設定プロパティが [共有プロパティ](https://doc.sitecore.com/xp/en/developers/102/sitecore-experience-manager/the-template-field-template.html)であれば、"Localizable" プロパティを "false"に設定しなければなりません。

## Solr構成の更新

Solr検索プロバイダーを使用していて、カスタムフィールドを使って検索できるようにしたい場合は、新しいフィールドとデータ型を定義する構成パッチファイルを作成しなければなりません。 /App\_Config/Include/Z.Commerce.Engineフォルダに設定ファイルを追加します。以下は構成パッチファイルの例です:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">

 <sitecore role:require="Standalone or ContentDelivery or ContentManagement" 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="stringpropertysolr" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" returnType="string" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider"/>
 </fieldNames>
 </fieldMap>
 </defaultSolrIndexConfiguration>
 </indexConfigurations>
 </contentSearch>
 </sitecore>
</configuration>