sitecore搜索同义词文件位置



我已将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:inetpubwwwrootwebsiteDatasynonyms.xml</param>
</param>

我想做的是在CMS中管理这些数据。有人知道我如何设置这个xmlSynymFilePath参数来实现我想要的吗?还是我错过了什么?

最简单的解决方案是使用只有一个名为Synonyms的多行字段的模板在Sitecore中创建一个项(例如/sitecore/system/synonyms),并将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类实例化时,它只读取同义词列表一次(我不知道Sitecore是保留实例还是多次创建新实例)。

您应该扩展此代码以缓存同义词,并在每次更改同义词列表时清除缓存。

此外,您应该考虑在Sitecore树中拥有一个漂亮的同义词结构,而不是拥有一个很难维护的单项和xml blob。

最新更新