Search in sources :

Example 1 with CharFilterFactory

use of org.elasticsearch.index.analysis.CharFilterFactory in project elasticsearch by elastic.

the class TransportAnalyzeAction method createStackedTokenStream.

private static TokenStream createStackedTokenStream(String source, CharFilterFactory[] charFilterFactories, TokenizerFactory tokenizerFactory, TokenFilterFactory[] tokenFilterFactories, int current) {
    Reader reader = new FastStringReader(source);
    for (CharFilterFactory charFilterFactory : charFilterFactories) {
        reader = charFilterFactory.create(reader);
    }
    Tokenizer tokenizer = tokenizerFactory.create();
    tokenizer.setReader(reader);
    TokenStream tokenStream = tokenizer;
    for (int i = 0; i < current; i++) {
        tokenStream = tokenFilterFactories[i].create(tokenStream);
    }
    return tokenStream;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) FastStringReader(org.elasticsearch.common.io.FastStringReader) CharFilterFactory(org.elasticsearch.index.analysis.CharFilterFactory) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) Tokenizer(org.apache.lucene.analysis.Tokenizer)

Example 2 with CharFilterFactory

use of org.elasticsearch.index.analysis.CharFilterFactory in project elasticsearch by elastic.

the class TransportAnalyzeAction method getCharFilterFactories.

private static CharFilterFactory[] getCharFilterFactories(AnalyzeRequest request, IndexSettings indexSettings, AnalysisRegistry analysisRegistry, Environment environment, CharFilterFactory[] charFilterFactories) throws IOException {
    if (request.charFilters() != null && request.charFilters().size() > 0) {
        charFilterFactories = new CharFilterFactory[request.charFilters().size()];
        for (int i = 0; i < request.charFilters().size(); i++) {
            final AnalyzeRequest.NameOrDefinition charFilter = request.charFilters().get(i);
            // parse anonymous settings
            if (charFilter.definition != null) {
                Settings settings = getAnonymousSettings(charFilter.definition);
                String charFilterTypeName = settings.get("type");
                if (charFilterTypeName == null) {
                    throw new IllegalArgumentException("Missing [type] setting for anonymous char filter: " + charFilter.definition);
                }
                AnalysisModule.AnalysisProvider<CharFilterFactory> charFilterFactoryFactory = analysisRegistry.getCharFilterProvider(charFilterTypeName);
                if (charFilterFactoryFactory == null) {
                    throw new IllegalArgumentException("failed to find global char filter under [" + charFilterTypeName + "]");
                }
                // Need to set anonymous "name" of char_filter
                charFilterFactories[i] = charFilterFactoryFactory.get(getNaIndexSettings(settings), environment, "_anonymous_charfilter_[" + i + "]", settings);
            } else {
                AnalysisModule.AnalysisProvider<CharFilterFactory> charFilterFactoryFactory;
                if (indexSettings == null) {
                    charFilterFactoryFactory = analysisRegistry.getCharFilterProvider(charFilter.name);
                    if (charFilterFactoryFactory == null) {
                        throw new IllegalArgumentException("failed to find global char filter under [" + charFilter.name + "]");
                    }
                    charFilterFactories[i] = charFilterFactoryFactory.get(environment, charFilter.name);
                } else {
                    charFilterFactoryFactory = analysisRegistry.getCharFilterProvider(charFilter.name, indexSettings);
                    if (charFilterFactoryFactory == null) {
                        throw new IllegalArgumentException("failed to find char filter under [" + charFilter.name + "]");
                    }
                    charFilterFactories[i] = charFilterFactoryFactory.get(indexSettings, environment, charFilter.name, AnalysisRegistry.getSettingsFromIndexSettings(indexSettings, AnalysisRegistry.INDEX_ANALYSIS_CHAR_FILTER + "." + charFilter.name));
                }
            }
            if (charFilterFactories[i] == null) {
                throw new IllegalArgumentException("failed to find char filter under [" + charFilter.name + "]");
            }
        }
    }
    return charFilterFactories;
}
Also used : CharFilterFactory(org.elasticsearch.index.analysis.CharFilterFactory) AnalysisModule(org.elasticsearch.indices.analysis.AnalysisModule) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 3 with CharFilterFactory

use of org.elasticsearch.index.analysis.CharFilterFactory in project elasticsearch by elastic.

the class TransportAnalyzeAction method detailAnalyze.

