Apache Lucene open-source search software
SOLR-1871: The 'map' function query accepts a ValueSource as target and default value
Shalin Shekhar Mangar4109c6db3365
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 9859b412fb5..f16bce3e712 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -73,6 +73,9 @@ New Features(missing the term, weight or payload). (Areek Zillur viaMike McCandless)+* SOLR-1871: The RangeMapFloatFunction accepts an arbitrary ValueSource+ as target and default values. (Chris Harris, shalin)+Build* LUCENE-5217: Maven config: get dependencies from Ant+Ivy config; disablediff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/RangeMapFloatFunction.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/RangeMapFloatFunction.javaindex 79df9b91411..2402af82fbe 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/RangeMapFloatFunction.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/RangeMapFloatFunction.java@@ -27,8 +27,8 @@ import java.io.IOException;import java.util.Map;/**- * <code>LinearFloatFunction</code> implements a linear function over- * another {@link org.apache.lucene.queries.function.ValueSource}.+ * <code>RangeMapFloatFunction</code> implements a map function over+ * another {@link ValueSource} whose values fall within min and max inclusive to target.* <br>* Normally Used as an argument to a {@link org.apache.lucene.queries.function.FunctionQuery}*@@ -38,10 +38,14 @@ public class RangeMapFloatFunction extends ValueSource {protected final ValueSource source;protected final float min;protected final float max;- protected final float target;- protected final Float defaultVal;+ protected final ValueSource target;+ protected final ValueSource defaultVal;public RangeMapFloatFunction(ValueSource source, float min, float max, float target, Float def) {+ this(source, min, max, new ConstValueSource(target), def == null ? null : new ConstValueSource(def));+ }++ public RangeMapFloatFunction(ValueSource source, float min, float max, ValueSource target, ValueSource def) {this.source = source;this.min = min;this.max = max;@@ -51,21 +55,23 @@ public class RangeMapFloatFunction extends ValueSource {@Overridepublic String description() {- return "map(" + source.description() + "," + min + "," + max + "," + target + ")";+ return "map(" + source.description() + "," + min + "," + max + "," + target.description() + ")";}@Overridepublic FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {final FunctionValues vals = source.getValues(context, readerContext);+ final FunctionValues targets = target.getValues(context, readerContext);+ final FunctionValues defaults = (this.defaultVal == null) ? null : defaultVal.getValues(context, readerContext);return new FloatDocValues(this) {@Overridepublic float floatVal(int doc) {float val = vals.floatVal(doc);- return (val>=min && val<=max) ? target : (defaultVal == null ? val : defaultVal);+ return (val>=min && val<=max) ? targets.floatVal(doc) : (defaultVal == null ? val : defaults.floatVal(doc));}@Overridepublic String toString(int doc) {- return "map(" + vals.toString(doc) + ",min=" + min + ",max=" + max + ",target=" + target + ")";+ return "map(" + vals.toString(doc) + ",min=" + min + ",max=" + max + ",target=" + targets.toString(doc) + ")";}};}@@ -82,8 +88,7 @@ public class RangeMapFloatFunction extends ValueSource {h += Float.floatToIntBits(min);h ^= (h << 14) | (h >>> 19);h += Float.floatToIntBits(max);- h ^= (h << 13) | (h >>> 20);- h += Float.floatToIntBits(target);+ h += target.hashCode();if (defaultVal != null)h += defaultVal.hashCode();return h;@@ -95,7 +100,7 @@ public class RangeMapFloatFunction extends ValueSource {RangeMapFloatFunction other = (RangeMapFloatFunction)o;return this.min == other.min&& this.max == other.max- && this.target == other.target+ && this.target.equals(other.target)&& this.source.equals(other.source)&& (this.defaultVal == other.defaultVal || (this.defaultVal != null && this.defaultVal.equals(other.defaultVal)));}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.javaindex e781b22b419..50f85a84bf2 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java@@ -276,6 +276,10 @@ public class TestValueSources extends LuceneTestCase {assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"),5, 6, 1, 0f)),new float[] { 1f, 0f });+ assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"),+ 5, 6, new SumFloatFunction(new ValueSource[] {new ConstValueSource(1f), new ConstValueSource(2f)}),+ new ConstValueSource(11f))),+ new float[] { 3f, 11f });}public void testReciprocal() throws Exception {@@ -338,8 +342,8 @@ public class TestValueSources extends LuceneTestCase {expectedDocs[i] = i;expected[i] = new ScoreDoc(i, scores[i]);}- TopDocs docs = searcher.search(q, documents.size(),- new Sort(new SortField("id", SortField.Type.STRING)));+ TopDocs docs = searcher.search(q, null, documents.size(),+ new Sort(new SortField("id", SortField.Type.STRING)), true, false);CheckHits.checkHits(random(), q, "", searcher, expectedDocs);CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);CheckHits.checkExplanations(q, "", searcher);diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 29b43f45b8a..9189b01c1d7 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -112,6 +112,9 @@ New Features* SOLR-5023: Add support for deleteInstanceDir to be passed from SolrJ for CoreUnload action. (Lyubov Romanchuk, shalin)+* SOLR-1871: The 'map' function query accepts a ValueSource as target and+ default value. (Chris Harris, shalin)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.javaindex e426b8b8fac..4c6cb941a3b 100644--- a/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/core/src/java/org/apache/solr/search/ValueSourceParser.java@@ -198,8 +198,8 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {ValueSource source = fp.parseValueSource();float min = fp.parseFloat();float max = fp.parseFloat();- float target = fp.parseFloat();- Float def = fp.hasMoreArguments() ? fp.parseFloat() : null;+ ValueSource target = fp.parseValueSource();+ ValueSource def = fp.hasMoreArguments() ? fp.parseValueSource() : null;return new RangeMapFloatFunction(source, min, max, target, def);}});diff --git a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.javaindex 7068b1620c9..1ed6dbcf4f3 100644--- a/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java+++ b/solr/core/src/test/org/apache/solr/search/function/TestFunctionQuery.java@@ -172,6 +172,9 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {singleTest(field,"map(\0,0,0,500)",10,10, -4,-4, 0,500);singleTest(field,"map(\0,-4,5,500)",100,100, -4,500, 0,500, 5,500, 10,10, 25,25);+ singleTest(field,"map(\0,0,0,sum(\0,500))",10,10, -4,-4, 0,500);+ singleTest(field,"map(\0,0,0,sum(\0,500),sum(\0,1))",10,11, -4,-3, 0,500);+ singleTest(field,"map(\0,-4,5,sum(\0,1))",100,100, -4,-3, 0,1, 5,6, 10,10, 25,25);singleTest(field,"scale(\0,-1,1)",-4,-1, 100,1, 0,-0.9230769f);singleTest(field,"scale(\0,-10,1000)",-4,-10, 100,1000, 0,28.846153f);