SOLR-940 followup -- Adding FieldType.getRangeQuery method

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@752596 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-03-11 19:50:04 +00:00
parent c02eee38d4
commit 60060d89f4
3 changed files with 44 additions and 23 deletions

View File

@ -24,6 +24,8 @@ import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.solr.search.function.ValueSource;
import org.apache.solr.search.function.OrdFieldSource;
import org.apache.solr.search.Sorting;
@ -424,4 +426,23 @@ public abstract class FieldType extends FieldProperties {
return new OrdFieldSource(field.name);
}
/**
* Returns a Query instance for doing range searches on this field type
*
* @param field the name of the field
* @param part1 the lower boundary of the range
* @param part2 the upper boundary of the range
* @param inclusive whether the range is inclusive or not
* @return a Query instance to perform range search according to given parameters
*/
public Query getRangeQuery(String field, String part1, String part2, boolean inclusive) {
RangeQuery rangeQuery = new RangeQuery(
field,
"*".equals(part1) ? null : toInternal(part1),
"*".equals(part2) ? null : toInternal(part2),
inclusive, inclusive);
rangeQuery.setConstantScoreRewrite(true);
return rangeQuery;
}
}

View File

@ -19,6 +19,8 @@ package org.apache.solr.schema;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.trie.IntTrieRangeFilter;
import org.apache.lucene.search.trie.LongTrieRangeFilter;
import org.apache.lucene.search.trie.TrieUtils;
@ -145,36 +147,45 @@ public class TrieField extends FieldType {
return type;
}
public Filter getTrieRangeFilter(String field, String min, String max, boolean minInclusive, boolean maxInclusive) {
@Override
public Query getRangeQuery(String field, String min, String max, boolean inclusive) {
Filter filter = null;
switch (type) {
case INTEGER:
return new IntTrieRangeFilter(field, field, precisionStep,
filter = new IntTrieRangeFilter(field, field, precisionStep,
"*".equals(min) ? null : Integer.parseInt(min),
"*".equals(max) ? null : Integer.parseInt(max),
minInclusive, maxInclusive);
inclusive, inclusive);
break;
case FLOAT:
return new IntTrieRangeFilter(field, field, precisionStep,
filter = new IntTrieRangeFilter(field, field, precisionStep,
"*".equals(min) ? null : TrieUtils.floatToSortableInt(Float.parseFloat(min)),
"*".equals(max) ? null : TrieUtils.floatToSortableInt(Float.parseFloat(max)),
minInclusive, maxInclusive);
inclusive, inclusive);
break;
case LONG:
return new LongTrieRangeFilter(field, field, precisionStep,
filter = new LongTrieRangeFilter(field, field, precisionStep,
"*".equals(min) ? null : Long.parseLong(min),
"*".equals(max) ? null : Long.parseLong(max),
minInclusive, maxInclusive);
inclusive, inclusive);
break;
case DOUBLE:
return new LongTrieRangeFilter(field, field, precisionStep,
filter = new LongTrieRangeFilter(field, field, precisionStep,
"*".equals(min) ? null : TrieUtils.doubleToSortableLong(Double.parseDouble(min)),
"*".equals(max) ? null : TrieUtils.doubleToSortableLong(Double.parseDouble(max)),
minInclusive, maxInclusive);
inclusive, inclusive);
break;
case DATE:
return new LongTrieRangeFilter(field, field, precisionStep,
filter = new LongTrieRangeFilter(field, field, precisionStep,
"*".equals(min) ? null : dateField.parseMath(null, min).getTime(),
"*".equals(max) ? null : dateField.parseMath(null, max).getTime(),
minInclusive, maxInclusive);
inclusive, inclusive);
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
}
return new ConstantScoreQuery(filter);
}
public enum TrieTypes {

View File

@ -119,18 +119,7 @@ public class SolrQueryParser extends QueryParser {
protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException {
checkNullField(field);
FieldType ft = schema.getFieldType(field);
if (ft instanceof TrieField) {
TrieField f = (TrieField) ft;
return new ConstantScoreQuery(f.getTrieRangeFilter(field, part1, part2, inclusive, inclusive));
} else {
RangeQuery rangeQuery = new RangeQuery(
field,
"*".equals(part1) ? null : ft.toInternal(part1),
"*".equals(part2) ? null : ft.toInternal(part2),
inclusive, inclusive);
rangeQuery.setConstantScoreRewrite(true);
return rangeQuery;
}
return ft.getRangeQuery(field, part1, part2, inclusive);
}
protected Query getPrefixQuery(String field, String termStr) throws ParseException {