LUCENE-1530: Support inclusive/exclusive for TrieRangeQuery/-Filter, remove default trie variant setters/getters

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@738109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-01-27 15:15:24 +00:00
parent c6f6f01643
commit 153acfd064
4 changed files with 129 additions and 121 deletions

View File

@ -39,35 +39,44 @@ public final class TrieRangeFilter extends Filter {
/**
* Universal constructor (expert use only): Uses already trie-converted min/max values.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeFilter(final String field, final String min, final String max, final TrieUtils variant) {
public TrieRangeFilter(final String field, String min, String max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
if (min==null && max==null) throw new IllegalArgumentException("The min and max values cannot be both null.");
this.trieVariant=variant;
this.field=field.intern();
// just for toString()
this.minUnconverted=min;
this.maxUnconverted=max;
this.min=(min==null) ? trieVariant.TRIE_CODED_NUMERIC_MIN : min;
this.max=(max==null) ? trieVariant.TRIE_CODED_NUMERIC_MAX : max;
this.field=field.intern();
this.minInclusive=minInclusive;
this.maxInclusive=maxInclusive;
// encode bounds
this.min=(min==null) ? trieVariant.TRIE_CODED_NUMERIC_MIN : (
minInclusive ? min : variant.incrementTrieCoded(min)
);
this.max=(max==null) ? trieVariant.TRIE_CODED_NUMERIC_MAX : (
maxInclusive ? max : variant.decrementTrieCoded(max)
);
}
/**
* Universal constructor (expert use only): Uses already trie-converted min/max values.
* Generates a trie filter using the supplied field with range bounds in numeric form (double).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie package returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeFilter(final String field, final String min, final String max) {
this(field,min,max,TrieUtils.getDefaultTrieVariant());
}
/**
* Generates a trie query using the supplied field with range bounds in numeric form (double).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeFilter(final String field, final Double min, final Double max, final TrieUtils variant) {
public TrieRangeFilter(final String field, final Double min, final Double max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
this(
field,
(min==null) ? null : variant.doubleToTrieCoded(min.doubleValue()),
(max==null) ? null : variant.doubleToTrieCoded(max.doubleValue()),
minInclusive,
maxInclusive,
variant
);
this.minUnconverted=min;
@ -75,23 +84,20 @@ public final class TrieRangeFilter extends Filter {
}
/**
* Generates a trie query using the supplied field with range bounds in numeric form (double).
* Generates a trie filter using the supplied field with range bounds in date/time form.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeFilter(final String field, final Double min, final Double max) {
this(field,min,max,TrieUtils.getDefaultTrieVariant());
}
/**
* Generates a trie query using the supplied field with range bounds in date/time form.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeFilter(final String field, final Date min, final Date max, final TrieUtils variant) {
public TrieRangeFilter(final String field, final Date min, final Date max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
this(
field,
(min==null) ? null : variant.dateToTrieCoded(min),
(max==null) ? null : variant.dateToTrieCoded(max),
minInclusive,
maxInclusive,
variant
);
this.minUnconverted=min;
@ -99,38 +105,26 @@ public final class TrieRangeFilter extends Filter {
}
/**
* Generates a trie query using the supplied field with range bounds in date/time form.
* Generates a trie filter using the supplied field with range bounds in integer form (long).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeFilter(final String field, final Date min, final Date max) {
this(field,min,max,TrieUtils.getDefaultTrieVariant());
}
/**
* Generates a trie query using the supplied field with range bounds in integer form (long).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeFilter(final String field, final Long min, final Long max, final TrieUtils variant) {
public TrieRangeFilter(final String field, final Long min, final Long max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
this(
field,
(min==null) ? null : variant.longToTrieCoded(min.longValue()),
(max==null) ? null : variant.longToTrieCoded(max.longValue()),
minInclusive,
maxInclusive,
variant
);
this.minUnconverted=min;
this.maxUnconverted=max;
}
/**
* Generates a trie query using the supplied field with range bounds in integer form (long).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
*/
public TrieRangeFilter(final String field, final Long min, final Long max) {
this(field,min,max,TrieUtils.getDefaultTrieVariant());
}
//@Override
public String toString() {
return toString(null);
@ -139,14 +133,24 @@ public final class TrieRangeFilter extends Filter {
public String toString(final String field) {
final StringBuffer sb=new StringBuffer();
if (!this.field.equals(field)) sb.append(this.field).append(':');
return sb.append('[').append(minUnconverted).append(" TO ").append(maxUnconverted).append(']').toString();
return sb.append(minInclusive ? '[' : '{')
.append((minUnconverted==null) ? "*" : minUnconverted.toString())
.append(" TO ")
.append((maxUnconverted==null) ? "*" : maxUnconverted.toString())
.append(maxInclusive ? ']' : '}').toString();
}
/**
* Two instances are equal if they have the same trie-encoded range bounds, same field, and same variant.
* If one of the instances uses an exclusive lower bound, it is equal to a range with inclusive bound,
* when the inclusive lower bound is equal to the incremented exclusive lower bound of the other one.
* The same applys for the upper bound in other direction.
*/
//@Override
public final boolean equals(final Object o) {
if (o instanceof TrieRangeFilter) {
TrieRangeFilter q=(TrieRangeFilter)o;
// trieVariants are singleton per type, so no equals needed
// trieVariants are singleton per type, so no equals needed.
return (field==q.field && min.equals(q.min) && max.equals(q.max) && trieVariant==q.trieVariant);
} else return false;
}
@ -282,6 +286,7 @@ public final class TrieRangeFilter extends Filter {
// members
private final String field,min,max;
private final TrieUtils trieVariant;
private final boolean minInclusive,maxInclusive;
private Object minUnconverted,maxUnconverted;
private int lastNumberOfTerms=-1;
}

View File

@ -34,69 +34,49 @@ public final class TrieRangeQuery extends ConstantScoreQuery {
/**
* Universal constructor (expert use only): Uses already trie-converted min/max values.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeQuery(final String field, final String min, final String max) {
super(new TrieRangeFilter(field,min,max));
}
/**
* Universal constructor (expert use only): Uses already trie-converted min/max values.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeQuery(final String field, final String min, final String max, final TrieUtils variant) {
super(new TrieRangeFilter(field,min,max,variant));
public TrieRangeQuery(final String field, final String min, final String max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
super(new TrieRangeFilter(field,min,max,minInclusive,maxInclusive,variant));
}
/**
* A trie query using the supplied field with range bounds in numeric form (double).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeQuery(final String field, final Double min, final Double max) {
super(new TrieRangeFilter(field,min,max));
}
/**
* A trie query using the supplied field with range bounds in numeric form (double).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeQuery(final String field, final Double min, final Double max, final TrieUtils variant) {
super(new TrieRangeFilter(field,min,max,variant));
public TrieRangeQuery(final String field, final Double min, final Double max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
super(new TrieRangeFilter(field,min,max,minInclusive,maxInclusive,variant));
}
/**
* A trie query using the supplied field with range bounds in date/time form.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeQuery(final String field, final Date min, final Date max) {
super(new TrieRangeFilter(field,min,max));
}
/**
* A trie query using the supplied field with range bounds in date/time form.
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeQuery(final String field, final Date min, final Date max, final TrieUtils variant) {
super(new TrieRangeFilter(field,min,max,variant));
public TrieRangeQuery(final String field, final Date min, final Date max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
super(new TrieRangeFilter(field,min,max,minInclusive,maxInclusive,variant));
}
/**
* A trie query using the supplied field with range bounds in integer form (long).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
* With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
* bound should be included or excluded from the range.
*/
public TrieRangeQuery(final String field, final Long min, final Long max) {
super(new TrieRangeFilter(field,min,max));
}
/**
* A trie query using the supplied field with range bounds in integer form (long).
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
*/
public TrieRangeQuery(final String field, final Long min, final Long max, final TrieUtils variant) {
super(new TrieRangeFilter(field,min,max,variant));
public TrieRangeQuery(final String field, final Long min, final Long max,
final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
) {
super(new TrieRangeFilter(field,min,max,minInclusive,maxInclusive,variant));
}
/**
@ -116,6 +96,12 @@ public final class TrieRangeQuery extends ConstantScoreQuery {
return ((TrieRangeFilter) filter).toString(field)+ToStringUtils.boost(getBoost());
}
/**
* Two instances are equal if they have the same trie-encoded range bounds, same field, same boost, and same variant.
* If one of the instances uses an exclusive lower bound, it is equal to a range with inclusive bound,
* when the inclusive lower bound is equal to the decremented exclusive lower bound.
* The same applys for the upper bound in other direction.
*/
//@Override
public final boolean equals(final Object o) {
if (!(o instanceof TrieRangeQuery)) return false;

View File

@ -51,13 +51,16 @@ import org.apache.lucene.search.ExtendedFieldCache;
*/
public final class TrieUtils {
/** Instance of TrieUtils using a trie factor of 8 bit. */
/** Instance of TrieUtils using a trie factor of 8 bit.
* This is the <b>recommended<b> one (rather fast and storage optimized) */
public static final TrieUtils VARIANT_8BIT=new TrieUtils(8);
/** Instance of TrieUtils using a trie factor of 4 bit. */
public static final TrieUtils VARIANT_4BIT=new TrieUtils(4);
/** Instance of TrieUtils using a trie factor of 2 bit. */
/** Instance of TrieUtils using a trie factor of 2 bit.
* This may be good for some indexes, but it needs much storage space
* and is not much faster than 8 bit in most cases. */
public static final TrieUtils VARIANT_2BIT=new TrieUtils(2);
/** Marker (PADDING) before lower-precision trie entries to signal the precision value. See class description! */
@ -89,27 +92,6 @@ public final class TrieUtils {
}
};
private static TrieUtils defaultTrieVariant=TrieUtils.VARIANT_8BIT;
/**
* Sets the default variant used for generating trie values and ranges.
* It is used by the constructors of {@link TrieRangeQuery} and {@link TrieRangeFilter} without <code>TrieUtils</code> parameter
* and can be used to get a default value through your whole application.
*/
public synchronized static final void setDefaultTrieVariant(final TrieUtils variant) {
defaultTrieVariant=variant;
}
/**
* Gets the default variant used for generating trie values and ranges.
* It is used by the constructors of {@link TrieRangeQuery} and {@link TrieRangeFilter} without <code>TrieUtils</code> parameter
* and can be used to get a default value through your whole application.
* <p>The default, if not set by {@link #setDefaultTrieVariant}, is {@link #VARIANT_8BIT}.
*/
public synchronized static final TrieUtils getDefaultTrieVariant() {
return defaultTrieVariant;
}
/**
* Detects and returns the variant of a trie encoded string using the length.
* @throws NumberFormatException if the length is not 8, 16, or 32 chars.

View File

@ -58,7 +58,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
TrieUtils.VARIANT_2BIT.addLongTrieCodedDocumentField(
doc, "field2", distance*l, true /*index it*/, Field.Store.YES
);
// add ascending fields with a distance of 1 to test the correct splitting of range
// add ascending fields with a distance of 1 to test the correct splitting of range and inclusive/exclusive
TrieUtils.VARIANT_8BIT.addLongTrieCodedDocumentField(
doc, "ascfield8", l, true /*index it*/, Field.Store.NO
);
@ -83,7 +83,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
String field="field"+variant.TRIE_BITS;
int count=3000;
long lower=96666L, upper=lower + count*distance + 1234L;
TrieRangeQuery q=new TrieRangeQuery(field, new Long(lower), new Long(upper), variant);
TrieRangeQuery q=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, true, variant);
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
System.out.println("Found "+q.getLastNumberOfTerms()+" distinct terms in range for field '"+field+"'.");
ScoreDoc[] sd = topDocs.scoreDocs;
@ -111,7 +111,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
String field="field"+variant.TRIE_BITS;
int count=3000;
long upper=(count-1)*distance + 1234L;
TrieRangeQuery q=new TrieRangeQuery(field, null, new Long(upper), variant);
TrieRangeQuery q=new TrieRangeQuery(field, null, new Long(upper), true, true, variant);
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
System.out.println("Found "+q.getLastNumberOfTerms()+" distinct terms in left open range for field '"+field+"'.");
ScoreDoc[] sd = topDocs.scoreDocs;
@ -141,12 +141,34 @@ public class TestTrieRangeQuery extends LuceneTestCase
for (int i=0; i<50; i++) {
long lower=(long)(rnd.nextDouble()*10000L*distance);
long upper=(long)(rnd.nextDouble()*10000L*distance);
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), variant);
// test inclusive range
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, true, variant);
RangeQuery cq=new RangeQuery(field, variant.longToTrieCoded(lower), variant.longToTrieCoded(upper), true, true);
cq.setConstantScoreRewrite(true);
TopDocs tTopDocs = searcher.search(tq, 1);
TopDocs cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for TrieRangeQuery and RangeQuery must be equal", tTopDocs.totalHits, cTopDocs.totalHits );
// test exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), false, false, variant);
cq=new RangeQuery(field, variant.longToTrieCoded(lower), variant.longToTrieCoded(upper), false, false);
cq.setConstantScoreRewrite(true);
tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for TrieRangeQuery and RangeQuery must be equal", tTopDocs.totalHits, cTopDocs.totalHits );
// test left exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), false, true, variant);
cq=new RangeQuery(field, variant.longToTrieCoded(lower), variant.longToTrieCoded(upper), false, true);
cq.setConstantScoreRewrite(true);
tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for TrieRangeQuery and RangeQuery must be equal", tTopDocs.totalHits, cTopDocs.totalHits );
// test right exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, false, variant);
cq=new RangeQuery(field, variant.longToTrieCoded(lower), variant.longToTrieCoded(upper), true, false);
cq.setConstantScoreRewrite(true);
tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for TrieRangeQuery and RangeQuery must be equal", tTopDocs.totalHits, cTopDocs.totalHits );
}
}
@ -171,9 +193,22 @@ public class TestTrieRangeQuery extends LuceneTestCase
if (lower>upper) {
long a=lower; lower=upper; upper=a;
}
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), variant);
// test inclusive range
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, true, variant);
TopDocs tTopDocs = searcher.search(tq, 1);
assertEquals("Returned count of range query must be equal to inclusive range length", tTopDocs.totalHits, upper-lower+1 );
// test exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), false, false, variant);
tTopDocs = searcher.search(tq, 1);
assertEquals("Returned count of range query must be equal to exclusive range length", tTopDocs.totalHits, upper-lower-1 );
// test left exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), false, true, variant);
tTopDocs = searcher.search(tq, 1);
assertEquals("Returned count of range query must be equal to half exclusive range length", tTopDocs.totalHits, upper-lower );
// test right exclusive range
tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, false, variant);
tTopDocs = searcher.search(tq, 1);
assertEquals("Returned count of range query must be equal to half exclusive range length", tTopDocs.totalHits, upper-lower );
}
}
@ -199,7 +234,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
if (lower>upper) {
long a=lower; lower=upper; upper=a;
}
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), variant);
TrieRangeQuery tq=new TrieRangeQuery(field, new Long(lower), new Long(upper), true, true, variant);
TopDocs topDocs = searcher.search(tq, null, 10000, new Sort(variant.getSortField(field, true)));
if (topDocs.totalHits==0) continue;
ScoreDoc[] sd = topDocs.scoreDocs;