Solrスキーマにカスタムフィールドを追加する
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
カスタムヘルパークラスを作成してSolrスキーマにカスタムフィールドを追加できます。
カスタムフィールドを追加するには:
-
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
GetAllFields() { return base.GetAllFields().Union(GetAddCustomFields()); } public override IEnumerable
GetAllFieldTypes() { return base.GetAllFieldTypes().Union(GetAddCustomFieldTypes()); } private IEnumerable
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
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のドキュメントにはすべてのパラメータの記述があります。
-
-
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); } } }
-
作成したものでSitecoreクラスファクトリーを上書きする構成パッチファイルを作成してください: