diff --git a/CHANGES.txt b/CHANGES.txt index 3c134ba4211..87b44d6c6e0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -143,6 +143,8 @@ New Features 31. SOLR-763: Add support for Lucene's PositionFilter (Mck SembWever via shalin) +32. SOLR-966: Enhance the map() function query to take in an optional default value (Noble Paul, shalin) + Optimizations ---------------------- diff --git a/src/java/org/apache/solr/search/ValueSourceParser.java b/src/java/org/apache/solr/search/ValueSourceParser.java index 5f2ce20dc6d..2d1aff40bee 100755 --- a/src/java/org/apache/solr/search/ValueSourceParser.java +++ b/src/java/org/apache/solr/search/ValueSourceParser.java @@ -160,7 +160,8 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin float min = fp.parseFloat(); float max = fp.parseFloat(); float target = fp.parseFloat(); - return new RangeMapFloatFunction(source,min,max,target); + Float def = fp.hasMoreArguments() ? fp.parseFloat() : null; + return new RangeMapFloatFunction(source,min,max,target,def); } public void init(NamedList args) { diff --git a/src/java/org/apache/solr/search/function/RangeMapFloatFunction.java b/src/java/org/apache/solr/search/function/RangeMapFloatFunction.java index a67a51d1783..7245b1cddc9 100755 --- a/src/java/org/apache/solr/search/function/RangeMapFloatFunction.java +++ b/src/java/org/apache/solr/search/function/RangeMapFloatFunction.java @@ -34,12 +34,14 @@ public class RangeMapFloatFunction extends ValueSource { protected final float min; protected final float max; protected final float target; + protected final Float defaultVal; - public RangeMapFloatFunction(ValueSource source, float min, float max, float target) { + public RangeMapFloatFunction(ValueSource source, float min, float max, float target, Float def) { this.source = source; this.min = min; this.max = max; this.target = target; + this.defaultVal = def; } public String description() { @@ -51,7 +53,7 @@ public class RangeMapFloatFunction extends ValueSource { return new DocValues() { public float floatVal(int doc) { float val = vals.floatVal(doc); - return (val>=min && val<=max) ? target : val; + return (val>=min && val<=max) ? target : (defaultVal == null ? val : defaultVal); } public int intVal(int doc) { return (int)floatVal(doc); @@ -74,11 +76,13 @@ public class RangeMapFloatFunction extends ValueSource { public int hashCode() { int h = source.hashCode(); h ^= (h << 10) | (h >>> 23); - Float.floatToIntBits(min); + h += Float.floatToIntBits(min); h ^= (h << 14) | (h >>> 19); h += Float.floatToIntBits(max); h ^= (h << 13) | (h >>> 20); h += Float.floatToIntBits(target); + if (defaultVal != null) + h += defaultVal.hashCode(); return h; } @@ -88,6 +92,7 @@ public class RangeMapFloatFunction extends ValueSource { return this.min == other.min && this.max == other.max && this.target == other.target - && this.source.equals(other.source); + && this.source.equals(other.source) + && (this.defaultVal == other.defaultVal || (this.defaultVal != null && this.defaultVal.equals(other.defaultVal))); } }