现象: 在搜索处输入 2012~2013 ,搜索报错
解决办法:
(1) 在StringUtil中添加 如下方法
/**
* 过滤特殊字符
* @param str
* @return
*/
public static String replaceSpecialStr(String str) {
if(isEmpty(str)) {
return str;
}
// 特殊字符列表
String[] specialStr = { "!", "||", "-", "(", ")"
,"{", "}", "[", "]", "^", "\"", ":", ";" ,
"+", "&&", "~", "*", "?", "AND", "NOT",
"OR", "(", ")", "【", "】", "\\", "<", ">"};
for(String s : specialStr) {
str = str.replace(s, "");
}
return str;
}
搜索服务实现类中:
(2)model.setKeyWord(StringUtil.filter(model.getKeyWord()));
model.setKeyWord(SolrUtil.escapeChar(model.getKeyWord())); 代码位置处
/** 需要将特殊符号进行转义*/
String keyWordTemp = model.getKeyWord();
model.setKeyWord(StringUtil.filter(model.getKeyWord()));
model.setKeyWord(SolrUtil.escapeChar(model.getKeyWord()));
model.setKeyWord(StringUtil.replaceSpecialStr(model.getKeyWord()));
(3) SolrPageResultContainer solrPageResultContainer = queryResult.getSolrPageResultContainer();
后 添加 如下,再return SolrServer.getPageResultContainerBySolr(solrPageResultContainer);
model.setKeyWord(keyWordTemp);