如何使用Grails Searchable Plugin重音不敏感进行全文搜索?
答案 0 :(得分:5)
我已经在Peter Ledbrook's post的帮助下解决了这个问题,但需要付出一些努力:
由于最新的可搜索插件使用的Lucene 2.4.1不包含ASCIIFoldingFilter(自2.9.0起可用)而ISOLatin1AccentFilter不支持多种语言,因此我为{{3}创建了自定义过滤器}:
import java.text.Normalizer
import org.apache.lucene.analysis.Token
import org.apache.lucene.analysis.TokenFilter
import org.apache.lucene.analysis.TokenStream
class StripAccentsFilter extends TokenFilter {
StripAccentsFilter(TokenStream input) {
super(input)
}
public final Token next(Token reusableToken) {
assert reusableToken
Token nextToken = input.next(reusableToken)
if (nextToken) {
nextToken.setTermBuffer(Normalizer.normalize(nextToken.termBuffer() as String, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""))
return nextToken
}
return null
}
}
和相应的过滤器提供商:
import org.apache.lucene.analysis.TokenStream
import org.compass.core.config.CompassSettings
import org.compass.core.lucene.engine.analyzer.LuceneAnalyzerTokenFilterProvider
class StripAccentsFilterProvider implements LuceneAnalyzerTokenFilterProvider {
public void configure(CompassSettings paramCompassSettings) {
}
public TokenStream createTokenFilter(TokenStream paramTokenStream) {
return new StripAccentsFilter(paramTokenStream)
}
}
现在您需要做的就是在可搜索插件的配置中注册此过滤器提供程序(grails-app / conf / Searchable.groovy):
compassSettings = [
'compass.engine.analyzer.default.filters': 'stripAccents',
'compass.engine.analyzer.search.filters': 'stripAccents',
'compass.engine.analyzerfilter.stripAccents.type': 'StripAccentsFilterProvider'
]