星期三, 12月 07, 2011

[Alfresco] Lucene Search: Escaping special characters


今天測試開了特殊字元的帳號查看垃圾桶的檔案,在alfresco的web ui也爆炸了。
發現UID含特殊字元時,也要記得跳脫!!
you are using Lucene 1.4 or prior, there is no escape convenience utility. Instead, you must write your own. The characters that need to be escaped are: + - ! ( ) { } [ ] ^ " ~ * ? : \
Lucene 1.4 Escaping (More Complete)
// Some constants.
private static final String LUCENE_ESCAPE_CHARS = "[\\\\+\\-\\!\\(\\)\\:\\^\\]\\{\\}\\~\\*\\?]";
private static final Pattern LUCENE_PATTERN = Pattern.compile(LUCENE_ESCAPE_CHARS);
private static final String REPLACEMENT_STRING = "\\\\$0";
 
// ... Then, in your code somewhere...
String userInput = // ...
String escaped = LUCENE_PATTERN.matcher(userInput).replaceAll(REPLACEMENT_STRING);
Query query = QueryParser.parse(escaped);
// ...


Reference:
Lucene: Escaping Special Characters

沒有留言:

張貼留言

留個話吧:)