Search in sources :

Example 1 with FastStringReader

use of org.elasticsearch.common.io.FastStringReader in project elasticsearch by elastic.

the class MustacheScriptEngineService method compile.

/**
     * Compile a template string to (in this case) a Mustache object than can
     * later be re-used for execution to fill in missing parameter values.
     *
     * @param templateSource a string representing the template to compile.
     * @return a compiled template object for later execution.
     * */
@Override
public Object compile(String templateName, String templateSource, Map<String, String> params) {
    final MustacheFactory factory = createMustacheFactory(params);
    Reader reader = new FastStringReader(templateSource);
    return factory.compile(reader, "query-template");
}
Also used : FastStringReader(org.elasticsearch.common.io.FastStringReader) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 2 with FastStringReader

use of org.elasticsearch.common.io.FastStringReader in project elasticsearch by elastic.

the class MoreLikeThisQuery method createQuery.

private Query createQuery(XMoreLikeThis mlt) throws IOException {
    BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder();
    if (this.likeFields != null) {
        Query mltQuery = mlt.like(this.likeFields);
        mltQuery = Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
        bqBuilder.add(mltQuery, BooleanClause.Occur.SHOULD);
    }
    if (this.likeText != null) {
        Reader[] readers = new Reader[likeText.length];
        for (int i = 0; i < readers.length; i++) {
            readers[i] = new FastStringReader(likeText[i]);
        }
        //LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field)
        Query mltQuery = mlt.like(moreLikeFields[0], readers);
        mltQuery = Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
        bqBuilder.add(mltQuery, BooleanClause.Occur.SHOULD);
    }
    return bqBuilder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) FastStringReader(org.elasticsearch.common.io.FastStringReader) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) IndexReader(org.apache.lucene.index.IndexReader)

Example 3 with FastStringReader

use of org.elasticsearch.common.io.FastStringReader in project elasticsearch by elastic.

the class XMoreLikeThis method retrieveTerms.

/**
     * Find words for a more-like-this query former.
     *
     * @param docNum the id of the lucene document from which to find terms
     */
private PriorityQueue<ScoreTerm> retrieveTerms(int docNum) throws IOException {
    Map<String, Int> termFreqMap = new HashMap<>();
    for (String fieldName : fieldNames) {
        final Fields vectors = ir.getTermVectors(docNum);
        final Terms vector;
        if (vectors != null) {
            vector = vectors.terms(fieldName);
        } else {
            vector = null;
        }
        // field does not store term vector info
        if (vector == null) {
            Document d = ir.document(docNum);
            IndexableField[] fields = d.getFields(fieldName);
            for (IndexableField field : fields) {
                final String stringValue = field.stringValue();
                if (stringValue != null) {
                    addTermFrequencies(new FastStringReader(stringValue), termFreqMap, fieldName);
                }
            }
        } else {
            addTermFrequencies(termFreqMap, vector, fieldName);
        }
    }
    return createQueue(termFreqMap);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) FastStringReader(org.elasticsearch.common.io.FastStringReader) HashMap(java.util.HashMap) Terms(org.apache.lucene.index.Terms) Document(org.apache.lucene.document.Document)

Example 4 with FastStringReader

use of org.elasticsearch.common.io.FastStringReader 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 5 with FastStringReader

use of org.elasticsearch.common.io.FastStringReader 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

FastStringReader (org.elasticsearch.common.io.FastStringReader)5 Reader (java.io.Reader)4 TokenStream (org.apache.lucene.analysis.TokenStream)2 Tokenizer (org.apache.lucene.analysis.Tokenizer)2 CharFilterFactory (org.elasticsearch.index.analysis.CharFilterFactory)2 MustacheFactory (com.github.mustachejava.MustacheFactory)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Document (org.apache.lucene.document.Document)1 Fields (org.apache.lucene.index.Fields)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexableField (org.apache.lucene.index.IndexableField)1 MultiFields (org.apache.lucene.index.MultiFields)1 Terms (org.apache.lucene.index.Terms)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 Query (org.apache.lucene.search.Query)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