| 
 | 1 | +/*  | 
 | 2 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 3 | + *  | 
 | 4 | + * The OpenSearch Contributors require contributions made to  | 
 | 5 | + * this file be licensed under the Apache-2.0 license or a  | 
 | 6 | + * compatible open source license.  | 
 | 7 | + */  | 
 | 8 | + | 
 | 9 | +/*  | 
 | 10 | + * Licensed to Elasticsearch under one or more contributor  | 
 | 11 | + * license agreements. See the NOTICE file distributed with  | 
 | 12 | + * this work for additional information regarding copyright  | 
 | 13 | + * ownership. Elasticsearch licenses this file to you under  | 
 | 14 | + * the Apache License, Version 2.0 (the "License"); you may  | 
 | 15 | + * not use this file except in compliance with the License.  | 
 | 16 | + * You may obtain a copy of the License at  | 
 | 17 | + *  | 
 | 18 | + *    http://www.apache.org/licenses/LICENSE-2.0  | 
 | 19 | + *  | 
 | 20 | + * Unless required by applicable law or agreed to in writing,  | 
 | 21 | + * software distributed under the License is distributed on an  | 
 | 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  | 
 | 23 | + * KIND, either express or implied.  See the License for the  | 
 | 24 | + * specific language governing permissions and limitations  | 
 | 25 | + * under the License.  | 
 | 26 | + */  | 
 | 27 | + | 
 | 28 | +/*  | 
 | 29 | + * Modifications Copyright OpenSearch Contributors. See  | 
 | 30 | + * GitHub history for details.  | 
 | 31 | + */  | 
 | 32 | + | 
 | 33 | +package org.opensearch.analysis.common;  | 
 | 34 | + | 
 | 35 | +import org.apache.lucene.util.BytesRef;  | 
 | 36 | +import org.opensearch.common.settings.Settings;  | 
 | 37 | +import org.opensearch.env.Environment;  | 
 | 38 | +import org.opensearch.index.analysis.AnalysisTestsHelper;  | 
 | 39 | +import org.opensearch.index.analysis.NamedAnalyzer;  | 
 | 40 | +import org.opensearch.test.OpenSearchTestCase;  | 
 | 41 | +import org.opensearch.test.OpenSearchTokenStreamTestCase;  | 
 | 42 | + | 
 | 43 | +import java.io.IOException;  | 
 | 44 | + | 
 | 45 | +public class TruncateTokenFilterTests extends OpenSearchTokenStreamTestCase {  | 
 | 46 | + | 
 | 47 | +    public void testFilter() throws IOException {  | 
 | 48 | +        Settings settings = Settings.builder()  | 
 | 49 | +            .put("index.analysis.filter.truncate.type", "truncate")  | 
 | 50 | +            .put("index.analysis.filter.truncate.length", 3)  | 
 | 51 | +            .put("index.analysis.analyzer.my_analyzer.type", "custom")  | 
 | 52 | +            .put("index.analysis.analyzer.my_analyzer.tokenizer", "whitespace")  | 
 | 53 | +            .putList("index.analysis.analyzer.my_analyzer.filter", "truncate")  | 
 | 54 | +            .putList("index.analysis.normalizer.my_normalizer.filter", "truncate")  | 
 | 55 | +            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())  | 
 | 56 | +            .build();  | 
 | 57 | +        OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(  | 
 | 58 | +            settings,  | 
 | 59 | +            new CommonAnalysisModulePlugin()  | 
 | 60 | +        );  | 
 | 61 | +        NamedAnalyzer analyzer = analysis.indexAnalyzers.get("my_analyzer");  | 
 | 62 | +        assertNotNull(analyzer);  | 
 | 63 | + | 
 | 64 | +        assertTokenStreamContents(analyzer.tokenStream("foo", "a bb ccc dddd"), new String[] { "a", "bb", "ccc", "ddd" });  | 
 | 65 | +    }  | 
 | 66 | + | 
 | 67 | +    public void testNormalizer() throws IOException {  | 
 | 68 | +        Settings settings = Settings.builder()  | 
 | 69 | +            .put("index.analysis.filter.truncate.type", "truncate")  | 
 | 70 | +            .put("index.analysis.filter.truncate.length", 3)  | 
 | 71 | +            .put("index.analysis.normalizer.my_normalizer.type", "custom")  | 
 | 72 | +            .putList("index.analysis.normalizer.my_normalizer.filter", "truncate")  | 
 | 73 | +            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())  | 
 | 74 | +            .build();  | 
 | 75 | +        OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(  | 
 | 76 | +            settings,  | 
 | 77 | +            new CommonAnalysisModulePlugin()  | 
 | 78 | +        );  | 
 | 79 | +        assertNull(analysis.indexAnalyzers.get("my_normalizer"));  | 
 | 80 | +        NamedAnalyzer normalizer = analysis.indexAnalyzers.getNormalizer("my_normalizer");  | 
 | 81 | +        assertNotNull(normalizer);  | 
 | 82 | +        assertEquals("my_normalizer", normalizer.name());  | 
 | 83 | +        assertEquals(new BytesRef("a"), normalizer.normalize("foo", "a"));  | 
 | 84 | +        assertEquals(new BytesRef("bb"), normalizer.normalize("foo", "bb"));  | 
 | 85 | +        assertEquals(new BytesRef("ccc"), normalizer.normalize("foo", "ccc"));  | 
 | 86 | +        assertEquals(new BytesRef("ddd"), normalizer.normalize("foo", "dddd"));  | 
 | 87 | +    }  | 
 | 88 | +}  | 
0 commit comments