Author: hossman
Date: Wed Mar  1 10:44:13 2006
New Revision: 382119

URL: http://svn.apache.org/viewcvs?rev=382119&view=rev
Log:
adding a 'max' function which is neccessary if you want to keep function scores 
positive

Added:
    
incubator/solr/trunk/src/java/org/apache/solr/search/function/MaxFloatFunction.java
Modified:
    incubator/solr/trunk/src/java/org/apache/solr/search/QueryParsing.java

Modified: incubator/solr/trunk/src/java/org/apache/solr/search/QueryParsing.java
URL: 
http://svn.apache.org/viewcvs/incubator/solr/trunk/src/java/org/apache/solr/search/QueryParsing.java?rev=382119&r1=382118&r2=382119&view=diff
==============================================================================
--- incubator/solr/trunk/src/java/org/apache/solr/search/QueryParsing.java 
(original)
+++ incubator/solr/trunk/src/java/org/apache/solr/search/QueryParsing.java Wed 
Mar  1 10:44:13 2006
@@ -450,6 +450,11 @@
         sp.expect(",");
         float intercept = sp.getFloat();
         vs = new LinearFloatFunction(source,slope,intercept);
+      } else if (id.equals("max")) {
+        ValueSource source = parseValSource(sp, schema);
+        sp.expect(",");
+        float val = sp.getFloat();
+        vs = new MaxFloatFunction(source,val);
       } else if (id.equals("recip")) {
         ValueSource source = parseValSource(sp,schema);
         sp.expect(",");

Added: 
incubator/solr/trunk/src/java/org/apache/solr/search/function/MaxFloatFunction.java
URL: 
http://svn.apache.org/viewcvs/incubator/solr/trunk/src/java/org/apache/solr/search/function/MaxFloatFunction.java?rev=382119&view=auto
==============================================================================
--- 
incubator/solr/trunk/src/java/org/apache/solr/search/function/MaxFloatFunction.java
 (added)
+++ 
incubator/solr/trunk/src/java/org/apache/solr/search/function/MaxFloatFunction.java
 Wed Mar  1 10:44:13 2006
@@ -0,0 +1,83 @@
+/**
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search.function;
+
+import org.apache.lucene.index.IndexReader;
+
+import java.io.IOException;
+
+/**
+ * Returns the max of a ValueSource and a float
+ * (which is useful for "bottoming out" another function at 0.0,
+ * or some positive number).
+ * <br>
+ * Normally Used as an argument to a [EMAIL PROTECTED] FunctionQuery}
+ *
+ * @author hossman
+ * @version $Id: $
+ */
+public class MaxFloatFunction extends ValueSource {
+  protected final ValueSource source;
+  protected final float fval;
+
+  public MaxFloatFunction(ValueSource source, float fval) {
+    this.source = source;
+    this.fval = fval;
+  }
+  
+  public String description() {
+    return "max(" + source.description() + "," + fval + ")";
+  }
+
+  public DocValues getValues(IndexReader reader) throws IOException {
+    final DocValues vals =  source.getValues(reader);
+    return new DocValues() {
+      public float floatVal(int doc) {
+       float v = vals.floatVal(doc);
+        return v < fval ? fval : v;
+      }
+      public int intVal(int doc) {
+        return (int)floatVal(doc);
+      }
+      public long longVal(int doc) {
+        return (long)floatVal(doc);
+      }
+      public double doubleVal(int doc) {
+        return (double)floatVal(doc);
+      }
+      public String strVal(int doc) {
+        return Float.toString(floatVal(doc));
+      }
+      public String toString(int doc) {
+       return "max(" + vals.toString(doc) + "," + fval + ")";
+      }
+    };
+  }
+
+  public int hashCode() {
+    int h = Float.floatToIntBits(fval);
+    h = (h >>> 2) | (h << 30);
+    return h + source.hashCode();
+  }
+
+  public boolean equals(Object o) {
+    if (MaxFloatFunction.class != o.getClass()) return false;
+    MaxFloatFunction other = (MaxFloatFunction)o;
+    return  this.fval == other.fval
+         && this.source.equals(other.source);
+  }
+}


Reply via email to