Solrスキーマへのカスタム・フィールドの追加

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

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

カスタム・ヘルパー・クラスを作成して、カスタム・フィールドを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つを除くすべてのパラメーターの説明があります。

      パラメーター

      種類

      形容

      既定値

      デフォルト名

      フィールドのデフォルト値を指定します

      ヌル

      isダイナミック

      ブール値

      フィールドが動的フィールドであるかどうかを指定します

    • CreateFieldTypeメソッドを使用して、新しいフィールドタイプと属性を追加します。Solrドキュメントには、すべてのパラメーターの説明があります。

  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>
この記事を改善するための提案がある場合は、 お知らせください!