Grails 中的重音不敏感搜索

发布于 2024-12-06 00:16:56 字数 99 浏览 0 评论 0原文

如何使用 Grails Searchable Plugin 不区分重音进行全文搜索?

How to make full text search using Grails Searchable Plugin accent insensitive ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2024-12-13 00:16:56

我在 Peter Ledbrook 的帖子,但是需要付出一些努力:

由于最新的可搜索插件使用 Lucene 2.4.1,它不包含 ASCIIFoldingFilter(自 2.9.0 起可用)和 ISOLatin1AccentFilter 不支持许多语言我为 剥离重音



    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' 
]

I have solved this problem with help of Peter Ledbrook's post, however some effort was needed:

Since latest searchable plugin uses Lucene 2.4.1 which does not contain ASCIIFoldingFilter (available since 2.9.0) and ISOLatin1AccentFilter doesn't support many languages I have created custom filter for stripping accents:



    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
        }
    }

and corresponding filter provider:



    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)
        }

    }

Now all you need to do is to register this filter provider in configuration of searchable plugin (grails-app/conf/Searchable.groovy):

compassSettings = [
    'compass.engine.analyzer.default.filters': 'stripAccents',
    'compass.engine.analyzer.search.filters': 'stripAccents',
    'compass.engine.analyzerfilter.stripAccents.type': 'StripAccentsFilterProvider' 
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文