Index: contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeFilter.java =================================================================== --- contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeFilter.java (revision 737879) +++ contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeFilter.java (working copy) @@ -39,35 +39,44 @@ /** * Universal constructor (expert use only): Uses already trie-converted min/max values. * You can set min or max (but not both) to null to leave one bound open. + * With minInclusive and maxInclusive 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 min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie package returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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 min or max (but not both) to null 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 @@ } /** - * 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 min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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 min or max (but not both) to null 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 @@ } /** - * 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 min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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 min or max (but not both) to null 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 min or max (but not both) to null to leave one bound open. - *

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 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 @@ // members private final String field,min,max; private final TrieUtils trieVariant; + private final boolean minInclusive,maxInclusive; private Object minUnconverted,maxUnconverted; private int lastNumberOfTerms=-1; } Index: contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeQuery.java =================================================================== --- contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeQuery.java (revision 737879) +++ contrib/queries/src/java/org/apache/lucene/search/trie/TrieRangeQuery.java (working copy) @@ -34,72 +34,52 @@ /** * Universal constructor (expert use only): Uses already trie-converted min/max values. * You can set min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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)); + 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)); } /** - * Universal constructor (expert use only): Uses already trie-converted min/max values. - * You can set min or max (but not both) to null 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)); - } - - /** * A trie query using the supplied field with range bounds in numeric form (double). * You can set min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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)); + 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 numeric form (double). - * You can set min or max (but not both) to null 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)); - } - - /** * A trie query using the supplied field with range bounds in date/time form. * You can set min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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)); + 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 date/time form. - * You can set min or max (but not both) to null 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)); - } - - /** * A trie query using the supplied field with range bounds in integer form (long). * You can set min or max (but not both) to null to leave one bound open. - *

This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}. + * With minInclusive and maxInclusive 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)); + 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)); } /** - * A trie query using the supplied field with range bounds in integer form (long). - * You can set min or max (but not both) to null 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)); - } - - /** * EXPERT: Return the number of terms visited during the last execution of the query. * This may be used for performance comparisons of different trie variants and their effectiveness. * When using this method be sure to query an one-segment index (optimized one) to get correct results. @@ -116,6 +96,12 @@ 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; Index: contrib/queries/src/java/org/apache/lucene/search/trie/TrieUtils.java =================================================================== --- contrib/queries/src/java/org/apache/lucene/search/trie/TrieUtils.java (revision 737879) +++ contrib/queries/src/java/org/apache/lucene/search/trie/TrieUtils.java (working copy) @@ -51,13 +51,16 @@ */ 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 recommended 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,28 +92,7 @@ } }; - 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 TrieUtils 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 TrieUtils parameter - * and can be used to get a default value through your whole application. - *

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. */ Index: contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java =================================================================== --- contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java (revision 737879) +++ contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java (working copy) @@ -58,7 +58,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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;