sitecore - 如何使用特殊字符查询 Sitecore SOLR?

标签 sitecore solr4 sitecore7.5

我希望能够构建站点搜索页面并允许用户在他们的查询中包含特殊的 Solr 字符(使用 Sitecore 7.5 和 SOLR 4.7)。例如,我希望用户能够搜索“f(x)”。我有一些代码有时似乎可以工作。基本上它转义了特殊字符。如果我搜索“f(x)”,它工作正常。但是,如果我想在查询中包含 2 个术语并搜索“f(x) green”,则它不起作用。它似乎只是搜索确切的短语“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;
}

最佳答案

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

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

关于sitecore - 如何使用特殊字符查询 Sitecore SOLR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786024/

相关文章:

Sitecore 8 EXM 从 listmanager 添加联系人到列表

error-handling - Sitecore - 多站点错误页面处理

sitecore - 使用 Glass Mapper 在 Sitecore MVC View 中继承模型和渲染参数

search - Solr 停用词魔法

solr - 无法在 Solr 4.4 中加载 KeywordRepeatFilter

sitecore - 使用静态值填充 Sitecore Droplist

solr - 在 Solr 4 中配置健康检查

sitecore - Sitecore 中是否有任何地方可以添加有关内容项版本的作者注释?

sitecore - 如何将 Sitecore 从 7.5 升级到 8.1 Update 3?

search - Sitecore HOWTO : Search item bucket for items with specific values