|
| 1 | +package org.elasticsearch.index.analysis; |
| 2 | + |
| 3 | +import org.apache.lucene.analysis.Analyzer; |
| 4 | +import org.apache.lucene.analysis.TokenStream; |
| 5 | +import org.apache.lucene.analysis.core.KeywordTokenizer; |
| 6 | +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; |
| 7 | +import org.elasticsearch.common.inject.Injector; |
| 8 | +import org.elasticsearch.common.inject.ModulesBuilder; |
| 9 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 10 | +import org.elasticsearch.common.settings.Settings; |
| 11 | +import org.elasticsearch.common.settings.SettingsModule; |
| 12 | +import org.elasticsearch.env.Environment; |
| 13 | +import org.elasticsearch.env.EnvironmentModule; |
| 14 | +import org.elasticsearch.index.Index; |
| 15 | +import org.elasticsearch.index.IndexNameModule; |
| 16 | +import org.elasticsearch.index.analysis.pl.PolishAnalysisBinderProcessor; |
| 17 | +import org.elasticsearch.index.settings.IndexSettingsModule; |
| 18 | +import org.elasticsearch.indices.analysis.IndicesAnalysisModule; |
| 19 | +import org.elasticsearch.indices.analysis.IndicesAnalysisService; |
| 20 | +import org.elasticsearch.test.ElasticsearchTestCase; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.StringReader; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | + |
| 28 | +public class SimplePolishTokenFilterTests extends ElasticsearchTestCase { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testBasicUsage() throws Exception { |
| 32 | + testToken("kwiaty", "kwć"); |
| 33 | + testToken("canona", "ć"); |
| 34 | + testToken("wirtualna", "wirtualny"); |
| 35 | + testToken("polska", "polski"); |
| 36 | + |
| 37 | + testAnalyzer("wirtualna polska", "wirtualny", "polski"); |
| 38 | + } |
| 39 | + |
| 40 | + private void testToken(String source, String expected) throws IOException { |
| 41 | + Index index = new Index("test"); |
| 42 | + Settings settings = ImmutableSettings.settingsBuilder() |
| 43 | + .put("index.analysis.filter.myStemmer.type", "polish_stem") |
| 44 | + .build(); |
| 45 | + AnalysisService analysisService = createAnalysisService(index, settings); |
| 46 | + |
| 47 | + TokenFilterFactory filterFactory = analysisService.tokenFilter("myStemmer"); |
| 48 | + |
| 49 | + TokenStream ts = filterFactory.create(new KeywordTokenizer(new StringReader(source))); |
| 50 | + |
| 51 | + CharTermAttribute term1 = ts.addAttribute(CharTermAttribute.class); |
| 52 | + ts.reset(); |
| 53 | + assertThat(ts.incrementToken(), equalTo(true)); |
| 54 | + |
| 55 | + assertThat(term1.toString(), equalTo(expected)); |
| 56 | + } |
| 57 | + |
| 58 | + private void testAnalyzer(String source, String... expected_terms) throws IOException { |
| 59 | + Index index = new Index("test"); |
| 60 | + Settings settings = ImmutableSettings.settingsBuilder().build(); |
| 61 | + AnalysisService analysisService = createAnalysisService(index, settings); |
| 62 | + |
| 63 | + Analyzer analyzer = analysisService.analyzer("polish").analyzer(); |
| 64 | + |
| 65 | + TokenStream ts = analyzer.tokenStream("test", source); |
| 66 | + |
| 67 | + CharTermAttribute term1 = ts.addAttribute(CharTermAttribute.class); |
| 68 | + ts.reset(); |
| 69 | + |
| 70 | + for (String expected : expected_terms) { |
| 71 | + assertThat(ts.incrementToken(), equalTo(true)); |
| 72 | + assertThat(term1.toString(), equalTo(expected)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private AnalysisService createAnalysisService(Index index, Settings settings) { |
| 77 | + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); |
| 78 | + Injector injector = new ModulesBuilder().add( |
| 79 | + new IndexSettingsModule(index, settings), |
| 80 | + new IndexNameModule(index), |
| 81 | + new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)).addProcessor(new PolishAnalysisBinderProcessor())) |
| 82 | + .createChildInjector(parentInjector); |
| 83 | + |
| 84 | + return injector.getInstance(AnalysisService.class); |
| 85 | + } |
| 86 | +} |
0 commit comments