List of usage examples for org.apache.lucene.util.automaton CompiledAutomaton getTermsEnum
public TermsEnum getTermsEnum(Terms terms) throws IOException
From source file:com.github.flaxsearch.resources.TermsResource.java
License:Apache License
private TermsEnum getTermsEnum(Terms terms, String filter) throws IOException { if (filter == null) return terms.iterator(); CompiledAutomaton automaton = new CompiledAutomaton(new RegExp(filter).toAutomaton()); return automaton.getTermsEnum(terms); }
From source file:org.exist.indexing.lucene.XMLToQuery.java
License:Open Source License
private Term[] expandTerms(String field, String queryStr) throws XPathException { List<Term> termList = new ArrayList<>(8); Automaton automaton = WildcardQuery.toAutomaton(new Term(field, queryStr)); CompiledAutomaton compiled = new CompiledAutomaton(automaton); IndexReader reader = null;/* w ww .j a va 2 s.c o m*/ try { reader = index.getReader(); for (AtomicReaderContext atomic : reader.leaves()) { Terms terms = atomic.reader().terms(field); if (terms != null) { TermsEnum termsEnum = compiled.getTermsEnum(terms); BytesRef data = termsEnum.next(); while (data != null) { String term = data.utf8ToString(); termList.add(new Term(field, term)); data = termsEnum.next(); } } } } catch (IOException e) { throw new XPathException("Lucene index error while creating query: " + e.getMessage(), e); } finally { index.releaseReader(reader); } Term[] matchingTerms = new Term[termList.size()]; return termList.toArray(matchingTerms); }
From source file:org.opengrok.suggest.query.SuggesterRangeQuery.java
License:Open Source License
/** {@inheritDoc} */ @Override// ww w. j av a 2 s. co m 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.zenoss.zep.index.impl.lucene.LuceneQueryBuilder.java
License:Open Source License
private static Term[] getMatchingTerms(String fieldName, IndexReader reader, String value) throws ZepException { // Don't search for matches if text doesn't contain wildcards if (value.indexOf('*') == -1 && value.indexOf('?') == -1) return new Term[] { new Term(fieldName, value) }; logger.debug("getMatchingTerms: field={}, value={}", fieldName, value); List<Term> matches = new ArrayList<Term>(); Automaton automaton = WildcardQuery.toAutomaton(new Term(fieldName, value)); CompiledAutomaton compiled = new CompiledAutomaton(automaton); try {//from ww w. ja va 2 s.c o m Terms terms = SlowCompositeReaderWrapper.wrap(reader).terms(fieldName); TermsEnum wildcardTermEnum = compiled.getTermsEnum(terms); BytesRef match; while (wildcardTermEnum.next() != null) { match = wildcardTermEnum.term(); logger.debug("Match: {}", match); matches.add(new Term(fieldName, match.utf8ToString())); } return matches.toArray(new Term[matches.size()]); } catch (IOException e) { throw new ZepException(e.getLocalizedMessage(), e); } }