最简单的解决方案是在Sitecore中创建一个项目(例如/sitecore/system/synonyms
),该模板只使用一个名为Synonyms
的多行字段,并将xml保留在该字段中,而不是从文件中读取。
然后创建的ISynonymEngine
喜欢您的自定义实现(这只是最简单的例子 - 这是不生产准备代码):
public class CustomSynonymEngine : Sitecore.ContentSearch.LuceneProvider.Analyzers.ISynonymEngine
{
private readonly List<ReadOnlyCollection<string>> _synonymGroups = new List<ReadOnlyCollection<string>>();
public CustomSynonymEngine()
{
Database database = Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database ?? Database.GetDatabase("web");
Item item = database.GetItem("/sitecore/system/synonyms"); // or whatever is the path
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(item["synonyms"]);
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/synonyms/group");
if (xmlNodeList == null)
throw new InvalidOperationException("There are no synonym groups in the file.");
foreach (IEnumerable source in xmlNodeList)
_synonymGroups.Add(
new ReadOnlyCollection<string>(
source.Cast<XmlNode>().Select(synNode => synNode.InnerText.Trim().ToLower()).ToList()));
}
public IEnumerable<string> GetSynonyms(string word)
{
Assert.ArgumentNotNull(word, "word");
foreach (ReadOnlyCollection<string> readOnlyCollection in _synonymGroups)
{
if (readOnlyCollection.Contains(word))
return readOnlyCollection;
}
return null;
}
}
并注册在Sitecore的配置,而不是默认引擎引擎:
<analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.PerExecutionContextAnalyzer, Sitecore.ContentSearch.LuceneProvider">
<param desc="defaultAnalyzer" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.DefaultPerFieldAnalyzer, Sitecore.ContentSearch.LuceneProvider">
<param desc="defaultAnalyzer" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.SynonymAnalyzer, Sitecore.ContentSearch.LuceneProvider">
<param hint="engine" type="My.Assembly.Namespace.CustomSynonymEngine, My.Assembly">
</param>
</param>
</param>
</analyzer>
这是不是生产就绪代码 - 它只能读取同义词列表一次CustomSynonymsEngine
类实例化(我不知道Sitecore是否保留实例或多次创建新实例)。
您应该扩展此代码以缓存同义词并在每次更改同义词列表时清除缓存。
另外,你应该考虑在Sitecore树中有一个很好的同义词结构,而不是有一个项目和xml blob,这将很难维护。
您是否想要在Sitecore或同义词列表中管理同义词文件的路径? –
@MarekMusielak同义词列表。谢谢 – Snapper