【问题标题】:How to query Sitecore SOLR using special characters?如何使用特殊字符查询 Sitecore SOLR?
【发布时间】:2015-11-18 17:02:26
【问题描述】:

我希望能够构建一个站点搜索页面并允许用户在他们的查询中包含特殊的 Solr 字符(使用 Sitecore 7.5 和 SOLR 4.7)。例如,我希望用户能够搜索“f(x)”。我有一些似乎有时可以工作的代码。基本上它会转义特殊字符。如果我搜索“f(x)”,它工作正常。但是,如果我想在查询中包含 2 个术语并搜索“f(x) green”,则它不起作用。它似乎只是搜索 EXACT 短语“f(x) green”。常规查询工作正常。如果我搜索“green yellow”,它会返回所有带有绿色或黄色的文档,如果我搜索“yellow green”,我会得到相同的结果,这很好。如果我搜索“f(x)”,我会得到我期望的结果。但是,如果我搜索“f(x) green”,我不会得到任何结果,这不是我所期望的。我的搜索代码如下。

var specialSOLRChars = @"\+\-\&\|\!\(\)\{\}\[\]\^\""\~\*\?\:\\";

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) {

   var query = context.GetQueryable<GeneralSearchResultItem>().Where(x => !x.IsStandardValue && x.Language == Sitecore.Context.Language.Name);
   var isSpecialMatch = Regex.IsMatch(searchTerm, "[" + specialSOLRChars + "]");
   if (isSpecialMatch) {
      var wildcardText = string.Format("\"*{0}*\"", Regex.Replace(searchTerm, "([" + specialSOLRChars + "])", @"\$1"));
      query = query.Where(i => (i.Content.MatchWildcard(wildcardText) || i.Name.MatchWildcard(wildcardText));
   } 
   else {
      query = query.Where(i => (i.Content.Contains(searchTerm) ||    i.Name.Contains(searchTerm));
   }
   var results = query.GetResults();
   return results;
}

【问题讨论】:

    标签: sitecore solr4 sitecore7.5


    【解决方案1】:

    您似乎打算获得同时包含 f(x)green 并且还包含其中任何一个的搜索结果。

    要模仿这种行为,您应该使用空格分隔搜索词,并检查 Content.Contains() 是否用 || 条件分隔它们。这将在您搜索 f(x) green 时为您提供结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多