3

我想找到与 Java 语言环境对应的 Lucene 分析器。例如,Locale.ENGLISH 将映射到 org.apache.lucene.analysis.en.EnglishAnalyzer。

某处有自动映射吗?

4

1 回答 1

1

这不是开箱即用的。请看下面我的做法。

public final class LocaleAwareAnalyzer extends AnalyzerWrapper {

private static final Logger LOG = LoggerFactory.getLogger(LocaleAwareAnalyzer.class);

private final Analyzer defaultAnalyzer;
private final Map<String, Analyzer> perLocaleAnalyzer = perLocaleAnalyzers();

public LocaleAwareAnalyzer(final Analyzer defaultAnalyzer) {
    this.defaultAnalyzer = Precondition.notNull("defaultAnalyzer", defaultAnalyzer);
}

@Override
protected Analyzer getWrappedAnalyzer(final String fieldName) {
    if (fieldName == null) {
        return defaultAnalyzer;
    }
    final int n = fieldName.indexOf('_');
    if (n >= 0) {
        // Unfortunately CharArrayMap does not offer get(CharSequence, start, end)
        final String locale = fieldName.substring(n + 1);
        final Analyzer a = perLocaleAnalyzer.get(locale);
        if (a != null) {
            return a;
        }
        LOG.warn("No Analyzer for Locale '%s', using default", locale);
    }
    return defaultAnalyzer;
}

@Override
protected TokenStreamComponents wrapComponents(final String fieldName, 
    final TokenStreamComponents components) {

    return components;
}

private static Map<String, Analyzer> perLocaleAnalyzers() {
    final Map<String, Analyzer> m = new HashMap<>();
    m.put("en", new EnglishAnalyzer(Version.LUCENE_43));
    m.put("es", new SpanishAnalyzer(Version.LUCENE_43));
    m.put("de", new GermanAnalyzer(Version.LUCENE_43));
    m.put("fr", new FrenchAnalyzer(Version.LUCENE_43));
    // ... etc
    return m;
}

}
于 2013-07-23T06:27:54.343 回答