|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.analysis; |
| 21 | + |
| 22 | +import org.apache.lucene.analysis.Analyzer; |
| 23 | +import org.apache.lucene.analysis.BaseTokenStreamTestCase; |
| 24 | +import org.elasticsearch.Version; |
| 25 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.index.IndexSettings; |
| 28 | +import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; |
| 29 | +import org.elasticsearch.test.IndexSettingsModule; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +import static org.hamcrest.Matchers.containsString; |
| 34 | + |
| 35 | +public class IcuAnalyzerTests extends BaseTokenStreamTestCase { |
| 36 | + |
| 37 | + public void testMixedAlphabetTokenization() throws IOException { |
| 38 | + |
| 39 | + Settings settings = Settings.builder() |
| 40 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 41 | + .build(); |
| 42 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 43 | + |
| 44 | + String input = "안녕은하철도999극장판2.1981년8월8일.일본개봉작1999년재더빙video판"; |
| 45 | + |
| 46 | + AnalysisICUPlugin plugin = new AnalysisICUPlugin(); |
| 47 | + Analyzer analyzer = plugin.getAnalyzers().get("icu_analyzer").get(idxSettings, null, "icu", settings).get(); |
| 48 | + assertAnalyzesTo(analyzer, input, |
| 49 | + new String[]{"안녕은하철도", "999", "극장판", "2.1981", "년", "8", "월", "8", "일", "일본개봉작", "1999", "년재더빙", "video", "판"}); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public void testMiddleDots() throws IOException { |
| 54 | + Settings settings = Settings.builder() |
| 55 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 56 | + .build(); |
| 57 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 58 | + |
| 59 | + String input = "경승지·산악·협곡·해협·곶·심연·폭포·호수·급류"; |
| 60 | + |
| 61 | + Analyzer analyzer = new IcuAnalyzerProvider(idxSettings, null, "icu", settings).get(); |
| 62 | + assertAnalyzesTo(analyzer, input, |
| 63 | + new String[]{"경승지", "산악", "협곡", "해협", "곶", "심연", "폭포", "호수", "급류"}); |
| 64 | + } |
| 65 | + |
| 66 | + public void testUnicodeNumericCharacters() throws IOException { |
| 67 | + |
| 68 | + Settings settings = Settings.builder() |
| 69 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 70 | + .build(); |
| 71 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 72 | + |
| 73 | + String input = "① ② ③ ⑴ ⑵ ⑶ ¼ ⅓ ⅜ ¹ ² ³ ₁ ₂ ₃"; |
| 74 | + |
| 75 | + Analyzer analyzer = new IcuAnalyzerProvider(idxSettings, null, "icu", settings).get(); |
| 76 | + assertAnalyzesTo(analyzer, input, |
| 77 | + new String[]{"1", "2", "3", "1", "2", "3", "1/4", "1/3", "3/8", "1", "2", "3", "1", "2", "3"}); |
| 78 | + } |
| 79 | + |
| 80 | + public void testBadSettings() { |
| 81 | + |
| 82 | + Settings settings = Settings.builder() |
| 83 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 84 | + .put("mode", "wrong") |
| 85 | + .build(); |
| 86 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 87 | + |
| 88 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { |
| 89 | + new IcuAnalyzerProvider(idxSettings, null, "icu", settings); |
| 90 | + }); |
| 91 | + |
| 92 | + assertThat(e.getMessage(), containsString("Unknown mode [wrong] in analyzer [icu], expected one of [compose, decompose]")); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments