如何使用特殊字符查询Sitecore SOLR?
我希望能够构建一个站点搜索页面,并允许用户在查询中包含特殊的Solr字符(使用Sitecore 7.5和Solr 4.7)。例如,我希望用户能够搜索“f(x)”。我有一些代码,似乎有时工作。基本上它逃避了特殊角色。如果我搜索“f(x)”,效果很好。然而,如果我想在我的查询中有两个术语并搜索“f(x)green”,它就不起作用。它似乎只是在搜索确切的短语“f(x)green”。常规查询可以正常工作。如果我搜索“绿-黄”,它会返回所有带有绿色或黄色的文档,如果我搜索“黄-绿”,我会得到相同的结果,这很好。如果我搜索“f(x)”,我会得到我期望的结果。但是如果我搜索“f(x)green”,我不会得到任何结果,这不是我所期望的。我的搜索代码如下如何使用特殊字符查询Sitecore SOLR?,sitecore,solr4,sitecore7.5,Sitecore,Solr4,Sitecore7.5,我希望能够构建一个站点搜索页面,并允许用户在查询中包含特殊的Solr字符(使用Sitecore 7.5和Solr 4.7)。例如,我希望用户能够搜索“f(x)”。我有一些代码,似乎有时工作。基本上它逃避了特殊角色。如果我搜索“f(x)”,效果很好。然而,如果我想在我的查询中有两个术语并搜索“f(x)green”,它就不起作用。它似乎只是在搜索确切的短语“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;
}
var specialSOLRChars=@“\+\-\&\\\\!\(\)\{\\\\\[\]\^\”\~*\?\:\”;
使用(var context=ContentSearchManager.GetIndex(“sitecore\u web\u index”).CreateSearchContext(){
var query=context.GetQueryable()。其中(x=>!x.IsStandardValue&&x.Language==Sitecore.context.Language.Name);
var isSpecialMatch=Regex.IsMatch(searchTerm,“[”+specialSOLRChars+“]);
如果(isSpecialMatch){
var wildcardText=string.Format(“\”*{0}*\”,Regex.Replace(searchTerm,“([”+specialSOLRChars+“])”,@“\$1”);
query=query.Where(i=>(i.Content.MatchWildcard(wildcardText)| | i.Name.MatchWildcard(wildcardText));
}
否则{
query=query.Where(i=>(i.Content.Contains(searchTerm)| | i.Name.Contains(searchTerm));
}
var results=query.GetResults();
返回结果;
}
您似乎想要得到既包含f(x)又包含绿色的搜索结果,并且还包含其中任何一个
要模拟此行为,您应该使用空格分割搜索词,并为这两个词检查Content.Contains(),并用|条件分隔。这将在搜索f(x)green时给出结果