Example usage for org.apache.lucene.index Term compareTo

List of usage examples for org.apache.lucene.index Term compareTo

Introduction

In this page you can find the example usage for org.apache.lucene.index Term compareTo.

Prototype

@Override
public final int compareTo(Term other) 

Source Link

Document

Compares two terms, returning a negative integer if this term belongs before the argument, zero if this term is equal to the argument, and a positive integer if this term belongs after the argument.

Usage

From source file:com.flaptor.indextank.index.lsi.term.IndexReaderTermMatcher.java

License:Apache License

@Override
public NavigableMap<String, SkippableIterable<DocTermMatch>> getMatches(String field, String termFrom,
        String termTo) {/*www. j a  v  a2s . c  o m*/
    TermEnum terms = null;
    NavigableMap<String, SkippableIterable<DocTermMatch>> result = new TreeMap<String, SkippableIterable<DocTermMatch>>();
    try {
        terms = reader.terms(new Term(field, termFrom));
        Term rightBoundary = new Term(field, termTo);
        int numberOfTerms = 0;

        if (terms.term() != null) {
            do {
                Term term = terms.term();
                if (term.compareTo(rightBoundary) >= 0) {
                    break;
                }

                SkippableIterable<DocTermMatch> docTermIterator = getDocTermIterator(term);
                result.put(term.text(), docTermIterator);

                numberOfTerms++;
                if (numberOfTerms >= 1000) {
                    break;
                }

            } while (terms.next());
        }

        return result;

    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (terms != null) {
            Execute.close(terms);
        }
    }
}

From source file:org.apache.jackrabbit.core.query.lucene.RangeScan.java

License:Apache License

protected boolean termCompare(Term term) {
    int compare = term.compareTo(upper);
    if (compare > 0) {
        endEnum = true;//from w w  w  .ja v  a2 s  .c  o  m
    }
    return compare <= 0;
}

From source file:org.elasticsearch.action.terms.TransportTermsAction.java

License:Apache License

private ExecuteTermResult executeTermSortedStringTerm(ShardTermsRequest request, String indexFieldName,
        Engine.Searcher searcher, @Nullable Pattern regexpPattern, @Nullable FieldMapper fieldMapper,
        @Nullable TermDocs termDocs) throws IOException {
    TObjectIntHashMap<Object> termsFreqs = new TObjectIntHashMap<Object>();
    String from = request.from();
    if (from == null) {
        from = request.prefix();/* w  ww . j av a 2s. com*/
    }
    if (from == null) {
        from = "";
    }
    Term fromTerm = new Term(indexFieldName, from);

    String to = request.to();
    if (to != null && fieldMapper != null) {
        to = fieldMapper.indexedValue(to);
    }
    Term toTerm = to == null ? null : new Term(indexFieldName, to);

    TermEnum termEnum = null;
    try {
        termEnum = searcher.reader().terms(fromTerm);

        // skip the first if we are not inclusive on from
        if (!request.fromInclusive() && request.from() != null) {
            Term term = termEnum.term();
            if (term != null && indexFieldName == term.field() && term.text().equals(request.from())) {
                termEnum.next();
            }
        }

        if (request.sortType() == TermsRequest.SortType.TERM) {
            int counter = 0;
            while (counter < request.size()) {
                Term term = termEnum.term();
                // have we reached the end?
                if (term == null || indexFieldName != term.field()) { // StirngHelper.intern
                    break;
                }
                // convert to actual term text
                if (fieldMapper != null) {
                    // valueAsString returns null indicating that this is not interesting
                    Object termObj = fieldMapper.valueFromTerm(term.text());
                    // if we need to break on this term enumeration, bail
                    if (fieldMapper.shouldBreakTermEnumeration(termObj)) {
                        break;
                    }
                    if (termObj == null) {
                        termEnum.next();
                        continue;
                    }
                }
                // does it match on the prefix?
                if (request.prefix() != null && !term.text().startsWith(request.prefix())) {
                    break;
                }
                // does it match on regexp?
                if (regexpPattern != null && !regexpPattern.matcher(term.text()).matches()) {
                    termEnum.next();
                    continue;
                }
                // check on the to term
                if (toTerm != null) {
                    int toCompareResult = term.compareTo(toTerm);
                    if (toCompareResult > 0 || (toCompareResult == 0 && !request.toInclusive())) {
                        break;
                    }
                }

                int docFreq = termEnum.docFreq();
                if (request.exact()) {
                    if (termDocs == null) {
                        termDocs = searcher.reader().termDocs();
                    }
                    termDocs.seek(termEnum);
                    docFreq = 0;
                    while (termDocs.next()) {
                        if (!searcher.reader().isDeleted(termDocs.doc())) {
                            docFreq++;
                        }
                    }
                }
                termsFreqs.put(term.text(), docFreq);
                if (!termEnum.next()) {
                    break;
                }
                counter++;
            }
        }
    } finally {
        if (termEnum != null) {
            try {
                termEnum.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
    return new ExecuteTermResult(termsFreqs, termDocs);
}