Apache Lucene open-source search software
SOLR-940 followup -- Adding FieldType.getRangeQuery method
Shalin Shekhar Mangar60060d89f4c5
diff --git a/src/java/org/apache/solr/schema/FieldType.java b/src/java/org/apache/solr/schema/FieldType.javaindex a4ddade1e2c..3a539bde9fc 100644--- a/src/java/org/apache/solr/schema/FieldType.java+++ b/src/java/org/apache/solr/schema/FieldType.java@@ -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;+ }+}diff --git a/src/java/org/apache/solr/schema/TrieField.java b/src/java/org/apache/solr/schema/TrieField.javaindex f016d3de3b3..4faea632c10 100644--- a/src/java/org/apache/solr/schema/TrieField.java+++ b/src/java/org/apache/solr/schema/TrieField.java@@ -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 {diff --git a/src/java/org/apache/solr/search/SolrQueryParser.java b/src/java/org/apache/solr/search/SolrQueryParser.javaindex a9aa5ae8d85..7036c6f7078 100644--- a/src/java/org/apache/solr/search/SolrQueryParser.java+++ b/src/java/org/apache/solr/search/SolrQueryParser.java@@ -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 {