Example usage for org.apache.lucene.search PrefixQuery toAutomaton

List of usage examples for org.apache.lucene.search PrefixQuery toAutomaton

Introduction

In this page you can find the example usage for org.apache.lucene.search PrefixQuery toAutomaton.

Prototype

public static Automaton toAutomaton(BytesRef prefix) 

Source Link

Document

Build an automaton accepting all terms with the specified prefix.

Usage

From source file:org.opengrok.suggest.query.SuggesterRangeQuery.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  w  ww.  j ava2  s  .c  om
public TermsEnum getTermsEnumForSuggestions(final Terms terms) {
    if (terms == null) {
        return TermsEnum.EMPTY;
    }

    BytesRef prefix = getPrefix();
    if (prefix != null) {
        Automaton prefixAutomaton = PrefixQuery.toAutomaton(prefix);

        Automaton finalAutomaton;
        if (suggestPosition == SuggestPosition.LOWER) {
            Automaton binaryInt = Automata.makeBinaryInterval(getLowerTerm(), includesLower(), getUpperTerm(),
                    includesUpper());

            finalAutomaton = Operations.intersection(binaryInt, prefixAutomaton);
        } else {
            Automaton binaryInt = Automata.makeBinaryInterval(null, true, getLowerTerm(), !includesLower());

            finalAutomaton = Operations.minus(prefixAutomaton, binaryInt, Integer.MIN_VALUE);
        }

        CompiledAutomaton compiledAutomaton = new CompiledAutomaton(finalAutomaton);
        try {
            return compiledAutomaton.getTermsEnum(terms);
        } catch (IOException e) {
            logger.log(Level.WARNING, "Could not compile automaton for range suggestions", e);
        }
    }

    return TermsEnum.EMPTY;
}

From source file:org.pageseeder.flint.lucene.search.Terms.java

License:Apache License

/**
 * Loads all the prefix terms in the list of terms given the reader.
 *
 * @param reader  Index reader to use./* w  ww  .ja v a 2s .  co m*/
 * @param values  The list of values to load.
 * @param term    The term to use.
 *
 * @throws IOException If an error is thrown by the prefix term enumeration.
 */
public static void prefix(IndexReader reader, List<String> values, Term term) throws IOException {
    Fields fields = MultiFields.getFields(reader);
    org.apache.lucene.index.Terms terms = fields == null ? null : fields.terms(term.field());
    if (terms == null)
        return;
    TermsEnum prefixes = terms.intersect(new CompiledAutomaton(PrefixQuery.toAutomaton(term.bytes())), null);
    BytesRef val;
    while ((val = prefixes.next()) != null) {
        values.add(val.utf8ToString());
    }
}

From source file:org.pageseeder.flint.lucene.search.Terms.java

License:Apache License

/**
 * Loads all the prefix terms in the list of terms given the reader.
 *
 * @param reader  Index reader to use.//w  w w . j  av  a 2 s  . c o m
 * @param bucket  Where to store the terms.
 * @param term    The term to use.
 *
 * @throws IOException If an error is thrown by the prefix term enumeration.
 */
public static void prefix(IndexReader reader, Bucket<Term> bucket, Term term) throws IOException {
    Fields fields = MultiFields.getFields(reader);
    org.apache.lucene.index.Terms terms = fields == null ? null : fields.terms(term.field());
    if (terms == null)
        return;
    TermsEnum prefixes = terms.intersect(new CompiledAutomaton(PrefixQuery.toAutomaton(term.bytes())),
            term.bytes());
    BytesRef val;
    while ((val = prefixes.next()) != null) {
        Term t = new Term(term.field(), BytesRef.deepCopyOf(val));
        bucket.add(t, reader.docFreq(t));
    }
}