sitecore 搜索同义词文件位置
sitecore search synonyms file location
我已将我的 DefaultIndexConfiguration 配置文件更改为基于同义词 (http://firebreaksice.com/sitecore-synonym-search-with-lucene/) 进行搜索,并且工作正常。但是,这是基于文件系统
中的 xml 文件
<param hint="engine" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.XmlSynonymEngine, Sitecore.ContentSearch.LuceneProvider">
<param hint="xmlSynonymFilePath">C:\inetpub\wwwroot\website\Data\synonyms.xml</param>
</param>
我想做的是在 CMS 中管理这些数据。
有谁知道如何设置此 xmlSynonymFilePath 参数来实现我想要的?还是我遗漏了什么?
最简单的解决方案是在 Sitecore 中创建一个项目(例如 /sitecore/system/synonyms
),使用只有一个名为 Synonyms
的 multi-line 字段的模板,并将 xml 保留在这个字段而不是从文件中读取它。
然后像那样创建 ISynonymEngine
的自定义实现(这只是最简单的示例 - 它是 NOT 生产就绪代码):
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>
这是 NOT 生产就绪代码 - 它只在实例化 CustomSynonymsEngine
class 时读取同义词列表一次(我不知道如果 Sitecore 保留实例或多次创建新实例)。
您应该扩展此代码以缓存同义词并在每次同义词列表更改时清除缓存。
此外,您应该考虑在 Sitecore 树中拥有一个很好的同义词结构,而不是只有一个项目和 xml blob,这将非常难以维护。
我已将我的 DefaultIndexConfiguration 配置文件更改为基于同义词 (http://firebreaksice.com/sitecore-synonym-search-with-lucene/) 进行搜索,并且工作正常。但是,这是基于文件系统
中的 xml 文件<param hint="engine" type="Sitecore.ContentSearch.LuceneProvider.Analyzers.XmlSynonymEngine, Sitecore.ContentSearch.LuceneProvider">
<param hint="xmlSynonymFilePath">C:\inetpub\wwwroot\website\Data\synonyms.xml</param>
</param>
我想做的是在 CMS 中管理这些数据。 有谁知道如何设置此 xmlSynonymFilePath 参数来实现我想要的?还是我遗漏了什么?
最简单的解决方案是在 Sitecore 中创建一个项目(例如 /sitecore/system/synonyms
),使用只有一个名为 Synonyms
的 multi-line 字段的模板,并将 xml 保留在这个字段而不是从文件中读取它。
然后像那样创建 ISynonymEngine
的自定义实现(这只是最简单的示例 - 它是 NOT 生产就绪代码):
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>
这是 NOT 生产就绪代码 - 它只在实例化 CustomSynonymsEngine
class 时读取同义词列表一次(我不知道如果 Sitecore 保留实例或多次创建新实例)。
您应该扩展此代码以缓存同义词并在每次同义词列表更改时清除缓存。
此外,您应该考虑在 Sitecore 树中拥有一个很好的同义词结构,而不是只有一个项目和 xml blob,这将非常难以维护。