【发布时间】: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