liferay_searching


Searching in Liferay 6.2

标签(liferay,search): liferay,search


liferay search 底层是基于lucene进行关键字搜索,在企业开发我们都会选择solr集成。通常客户在查询content时,不需要显示出过多的content(因为默认的分词是或者的关系),这个时候我们一般需要更精确的搜所:

"liferay" AND "kaleo workflow"

这样我们就会查询既包含liferay的关键字也包含kaleo workflow的关键字的content。

1
2
3
4
5
6
7
8
9
10
11
12
13
BooleanQuery keyWordsQuery = BooleanQueryFactoryUtil.create(searchContext);
keyWords=escapeQueryCharsAnd(keyWords);
String[] keyarr=keyWords.split(" ");
StringBuilder sb = new StringBuilder();
int i=0;
for(String keyword:keyarr){
keyword="\""+keyword+"\"";
sb.append(keyword);
if(i<keyarr.length-1){
sb.append(" AND ");
}
i++;
}

这个escapeQueryCharsAnd方法的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public String escapeQueryChars(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
|| c == ':'|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{'
|| c == '}' || c == '~'|| c == '*' || c == '?' || c == '|' || c == '&'
|| c == ';' || c == '/'|| Character.isWhitespace(c)) {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}

热评文章