Search in sources :

Example 1 with IndexAnalyzers

use of org.opensearch.index.analysis.IndexAnalyzers in project OpenSearch by opensearch-project.

the class CommonGramsTokenFilterFactoryTests method testQueryModeCommonGramsAnalysis.

public void testQueryModeCommonGramsAnalysis() throws IOException {
    String json = "/org/opensearch/analysis/common/commongrams_query_mode.json";
    Settings settings = Settings.builder().loadFromStream(json, getClass().getResourceAsStream(json), false).put(Environment.PATH_HOME_SETTING.getKey(), createHome()).build();
    {
        IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers;
        Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer").analyzer();
        String source = "the quick brown is a fox or not";
        String[] expected = new String[] { "the", "quick_brown", "brown_is", "is", "a_fox", "fox_or", "or", "not" };
        assertTokenStreamContents(analyzer.tokenStream("test", source), expected);
    }
    {
        IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers;
        Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer_file").analyzer();
        String source = "the quick brown is a fox or not";
        String[] expected = new String[] { "the", "quick_brown", "brown_is", "is", "a_fox", "fox_or", "or", "not" };
        assertTokenStreamContents(analyzer.tokenStream("test", source), expected);
    }
}
Also used : IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) Analyzer(org.apache.lucene.analysis.Analyzer) Settings(org.opensearch.common.settings.Settings)

Example 2 with IndexAnalyzers

use of org.opensearch.index.analysis.IndexAnalyzers in project OpenSearch by opensearch-project.

the class CompoundAnalysisTests method analyze.

private List<String> analyze(Settings settings, String analyzerName, String text) throws IOException {
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("test", settings);
    AnalysisModule analysisModule = createAnalysisModule(settings);
    IndexAnalyzers indexAnalyzers = analysisModule.getAnalysisRegistry().build(idxSettings);
    Analyzer analyzer = indexAnalyzers.get(analyzerName).analyzer();
    TokenStream stream = analyzer.tokenStream("", text);
    stream.reset();
    CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);
    List<String> terms = new ArrayList<>();
    while (stream.incrementToken()) {
        String tokText = termAtt.toString();
        terms.add(tokText);
    }
    return terms;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) IndexSettings(org.opensearch.index.IndexSettings) ArrayList(java.util.ArrayList) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) AnalysisModule(org.opensearch.indices.analysis.AnalysisModule) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 3 with IndexAnalyzers

use of org.opensearch.index.analysis.IndexAnalyzers in project OpenSearch by opensearch-project.

the class StemmerTokenFilterFactoryTests method testEnglishFilterFactory.

public void testEnglishFilterFactory() throws IOException {
    int iters = scaledRandomIntBetween(20, 100);
    for (int i = 0; i < iters; i++) {
        Version v = VersionUtils.randomVersion(random());
        Settings settings = Settings.builder().put("index.analysis.filter.my_english.type", "stemmer").put("index.analysis.filter.my_english.language", "english").put("index.analysis.analyzer.my_english.tokenizer", "whitespace").put("index.analysis.analyzer.my_english.filter", "my_english").put(SETTING_VERSION_CREATED, v).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
        OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, PLUGIN);
        TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_english");
        assertThat(tokenFilter, instanceOf(StemmerTokenFilterFactory.class));
        Tokenizer tokenizer = new WhitespaceTokenizer();
        tokenizer.setReader(new StringReader("foo bar"));
        TokenStream create = tokenFilter.create(tokenizer);
        IndexAnalyzers indexAnalyzers = analysis.indexAnalyzers;
        NamedAnalyzer analyzer = indexAnalyzers.get("my_english");
        assertThat(create, instanceOf(PorterStemFilter.class));
        assertAnalyzesTo(analyzer, "consolingly", new String[] { "consolingli" });
    }
}
Also used : WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) TokenStream(org.apache.lucene.analysis.TokenStream) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) PorterStemFilter(org.apache.lucene.analysis.en.PorterStemFilter) TokenFilterFactory(org.opensearch.index.analysis.TokenFilterFactory) Version(org.opensearch.Version) StringReader(java.io.StringReader) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) Tokenizer(org.apache.lucene.analysis.Tokenizer) Settings(org.opensearch.common.settings.Settings)

Example 4 with IndexAnalyzers

use of org.opensearch.index.analysis.IndexAnalyzers in project OpenSearch by opensearch-project.

the class PatternCaptureTokenFilterTests method testPatternCaptureTokenFilter.

public void testPatternCaptureTokenFilter() throws Exception {
    String json = "/org/opensearch/analysis/common/pattern_capture.json";
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).loadFromStream(json, getClass().getResourceAsStream(json), false).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);
    IndexAnalyzers indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers;
    NamedAnalyzer analyzer1 = indexAnalyzers.get("single");
    assertTokenStreamContents(analyzer1.tokenStream("test", "foobarbaz"), new String[] { "foobarbaz", "foobar", "foo" });
    NamedAnalyzer analyzer2 = indexAnalyzers.get("multi");
    assertTokenStreamContents(analyzer2.tokenStream("test", "abc123def"), new String[] { "abc123def", "abc", "123", "def" });
    NamedAnalyzer analyzer3 = indexAnalyzers.get("preserve");
    assertTokenStreamContents(analyzer3.tokenStream("test", "foobarbaz"), new String[] { "foobar", "foo" });
}
Also used : NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexSettings(org.opensearch.index.IndexSettings) Settings(org.opensearch.common.settings.Settings)

Example 5 with IndexAnalyzers

use of org.opensearch.index.analysis.IndexAnalyzers in project OpenSearch by opensearch-project.

the class SearchAsYouTypeFieldMapperTests method createIndexAnalyzers.

@Override
protected IndexAnalyzers createIndexAnalyzers(IndexSettings indexSettings) {
    NamedAnalyzer dflt = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer(), TextFieldMapper.Defaults.POSITION_INCREMENT_GAP);
    NamedAnalyzer standard = new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer());
    NamedAnalyzer keyword = new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer());
    NamedAnalyzer simple = new NamedAnalyzer("simple", AnalyzerScope.INDEX, new SimpleAnalyzer());
    NamedAnalyzer whitespace = new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer());
    return new IndexAnalyzers(org.opensearch.common.collect.Map.of("default", dflt, "standard", standard, "keyword", keyword, "simple", simple, "whitespace", whitespace), org.opensearch.common.collect.Map.of(), org.opensearch.common.collect.Map.of());
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) SimpleAnalyzer(org.apache.lucene.analysis.core.SimpleAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers)

Aggregations

IndexAnalyzers (org.opensearch.index.analysis.IndexAnalyzers)33 NamedAnalyzer (org.opensearch.index.analysis.NamedAnalyzer)20 Settings (org.opensearch.common.settings.Settings)15 IndexSettings (org.opensearch.index.IndexSettings)12 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)10 HashMap (java.util.HashMap)9 TokenStream (org.apache.lucene.analysis.TokenStream)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Version (org.opensearch.Version)7 TokenFilterFactory (org.opensearch.index.analysis.TokenFilterFactory)7 Analyzer (org.apache.lucene.analysis.Analyzer)6 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)5 AnalysisRegistry (org.opensearch.index.analysis.AnalysisRegistry)5 AnalysisModule (org.opensearch.indices.analysis.AnalysisModule)5 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 Tokenizer (org.apache.lucene.analysis.Tokenizer)4 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)4 CustomAnalyzer (org.opensearch.index.analysis.CustomAnalyzer)4 StandardTokenizerFactory (org.opensearch.index.analysis.StandardTokenizerFactory)4