private static DetailAnalyzeResponse detailAnalyze(AnalyzeRequest request, Analyzer analyzer, String field) {
    DetailAnalyzeResponse detailResponse;
    final Set<String> includeAttributes = new HashSet<>();
    if (request.attributes() != null) {
        for (String attribute : request.attributes()) {
            includeAttributes.add(attribute.toLowerCase(Locale.ROOT));
        }
    }
    CustomAnalyzer customAnalyzer = null;
    if (analyzer instanceof CustomAnalyzer) {
        customAnalyzer = (CustomAnalyzer) analyzer;
    } else if (analyzer instanceof NamedAnalyzer && ((NamedAnalyzer) analyzer).analyzer() instanceof CustomAnalyzer) {
        customAnalyzer = (CustomAnalyzer) ((NamedAnalyzer) analyzer).analyzer();
    }
    if (customAnalyzer != null) {
        // customAnalyzer = divide charfilter, tokenizer tokenfilters
        CharFilterFactory[] charFilterFactories = customAnalyzer.charFilters();
        TokenizerFactory tokenizerFactory = customAnalyzer.tokenizerFactory();
        TokenFilterFactory[] tokenFilterFactories = customAnalyzer.tokenFilters();
        String[][] charFiltersTexts = new String[charFilterFactories != null ? charFilterFactories.length : 0][request.text().length];
        TokenListCreator[] tokenFiltersTokenListCreator = new TokenListCreator[tokenFilterFactories != null ? tokenFilterFactories.length : 0];
        TokenListCreator tokenizerTokenListCreator = new TokenListCreator();
        for (int textIndex = 0; textIndex < request.text().length; textIndex++) {
            String charFilteredSource = request.text()[textIndex];
            Reader reader = new FastStringReader(charFilteredSource);
            if (charFilterFactories != null) {
                for (int charFilterIndex = 0; charFilterIndex < charFilterFactories.length; charFilterIndex++) {
                    reader = charFilterFactories[charFilterIndex].create(reader);
                    Reader readerForWriteOut = new FastStringReader(charFilteredSource);
                    readerForWriteOut = charFilterFactories[charFilterIndex].create(readerForWriteOut);
                    charFilteredSource = writeCharStream(readerForWriteOut);
                    charFiltersTexts[charFilterIndex][textIndex] = charFilteredSource;
                }
            }
            // analyzing only tokenizer
            Tokenizer tokenizer = tokenizerFactory.create();
            tokenizer.setReader(reader);
            tokenizerTokenListCreator.analyze(tokenizer, customAnalyzer, field, includeAttributes);
            // analyzing each tokenfilter
            if (tokenFilterFactories != null) {
                for (int tokenFilterIndex = 0; tokenFilterIndex < tokenFilterFactories.length; tokenFilterIndex++) {
                    if (tokenFiltersTokenListCreator[tokenFilterIndex] == null) {
                        tokenFiltersTokenListCreator[tokenFilterIndex] = new TokenListCreator();
                    }
                    TokenStream stream = createStackedTokenStream(request.text()[textIndex], charFilterFactories, tokenizerFactory, tokenFilterFactories, tokenFilterIndex + 1);
                    tokenFiltersTokenListCreator[tokenFilterIndex].analyze(stream, customAnalyzer, field, includeAttributes);
                }
            }
        }
        DetailAnalyzeResponse.CharFilteredText[] charFilteredLists = new DetailAnalyzeResponse.CharFilteredText[charFiltersTexts.length];
        if (charFilterFactories != null) {
            for (int charFilterIndex = 0; charFilterIndex < charFiltersTexts.length; charFilterIndex++) {
                charFilteredLists[charFilterIndex] = new DetailAnalyzeResponse.CharFilteredText(charFilterFactories[charFilterIndex].name(), charFiltersTexts[charFilterIndex]);
            }
        }
        DetailAnalyzeResponse.AnalyzeTokenList[] tokenFilterLists = new DetailAnalyzeResponse.AnalyzeTokenList[tokenFiltersTokenListCreator.length];
        if (tokenFilterFactories != null) {
            for (int tokenFilterIndex = 0; tokenFilterIndex < tokenFiltersTokenListCreator.length; tokenFilterIndex++) {
                tokenFilterLists[tokenFilterIndex] = new DetailAnalyzeResponse.AnalyzeTokenList(tokenFilterFactories[tokenFilterIndex].name(), tokenFiltersTokenListCreator[tokenFilterIndex].getArrayTokens());
            }
        }
        detailResponse = new DetailAnalyzeResponse(charFilteredLists, new DetailAnalyzeResponse.AnalyzeTokenList(tokenizerFactory.name(), tokenizerTokenListCreator.getArrayTokens()), tokenFilterLists);
    } else {
        String name;
        if (analyzer instanceof NamedAnalyzer) {
            name = ((NamedAnalyzer) analyzer).name();
        } else {
            name = analyzer.getClass().getName();
        }
        TokenListCreator tokenListCreator = new TokenListCreator();
        for (String text : request.text()) {
            tokenListCreator.analyze(analyzer.tokenStream(field, text), analyzer, field, includeAttributes);
        }
        detailResponse = new DetailAnalyzeResponse(new DetailAnalyzeResponse.AnalyzeTokenList(name, tokenListCreator.getArrayTokens()));
    }
    return detailResponse;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) FastStringReader(org.elasticsearch.common.io.FastStringReader) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) Tokenizer(org.apache.lucene.analysis.Tokenizer) HashSet(java.util.HashSet) TokenizerFactory(org.elasticsearch.index.analysis.TokenizerFactory) CharFilterFactory(org.elasticsearch.index.analysis.CharFilterFactory) TokenFilterFactory(org.elasticsearch.index.analysis.TokenFilterFactory) CustomAnalyzer(org.elasticsearch.index.analysis.CustomAnalyzer)

Aggregations

CharFilterFactory (org.elasticsearch.index.analysis.CharFilterFactory)3 Reader (java.io.Reader)2 TokenStream (org.apache.lucene.analysis.TokenStream)2 Tokenizer (org.apache.lucene.analysis.Tokenizer)2 FastStringReader (org.elasticsearch.common.io.FastStringReader)2 HashSet (java.util.HashSet)1 Settings (org.elasticsearch.common.settings.Settings)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 CustomAnalyzer (org.elasticsearch.index.analysis.CustomAnalyzer)1 NamedAnalyzer (org.elasticsearch.index.analysis.NamedAnalyzer)1 TokenFilterFactory (org.elasticsearch.index.analysis.TokenFilterFactory)1 TokenizerFactory (org.elasticsearch.index.analysis.TokenizerFactory)1 AnalysisModule (org.elasticsearch.indices.analysis.AnalysisModule)1