建议先将关键词进行分词
// tokenStream分词
public static String analyze(Analyzer analyzer, String keyword) throws IOException {
StringBuffer sb = new StringBuffer();
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(keyword));
for (Token token = new Token(); (token = tokenStream.next(token)) != null;) {
sb.append(token.term() + " ");
}
return sb.toString();
}
设置关键词之间空格的与或关系
queryParser_and.setDefaultOperator(QueryParser.AND_OPERATOR);
queryParser_or.setDefaultOperator(QueryParser.OR_OPERATOR);
过滤特殊字符
Query query_and = queryParser_and.parse(QueryParser.escape(keyword));
遇到多余一个空格后的处理
/**
*
* 把超过一个空格后面的转化为 OR 号表达式
*
* @param wd
* @return eg: ibm t60 mp3 液晶 ibm t60 OR mp3 OR 液晶
*/
public static String nvl(String value) {
return value == null ? "" : value;
}
public static String parseWd(String wd) {
String retwd = nvl(wd).replaceAll(" ", " ").replaceAll(" ", " ");
String[] arr = nvl(retwd).split(" ");
if (arr != null && arr.length > 2) {
retwd = (arr[0].trim().equals("OR") ? "" : arr[0] + " ") + (arr[1].trim().equals("OR") ? "" : arr[1]);
for (int i = 2; i < arr.length; i++) {
if (!arr[i].trim().equals("OR")) {
retwd += " OR " + arr[i];
}
}
}
return retwd;
}