mirror of https://github.com/apache/lucene.git
LUCENE-1517: Change superclass of TrieRangeQuery
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@733519 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4905d4404c
commit
f0300a643e
|
@ -18,21 +18,18 @@ package org.apache.lucene.search.trie;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.apache.lucene.search.Query;
|
|
||||||
import org.apache.lucene.search.ConstantScoreQuery;
|
import org.apache.lucene.search.ConstantScoreQuery;
|
||||||
import org.apache.lucene.util.ToStringUtils;
|
import org.apache.lucene.util.ToStringUtils;
|
||||||
import org.apache.lucene.index.IndexReader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of a Lucene {@link Query} that implements a trie-based range query.
|
* A Lucene {@link Query} that implements a trie-based range query.
|
||||||
* This query depends on a specific structure of terms in the index that can only be created
|
* This query depends on a specific structure of terms in the index that can only be created
|
||||||
* by {@link TrieUtils} methods.
|
* by {@link TrieUtils} methods.
|
||||||
* <p>This class wraps a {@link TrieRangeFilter} using a {@link ConstantScoreQuery}.
|
* <p>This class wraps a {@link TrieRangeFilter}.
|
||||||
* @see TrieRangeFilter
|
* @see TrieRangeFilter
|
||||||
*/
|
*/
|
||||||
public final class TrieRangeQuery extends Query {
|
public final class TrieRangeQuery extends ConstantScoreQuery {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Universal constructor (expert use only): Uses already trie-converted min/max values.
|
* Universal constructor (expert use only): Uses already trie-converted min/max values.
|
||||||
|
@ -40,7 +37,7 @@ public final class TrieRangeQuery extends Query {
|
||||||
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
|
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
|
||||||
*/
|
*/
|
||||||
public TrieRangeQuery(final String field, final String min, final String max) {
|
public TrieRangeQuery(final String field, final String min, final String max) {
|
||||||
filter=new TrieRangeFilter(field,min,max);
|
super(new TrieRangeFilter(field,min,max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,97 +45,86 @@ public final class TrieRangeQuery extends Query {
|
||||||
* You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
|
* 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) {
|
public TrieRangeQuery(final String field, final String min, final String max, final TrieUtils variant) {
|
||||||
filter=new TrieRangeFilter(field,min,max,variant);
|
super(new TrieRangeFilter(field,min,max,variant));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in numeric form (double).
|
* 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.
|
* 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()}.
|
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
|
||||||
*/
|
*/
|
||||||
public TrieRangeQuery(final String field, final Double min, final Double max) {
|
public TrieRangeQuery(final String field, final Double min, final Double max) {
|
||||||
filter=new TrieRangeFilter(field,min,max);
|
super(new TrieRangeFilter(field,min,max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in numeric form (double).
|
* 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.
|
* 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) {
|
public TrieRangeQuery(final String field, final Double min, final Double max, final TrieUtils variant) {
|
||||||
filter=new TrieRangeFilter(field,min,max,variant);
|
super(new TrieRangeFilter(field,min,max,variant));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in date/time form.
|
* 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.
|
* 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()}.
|
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
|
||||||
*/
|
*/
|
||||||
public TrieRangeQuery(final String field, final Date min, final Date max) {
|
public TrieRangeQuery(final String field, final Date min, final Date max) {
|
||||||
filter=new TrieRangeFilter(field,min,max);
|
super(new TrieRangeFilter(field,min,max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in date/time form.
|
* 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.
|
* 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) {
|
public TrieRangeQuery(final String field, final Date min, final Date max, final TrieUtils variant) {
|
||||||
filter=new TrieRangeFilter(field,min,max,variant);
|
super(new TrieRangeFilter(field,min,max,variant));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in integer form (long).
|
* 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.
|
* 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()}.
|
* <p>This constructor uses the trie variant returned by {@link TrieUtils#getDefaultTrieVariant()}.
|
||||||
*/
|
*/
|
||||||
public TrieRangeQuery(final String field, final Long min, final Long max) {
|
public TrieRangeQuery(final String field, final Long min, final Long max) {
|
||||||
filter=new TrieRangeFilter(field,min,max);
|
super(new TrieRangeFilter(field,min,max));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a trie query using the supplied field with range bounds in integer form (long).
|
* 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.
|
* 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) {
|
public TrieRangeQuery(final String field, final Long min, final Long max, final TrieUtils variant) {
|
||||||
filter=new TrieRangeFilter(field,min,max,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.
|
||||||
|
* This method is not thread safe, be sure to only call it when no query is running!
|
||||||
|
* @throws IllegalStateException if query was not yet executed.
|
||||||
|
*/
|
||||||
|
public int getLastNumberOfTerms() {
|
||||||
|
return ((TrieRangeFilter) filter).getLastNumberOfTerms();
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
//@Override
|
||||||
public String toString(final String field) {
|
public String toString(final String field) {
|
||||||
return filter.toString(field)+ToStringUtils.boost(getBoost());
|
// return a more convenient representation of this query than ConstantScoreQuery does:
|
||||||
|
return ((TrieRangeFilter) filter).toString(field)+ToStringUtils.boost(getBoost());
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
//@Override
|
||||||
public final boolean equals(final Object o) {
|
public final boolean equals(final Object o) {
|
||||||
if (o instanceof TrieRangeQuery) {
|
if (!(o instanceof TrieRangeQuery)) return false;
|
||||||
TrieRangeQuery q=(TrieRangeQuery)o;
|
return super.equals(o);
|
||||||
return (filter.equals(q.filter) && getBoost()==q.getBoost());
|
|
||||||
} else return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
//@Override
|
||||||
public final int hashCode() {
|
public final int hashCode() {
|
||||||
return filter.hashCode()^0x1756fa55+Float.floatToIntBits(getBoost());
|
// make hashCode a little bit different:
|
||||||
|
return super.hashCode()^0x1756fa55;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Rewrites the query to native Lucene {@link Query}'s. This implementation uses a {@link ConstantScoreQuery} with
|
|
||||||
* a {@link TrieRangeFilter} as implementation of the trie algorithm.
|
|
||||||
*/
|
|
||||||
//@Override
|
|
||||||
public Query rewrite(final IndexReader reader) throws IOException {
|
|
||||||
final ConstantScoreQuery q = new ConstantScoreQuery(filter);
|
|
||||||
q.setBoost(getBoost());
|
|
||||||
return q.rewrite(reader);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the underlying filter.
|
|
||||||
*/
|
|
||||||
public TrieRangeFilter getFilter() {
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
// members
|
|
||||||
private final TrieRangeFilter filter;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
|
||||||
long lower=96666L, upper=lower + count*distance + 1234L;
|
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), variant);
|
||||||
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
|
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
|
||||||
System.out.println("Found "+q.getFilter().getLastNumberOfTerms()+" distinct terms in range for field '"+field+"'.");
|
System.out.println("Found "+q.getLastNumberOfTerms()+" distinct terms in range for field '"+field+"'.");
|
||||||
ScoreDoc[] sd = topDocs.scoreDocs;
|
ScoreDoc[] sd = topDocs.scoreDocs;
|
||||||
assertNotNull(sd);
|
assertNotNull(sd);
|
||||||
assertEquals("Score docs must match "+count+" docs, found "+sd.length+" docs", sd.length, count );
|
assertEquals("Score docs must match "+count+" docs, found "+sd.length+" docs", sd.length, count );
|
||||||
|
@ -112,7 +112,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
|
||||||
long upper=(count-1)*distance + 1234L;
|
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), variant);
|
||||||
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
|
TopDocs topDocs = searcher.search(q, null, 10000, Sort.INDEXORDER);
|
||||||
System.out.println("Found "+q.getFilter().getLastNumberOfTerms()+" distinct terms in left open range for field '"+field+"'.");
|
System.out.println("Found "+q.getLastNumberOfTerms()+" distinct terms in left open range for field '"+field+"'.");
|
||||||
ScoreDoc[] sd = topDocs.scoreDocs;
|
ScoreDoc[] sd = topDocs.scoreDocs;
|
||||||
assertNotNull(sd);
|
assertNotNull(sd);
|
||||||
assertEquals("Score docs must match "+count+" docs, found "+sd.length+" docs", sd.length, count );
|
assertEquals("Score docs must match "+count+" docs, found "+sd.length+" docs", sd.length, count );
|
||||||
|
|
Loading…
Reference in New Issue