---
title: "Solrスキーマにカスタムフィールドを追加する"
slug: "add-custom-fields-to-a-solr-schema"
abstract: "このトピックでは、カスタムヘルパークラスを使ってSolrスキーマにフィールドを追加する方法について説明します。"
---
# Solrスキーマにカスタムフィールドを追加する

カスタムヘルパークラスを作成してSolrスキーマにカスタムフィールドを追加できます。

カスタムフィールドを追加するには:

1.  SchemaPopulateHelperを継承するクラスを作成し、このクラスを使ってSolrスキーマに追加したいフィールドやフィールドタイプを定義します。
    
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    using Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema;
    using SolrNet.Schema;
    
    namespace Documentation.Examples
    {
     public class CustomPopulateHelper : SchemaPopulateHelper
     {
     public CustomPopulateHelper(SolrSchema schema) : base(schema)
     {
    
     }
    
     public override IEnumerable<XElement> GetAllFields()
     {
     return base.GetAllFields().Union(GetAddCustomFields());
     }
    
     public override IEnumerable<XElement> GetAllFieldTypes()
     {
     return base.GetAllFieldTypes().Union(GetAddCustomFieldTypes());
     }
    
     private IEnumerable<XElement> GetAddCustomFields()
     {
     yield return CreateField("\*\_t\_pl",
     "text\_general",
     isDynamic: true,
     required: false,
     indexed: true,
     stored: true,
     multiValued: false,
     omitNorms: false,
     termOffsets: false,
     termPositions: false,
     termVectors: false);
     }
    
     private IEnumerable<XElement> GetAddCustomFieldTypes()
     {
         var fieldType = CreateFieldType("custom\_text", "solr.TextField",
             new Dictionary<string, string>
             {
                 { "positionIncrementGap", "100" },
                 { "multiValued", "false" },
             });
            var indexAnalyzer = new XElement("indexAnalyzer");
        indexAnalyzer.Add(new XElement("tokenizer", new XElement("class", "solr.StandardTokenizerFactory")));
       indexAnalyzer.Add(new XElement("filters", new XElement("class", "solr.StopFilterFactory"), new XElement("ignoreCase", "true"), new XElement("words", "stopwords.txt")));
       indexAnalyzer.Add(new XElement("filters", new XElement("class", "solr.LowerCaseFilterFactory")));
       fieldType.Add(indexAnalyzer);
    
            var queryAnalyzer = new XElement("queryAnalyzer");
       queryAnalyzer.Add(new XElement("tokenizer", new XElement("class", "solr.StandardTokenizerFactory")));
         queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.StopFilterFactory"), new XElement("ignoreCase", "true"), new XElement("words", "stopwords.txt")));
       queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.SynonymFilterFactory"), new XElement("synonyms", "synonyms.txt"), new XElement("ignoreCase", "true"), new XElement("expand", "true")));
       queryAnalyzer.Add(new XElement("filters", new XElement("class", "solr.LowerCaseFilterFactory")));
        fieldType.Add(queryAnalyzer);
    
            yield return fieldType;
     }
     }
    }
    
    -   GetAllFieldsメソッドをオーバーライドしてカスタムフィールドを追加してください。ベースメソッドを呼び出し、結果とカスタムメソッドの結果を組み合わせることが重要です。
        
    -   GetAllFieldTypesメソッドをオーバーライドしてカスタムフィールドタイプを追加してください。ベースメソッドを呼び出し、結果とカスタムメソッドの結果を組み合わせることが重要です。
        
    -   CreateFieldメソッドを使って新しいフィールドと属性を追加してください。Solrのドキュメントには、以下の2つを除くすべてのパラメータの説明があります:
        
        パラメータ
        
        種類
        
        概要
        
        デフォルト値
        
        defaultName
        
        ストリング
        
        フィールドのデフォルト値を指定します
        
        ヌル
        
        isDynamic
        
        ブール
        
        その場が動的場かどうかを指定してください。
        
        偽り
        
    -   CreateFieldTypeメソッドを使って新しいフィールドタイプと属性を追加してください。[Solrのドキュメント](https://lucene.apache.org/solr/guide/8_1/field-type-definitions-and-properties.html#field-type-definitions-and-properties)にはすべてのパラメータの記述があります。
        
2.  IPopulateHelperFactoryから継承するPopulateHelperファクトリークラスを作成し、GetPopulateHelperメソッドを実装し、カスタムクラスを返します。
    
    using Sitecore.ContentSearch.SolrProvider.Abstractions;
    using Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema;
    using SolrNet.Schema;
    
    namespace Documentation.Examples
    {
     public class CustomPopulateHelperFactory : IPopulateHelperFactory
     {
     public ISchemaPopulateHelper GetPopulateHelper(SolrSchema solrSchema)
     {
     return new CustomPopulateHelper(solrSchema);
     }
     }
    }
    
3.  作成したものでSitecoreクラスファクトリーを上書きする構成パッチファイルを作成してください:
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
     <sitecore>
     <pipelines>
     <contentSearch.PopulateSolrSchema>
     <processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider">
     <param type="Documentation.Examples.CustomPopulateHelperFactory, Documentation.Examples" patch:instead="\*@type='Sitecore.ContentSearch.SolrProvider.Factories.DefaultPopulateHelperFactory'"/>
     </processor>
     </contentSearch.PopulateSolrSchema>
     </pipelines>
     </sitecore>
    </configuration>