Search in sources :

Example 1 with PrefixTermEnum

use of org.apache.lucene.search.PrefixTermEnum in project step by STEPBible.

the class LuceneUtils method getAllTermsPrefixedWith.

/**
 * Returns all terms starting with a particular prefix
 *
 * @param exact      indicates we want 'exact' matches only
 * @param fieldName  the name of the fields
 * @param searchTerm the search term
 * @return the list of terms matching searchTerm as a prefix
 */
public static TermsAndMaxCount getAllTermsPrefixedWith(final boolean exact, final boolean trackMax, IndexSearcher searcher, final String fieldName, final String searchTerm, final int max) {
    final String lastTerm = getLastTerm(searchTerm);
    if (StringUtils.isBlank(lastTerm)) {
        return getBlankTermsAndMaxCount();
    }
    TermEnum termEnum = null;
    try {
        final Term term = new Term(fieldName, QueryParser.escape(lastTerm.toLowerCase().trim()));
        termEnum = exact ? new SingleTermEnum(searcher.getIndexReader(), term) : new PrefixTermEnum(searcher.getIndexReader(), term);
        int count = 0;
        if (termEnum.term() == null) {
            return getBlankTermsAndMaxCount();
        }
        final Set<String> terms = new HashSet<String>();
        do {
            if (count < max) {
                // when inexact, don't include exact terms
                final String termValue = termEnum.term().text();
                if (!exact && termValue.equalsIgnoreCase(searchTerm)) {
                    // we didn't really find a term after all, since it's the exact same term
                    count--;
                } else {
                    terms.add(termValue);
                }
            }
            count++;
        // we continue round the loop until we've got enough, or in case we're wanting to keep track of the total number
        } while (termEnum.next() && ((count < max) || trackMax || count < MAX_TRACK));
        // finalise and return
        TermsAndMaxCount termsAndMaxCount = new TermsAndMaxCount();
        termsAndMaxCount.setTotalCount(count);
        termsAndMaxCount.setTerms(terms);
        return termsAndMaxCount;
    } catch (IOException ex) {
        throw new StepInternalException(ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(termEnum);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) SingleTermEnum(org.apache.lucene.search.SingleTermEnum) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) TermEnum(org.apache.lucene.index.TermEnum) SingleTermEnum(org.apache.lucene.search.SingleTermEnum) PrefixTermEnum(org.apache.lucene.search.PrefixTermEnum) PrefixTermEnum(org.apache.lucene.search.PrefixTermEnum) HashSet(java.util.HashSet) TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Aggregations

TermsAndMaxCount (com.tyndalehouse.step.core.data.common.TermsAndMaxCount)1 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Term (org.apache.lucene.index.Term)1 TermEnum (org.apache.lucene.index.TermEnum)1 PrefixTermEnum (org.apache.lucene.search.PrefixTermEnum)1 SingleTermEnum (org.apache.lucene.search.SingleTermEnum)1