SOLR-966 -- Enhance the map function query to take in an optional default value

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@740445 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-02-03 21:09:04 +00:00
parent d707212bbf
commit 522a5bed44
3 changed files with 13 additions and 5 deletions

View File

@ -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
----------------------

View File

@ -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) {

View File

@ -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)));
}
}