You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by sh...@apache.org on 2009/03/11 20:50:05 UTC

svn commit: r752596 - in /lucene/solr/trunk/src/java/org/apache/solr: schema/FieldType.java schema/TrieField.java search/SolrQueryParser.java

Author: shalin
Date: Wed Mar 11 19:50:04 2009
New Revision: 752596

URL: http://svn.apache.org/viewvc?rev=752596&view=rev
Log:
SOLR-940 followup -- Adding FieldType.getRangeQuery method

Modified:
    lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java
    lucene/solr/trunk/src/java/org/apache/solr/schema/TrieField.java
    lucene/solr/trunk/src/java/org/apache/solr/search/SolrQueryParser.java

Modified: lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java?rev=752596&r1=752595&r2=752596&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java Wed Mar 11 19:50:04 2009
@@ -24,6 +24,8 @@
 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 @@
     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;
+  }
+
 }

Modified: lucene/solr/trunk/src/java/org/apache/solr/schema/TrieField.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/schema/TrieField.java?rev=752596&r1=752595&r2=752596&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/schema/TrieField.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/schema/TrieField.java Wed Mar 11 19:50:04 2009
@@ -19,6 +19,8 @@
 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 @@
     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 {

Modified: lucene/solr/trunk/src/java/org/apache/solr/search/SolrQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/search/SolrQueryParser.java?rev=752596&r1=752595&r2=752596&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/search/SolrQueryParser.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/search/SolrQueryParser.java Wed Mar 11 19:50:04 2009
@@ -119,18 +119,7 @@
   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 {



Re: svn commit: r752596 - in /lucene/solr/trunk/src/java/org/apache/solr: schema/FieldType.java schema/TrieField.java search/SolrQueryParser.java

Posted by Shalin Shekhar Mangar <sh...@gmail.com>.
On Thu, Mar 12, 2009 at 3:15 AM, Chris Hostetter
<ho...@fucit.org>wrote:

>
>  I don't think treating "*" as special is something FieldType (or
> TrieField) should do -- that's specific to the syntax of the QueryParser.
> The FieldType classes should treat the string as a string. (otherwise if i
> write a new QueryParser where * isn't a special character and use some
> syntax like "phoneNumber < *69" i'm screwed.
> "*69" as the
>
> I also think having a single "inclusive" boolean is a bad idea.
>
> I would javadoc that the lower/upper bounds can be null, and have
> SolrQueryParser pass null when it sees "*" in the syntax.  we should also
> be explicit in the javadocs about what combinations of inclusion booleans
> and null values are allowed so that subclasses know what to expect
>

Agree on all points. I'll give a fix.

-- 
Regards,
Shalin Shekhar Mangar.

Re: svn commit: r752596 - in /lucene/solr/trunk/src/java/org/apache/solr: schema/FieldType.java schema/TrieField.java search/SolrQueryParser.java

Posted by Chris Hostetter <ho...@fucit.org>.
: +++ lucene/solr/trunk/src/java/org/apache/solr/schema/FieldType.java Wed Mar 11 19:50:04 2009

: +  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;

I don't think treating "*" as special is something FieldType (or 
TrieField) should do -- that's specific to the syntax of the QueryParser.  
The FieldType classes should treat the string as a string. (otherwise if i 
write a new QueryParser where * isn't a special character and use some 
syntax like "phoneNumber < *69" i'm screwed.
"*69" as the

I also think having a single "inclusive" boolean is a bad idea.

I would javadoc that the lower/upper bounds can be null, and have 
SolrQueryParser pass null when it sees "*" in the syntax.  we should also 
be explicit in the javadocs about what combinations of inclusion booleans 
and null values are allowed so that subclasses know what to expect

(i remember there was some confusion about this when RangeFilter was 
introduced in Lucene - different people have different assumptions about 
wether you can have a range that includes an un-specified boundary)



-Hoss