All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.search.TermRangeTermsEnum Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.apache.lucene.search;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.Comparator;

import org.apache.lucene.index.FilteredTermsEnum;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.util.BytesRef;

/**
 * Subclass of FilteredTermEnum for enumerating all terms that match the
 * specified range parameters.
 * 

Term enumerations are always ordered by * {@link #getComparator}. Each term in the enumeration is * greater than all that precede it.

*/ public class TermRangeTermsEnum extends FilteredTermsEnum { final private boolean includeLower; final private boolean includeUpper; final private BytesRef lowerBytesRef; final private BytesRef upperBytesRef; private final Comparator termComp; /** * Enumerates all terms greater/equal than lowerTerm * but less/equal than upperTerm. * * If an endpoint is null, it is said to be "open". Either or both * endpoints may be open. Open endpoints may not be exclusive * (you can't select all but the first or last term without * explicitly specifying the term to exclude.) * * @param tenum * TermsEnum to filter * @param lowerTerm * The term text at the lower end of the range * @param upperTerm * The term text at the upper end of the range * @param includeLower * If true, the lowerTerm is included in the range. * @param includeUpper * If true, the upperTerm is included in the range. */ public TermRangeTermsEnum(TermsEnum tenum, BytesRef lowerTerm, BytesRef upperTerm, boolean includeLower, boolean includeUpper) { super(tenum); // do a little bit of normalization... // open ended range queries should always be inclusive. if (lowerTerm == null) { this.lowerBytesRef = new BytesRef(); this.includeLower = true; } else { this.lowerBytesRef = lowerTerm; this.includeLower = includeLower; } if (upperTerm == null) { this.includeUpper = true; upperBytesRef = null; } else { this.includeUpper = includeUpper; upperBytesRef = upperTerm; } setInitialSeekTerm(lowerBytesRef); termComp = getComparator(); } @Override protected AcceptStatus accept(BytesRef term) { if (!this.includeLower && term.equals(lowerBytesRef)) return AcceptStatus.NO; // Use this field's default sort ordering if (upperBytesRef != null) { final int cmp = termComp.compare(upperBytesRef, term); /* * if beyond the upper term, or is exclusive and this is equal to * the upper term, break out */ if ((cmp < 0) || (!includeUpper && cmp==0)) { return AcceptStatus.END; } } return AcceptStatus.YES; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy