A Filter that restricts search results to a range of values in a given
field.
| Constructor: |
public RangeFilter(String fieldName,
String lowerTerm,
String upperTerm,
boolean includeLower,
boolean includeUpper) {
this.fieldName = fieldName;
this.lowerTerm = lowerTerm;
this.upperTerm = upperTerm;
this.includeLower = includeLower;
this.includeUpper = includeUpper;
if (null == lowerTerm && null == upperTerm) {
throw new IllegalArgumentException
("At least one value must be non-null");
}
if (includeLower && null == lowerTerm) {
throw new IllegalArgumentException
("The lower bound must be non-null to be inclusive");
}
if (includeUpper && null == upperTerm) {
throw new IllegalArgumentException
("The upper bound must be non-null to be inclusive");
}
}
Parameters:
fieldName - The field this range applies to
lowerTerm - The lower bound on this range
upperTerm - The upper bound on this range
includeLower - Does this range include the lower bound?
includeUpper - Does this range include the upper bound?
Throws:
IllegalArgumentException - if both terms are null or if
lowerTerm is null and includeLower is true (similar for upperTerm
and includeUpper)
|
public RangeFilter(String fieldName,
String lowerTerm,
String upperTerm,
boolean includeLower,
boolean includeUpper,
Collator collator) {
this(fieldName, lowerTerm, upperTerm, includeLower, includeUpper);
this.collator = collator;
}
WARNING: Using this constructor and supplying a non-null
value in the collator parameter will cause every single
index Term in the Field referenced by lowerTerm and/or upperTerm to be
examined. Depending on the number of index Terms in this Field, the
operation could be very slow. Parameters:
lowerTerm - The lower bound on this range
upperTerm - The upper bound on this range
includeLower - Does this range include the lower bound?
includeUpper - Does this range include the upper bound?
collator - The collator to use when determining range inclusion; set
to null to use Unicode code point ordering instead of collation.
Throws:
IllegalArgumentException - if both terms are null or if
lowerTerm is null and includeLower is true (similar for upperTerm
and includeUpper)
|
| Method from org.apache.lucene.search.RangeFilter Detail: |
public static RangeFilter Less(String fieldName,
String upperTerm) {
return new RangeFilter(fieldName, null, upperTerm, false, true);
}
Constructs a filter for field fieldName matching
less than or equal to upperTerm. |
public static RangeFilter More(String fieldName,
String lowerTerm) {
return new RangeFilter(fieldName, lowerTerm, null, true, false);
}
Constructs a filter for field fieldName matching
greater than or equal to lowerTerm. |
public BitSet bits(IndexReader reader) throws IOException {
BitSet bits = new BitSet(reader.maxDoc());
TermEnum enumerator =
(null != lowerTerm && collator == null
? reader.terms(new Term(fieldName, lowerTerm))
: reader.terms(new Term(fieldName)));
try {
if (enumerator.term() == null) {
return bits;
}
TermDocs termDocs = reader.termDocs();
try {
if (collator != null) {
do {
Term term = enumerator.term();
if (term != null && term.field().equals(fieldName)) {
if ((lowerTerm == null
|| (includeLower
? collator.compare(term.text(), lowerTerm) >= 0
: collator.compare(term.text(), lowerTerm) > 0))
&& (upperTerm == null
|| (includeUpper
? collator.compare(term.text(), upperTerm) < = 0
: collator.compare(term.text(), upperTerm) < 0))) {
/* we have a good term, find the docs */
termDocs.seek(enumerator.term());
while (termDocs.next()) {
bits.set(termDocs.doc());
}
}
}
}
while (enumerator.next());
} else { // collator is null - use Unicode code point ordering
boolean checkLower = false;
if (!includeLower) // make adjustments to set to exclusive
checkLower = true;
do {
Term term = enumerator.term();
if (term != null && term.field().equals(fieldName)) {
if (!checkLower || null==lowerTerm || term.text().compareTo(lowerTerm) > 0) {
checkLower = false;
if (upperTerm != null) {
int compare = upperTerm.compareTo(term.text());
/* if beyond the upper term, or is exclusive and
* this is equal to the upper term, break out */
if ((compare < 0) ||
(!includeUpper && compare==0)) {
break;
}
}
/* we have a good term, find the docs */
termDocs.seek(enumerator.term());
while (termDocs.next()) {
bits.set(termDocs.doc());
}
}
} else {
break;
}
}
while (enumerator.next());
}
} finally {
termDocs.close();
}
} finally {
enumerator.close();
}
return bits;
} Deprecated! Use - #getDocIdSet(IndexReader) instead.
Returns a BitSet with true for documents which should be
permitted in search results, and false for those that should
not. |
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RangeFilter)) return false;
RangeFilter other = (RangeFilter) o;
if (!this.fieldName.equals(other.fieldName)
|| this.includeLower != other.includeLower
|| this.includeUpper != other.includeUpper
|| (this.collator != null && ! this.collator.equals(other.collator))
) { return false; }
if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false;
if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false;
return true;
}
Returns true if o is equal to this. |
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
OpenBitSet bits = new OpenBitSet(reader.maxDoc());
TermEnum enumerator =
(null != lowerTerm && collator == null
? reader.terms(new Term(fieldName, lowerTerm))
: reader.terms(new Term(fieldName)));
try {
if (enumerator.term() == null) {
return bits;
}
TermDocs termDocs = reader.termDocs();
try {
if (collator != null) {
do {
Term term = enumerator.term();
if (term != null && term.field().equals(fieldName)) {
if ((lowerTerm == null
|| (includeLower
? collator.compare(term.text(), lowerTerm) >= 0
: collator.compare(term.text(), lowerTerm) > 0))
&& (upperTerm == null
|| (includeUpper
? collator.compare(term.text(), upperTerm) < = 0
: collator.compare(term.text(), upperTerm) < 0))) {
/* we have a good term, find the docs */
termDocs.seek(enumerator.term());
while (termDocs.next()) {
bits.set(termDocs.doc());
}
}
}
}
while (enumerator.next());
} else { // collator is null - use Unicode code point ordering
boolean checkLower = false;
if (!includeLower) // make adjustments to set to exclusive
checkLower = true;
do {
Term term = enumerator.term();
if (term != null && term.field().equals(fieldName)) {
if (!checkLower || null==lowerTerm || term.text().compareTo(lowerTerm) > 0) {
checkLower = false;
if (upperTerm != null) {
int compare = upperTerm.compareTo(term.text());
/* if beyond the upper term, or is exclusive and
* this is equal to the upper term, break out */
if ((compare < 0) ||
(!includeUpper && compare==0)) {
break;
}
}
/* we have a good term, find the docs */
termDocs.seek(enumerator.term());
while (termDocs.next()) {
bits.set(termDocs.doc());
}
}
} else {
break;
}
}
while (enumerator.next());
}
} finally {
termDocs.close();
}
} finally {
enumerator.close();
}
return bits;
}
Returns a DocIdSet with documents that should be
permitted in search results. |
public int hashCode() {
int h = fieldName.hashCode();
h ^= lowerTerm != null ? lowerTerm.hashCode() : 0xB6ECE882;
h = (h < < 1) | (h > > > 31); // rotate to distinguish lower from upper
h ^= (upperTerm != null ? (upperTerm.hashCode()) : 0x91BEC2C2);
h ^= (includeLower ? 0xD484B933 : 0)
^ (includeUpper ? 0x6AE423AC : 0);
h ^= collator != null ? collator.hashCode() : 0;
return h;
}
Returns a hash code value for this object. |
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(fieldName);
buffer.append(":");
buffer.append(includeLower ? "[" : "{");
if (null != lowerTerm) {
buffer.append(lowerTerm);
}
buffer.append("-");
if (null != upperTerm) {
buffer.append(upperTerm);
}
buffer.append(includeUpper ? "]" : "}");
return buffer.toString();
}
|