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

Sitecore Experience Commerce(XC)はデフォルトでカタログエンティティのプロパティ(カタログ、カテゴリ、販売可能なアイテムのプロパティ)をインデックス化します。 [Sitecoreインデックスの再構築](https://doc.sitecore.com/xp/en/developers/101/platform-administration-and-architecture/en/rebuild-search-indexes.html) が起動されると、Commerce Engineのミニオンがインデックスされたフィールドをsitecore\_web\_indexとsitecore\_master\_indexに追加します。デフォルトのインデックスは、例えばHabitatカタログのコンテンツ項目をSitecore Content EditorやSXAストアフロントで検索する機能を提供します。新しい検索フィールド(カスタムフィールドを含む)を追加することで検索インデックスを拡張できます。

検索インデックスに新しいCommerceプロパティフィールドを導入するには、新しいプロパティフィールドを検索プロバイダーが処理可能なフィールドタイプにマッピングする必要があります。このマッピングは、ポリシーセットファイルの設定を通じて実現でき、コードを書いたり変更したりする必要がありません。新しいフィールドをデフォルトの検索フィールドハンドラーにマッピングできればの話です。 [Searchプラグイン](https://doc.sitecore.com/xp/ja/developers/101/sitecore-experience-commerce/search-field-handlers.html "Search field handlers") と[Catalogプラグイン](https://doc.sitecore.com/xp/ja/developers/101/sitecore-experience-commerce/catalog-search-field-handlers.html "Catalog search field handlers") は、カタログプロパティマッピングに使用されるデフォルトの検索フィールドハンドラを定義しています。既存のフィールドハンドラーを使えない場合は、[カスタムフィールドハンドラーを作成できます](https://doc.sitecore.com/xp/ja/developers/101/sitecore-experience-commerce/walkthrough--create-a-custom-index-field-handler.html "Walkthrough: Create a custom index field handler")。

## 検索ポリシーセットファイル内のインデックスフィールドマッピング定義

新しいフィールドをインデックスにマッピングする際に考慮すべきアプローチは2つあります。新しいインデックスフィールドを直接Commerceエンティティプロパティにマッピングするか、[Solrの動的フィールド](http://) などの計算フィールドをフィールドハンドラー実装で使用する方法と似ています。これはSitecoreが[Solrフィールド名解決](https://doc.sitecore.com/xp/en/developers/101/platform-administration-and-architecture/en/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"セクションでは、異なるデータ型がインデックスフィールドにどのようにマッピングされるかを定義する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

#### FieldTypeMappersプロパティ

以下の表はFieldTypeMappers構成の性質を一覧かつ説明しています。

性質

概要

"TypeName"

フィールドタイプの名称を定義します。

"Type"

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

"NameFormat"

型名の形式を定義します。

"MultiValued"

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

### フィールド構成

"Fields"セクションでは、Commerceエンティティやコンポーネントプロパティをインデックスフィールドに実際にマッピングする設定プロパティの集合を詳細に説明しています。以下は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"

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

"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エンティティのプロパティ名に直接対応しないケースもあります。以下はその例を示しています:

{
 "$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/101/sitecore-experience-commerce/en/work-with-custom-child-views.html)場合(つまり、テンプレートをカタログ、カテゴリ、またはセレアブルアイテムエンティティにリンクした場合)、新しいプロパティフィールドをインデックスに追加できます。

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

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

[Catalogプラグイン](https://doc.sitecore.com/xp/ja/developers/101/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"
 }

Composerフィールドハンドラーの設定パラメータは以下の通りです:

-   "$type": は を実装するクラス名を指定します。 ComposerFieldHandler
-   "Viewname": はカスタムエンティティプロパティを定義する子ビュー名を指定します。
-   "PropertyName": はインデックスフィールドに対応するカスタムエンティティプロパティの名前を指定します。

> !注ViewnameとPropertyNameは [、カタログエンティティの子ビュー](https://doc.sitecore.com/xp/en/users/101/sitecore-experience-commerce/en/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"
 }
 }

## 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>