sitecore搜索同义词文件位置

Sna*_*per 4 lucene sitecore sitecore7

我已经将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>
Run Code Online (Sandbox Code Playgroud)

我想做的是在CMS中管理这些数据.有谁知道如何设置这个xmlSynonymFilePath参数来实现我想要的?或者我错过了什么?

Mar*_*lak 5

最简单的解决方案是/sitecore/system/synonyms使用模板在Sitecore(例如)中创建一个项目,只需调用一个多行字段,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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并在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>
Run Code Online (Sandbox Code Playgroud)

不是生产就绪代码 - 它只在CustomSynonymsEngine实例化类时读取同义词列表(我不知道Sitecore是保留实例还是多次创建新实例).

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

此外,你应该考虑在Sitecore树中有一个很好的同义词结构,而不是有一个项目和xml blob,这将很难维护.