SOLR-1871: The 'map' function query accepts a ValueSource as target and default value

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1546926 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2013-12-02 08:33:33 +00:00
parent a7f879db76
commit 4109c6db33
6 changed files with 32 additions and 14 deletions

View File

@ -73,6 +73,9 @@ New Features
(missing the term, weight or payload). (Areek Zillur via (missing the term, weight or payload). (Areek Zillur via
Mike McCandless) Mike McCandless)
* SOLR-1871: The RangeMapFloatFunction accepts an arbitrary ValueSource
as target and default values. (Chris Harris, shalin)
Build Build
* LUCENE-5217: Maven config: get dependencies from Ant+Ivy config; disable * LUCENE-5217: Maven config: get dependencies from Ant+Ivy config; disable

View File

@ -27,8 +27,8 @@ import java.io.IOException;
import java.util.Map; import java.util.Map;
/** /**
* <code>LinearFloatFunction</code> implements a linear function over * <code>RangeMapFloatFunction</code> implements a map function over
* another {@link org.apache.lucene.queries.function.ValueSource}. * another {@link ValueSource} whose values fall within min and max inclusive to target.
* <br> * <br>
* Normally Used as an argument to a {@link org.apache.lucene.queries.function.FunctionQuery} * 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 ValueSource source;
protected final float min; protected final float min;
protected final float max; protected final float max;
protected final float target; protected final ValueSource target;
protected final Float defaultVal; protected final ValueSource defaultVal;
public RangeMapFloatFunction(ValueSource source, float min, float max, float target, Float def) { 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.source = source;
this.min = min; this.min = min;
this.max = max; this.max = max;
@ -51,21 +55,23 @@ public class RangeMapFloatFunction extends ValueSource {
@Override @Override
public String description() { public String description() {
return "map(" + source.description() + "," + min + "," + max + "," + target + ")"; return "map(" + source.description() + "," + min + "," + max + "," + target.description() + ")";
} }
@Override @Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException { public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
final FunctionValues vals = source.getValues(context, readerContext); 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) { return new FloatDocValues(this) {
@Override @Override
public float floatVal(int doc) { public float floatVal(int doc) {
float val = vals.floatVal(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));
} }
@Override @Override
public String toString(int doc) { public 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 += Float.floatToIntBits(min);
h ^= (h << 14) | (h >>> 19); h ^= (h << 14) | (h >>> 19);
h += Float.floatToIntBits(max); h += Float.floatToIntBits(max);
h ^= (h << 13) | (h >>> 20); h += target.hashCode();
h += Float.floatToIntBits(target);
if (defaultVal != null) if (defaultVal != null)
h += defaultVal.hashCode(); h += defaultVal.hashCode();
return h; return h;
@ -95,7 +100,7 @@ public class RangeMapFloatFunction extends ValueSource {
RangeMapFloatFunction other = (RangeMapFloatFunction)o; RangeMapFloatFunction other = (RangeMapFloatFunction)o;
return this.min == other.min return this.min == other.min
&& this.max == other.max && this.max == other.max
&& this.target == other.target && this.target.equals(other.target)
&& this.source.equals(other.source) && this.source.equals(other.source)
&& (this.defaultVal == other.defaultVal || (this.defaultVal != null && this.defaultVal.equals(other.defaultVal))); && (this.defaultVal == other.defaultVal || (this.defaultVal != null && this.defaultVal.equals(other.defaultVal)));
} }

View File

@ -276,6 +276,10 @@ public class TestValueSources extends LuceneTestCase {
assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"),
5, 6, 1, 0f)), 5, 6, 1, 0f)),
new float[] { 1f, 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 { public void testReciprocal() throws Exception {
@ -338,8 +342,8 @@ public class TestValueSources extends LuceneTestCase {
expectedDocs[i] = i; expectedDocs[i] = i;
expected[i] = new ScoreDoc(i, scores[i]); expected[i] = new ScoreDoc(i, scores[i]);
} }
TopDocs docs = searcher.search(q, documents.size(), TopDocs docs = searcher.search(q, null, documents.size(),
new Sort(new SortField("id", SortField.Type.STRING))); new Sort(new SortField("id", SortField.Type.STRING)), true, false);
CheckHits.checkHits(random(), q, "", searcher, expectedDocs); CheckHits.checkHits(random(), q, "", searcher, expectedDocs);
CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs); CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);
CheckHits.checkExplanations(q, "", searcher); CheckHits.checkExplanations(q, "", searcher);

View File

@ -112,6 +112,9 @@ New Features
* SOLR-5023: Add support for deleteInstanceDir to be passed from SolrJ for Core * SOLR-5023: Add support for deleteInstanceDir to be passed from SolrJ for Core
Unload action. (Lyubov Romanchuk, shalin) Unload action. (Lyubov Romanchuk, shalin)
* SOLR-1871: The 'map' function query accepts a ValueSource as target and
default value. (Chris Harris, shalin)
Bug Fixes Bug Fixes
---------------------- ----------------------

View File

@ -198,8 +198,8 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {
ValueSource source = fp.parseValueSource(); ValueSource source = fp.parseValueSource();
float min = fp.parseFloat(); float min = fp.parseFloat();
float max = fp.parseFloat(); float max = fp.parseFloat();
float target = fp.parseFloat(); ValueSource target = fp.parseValueSource();
Float def = fp.hasMoreArguments() ? fp.parseFloat() : null; ValueSource def = fp.hasMoreArguments() ? fp.parseValueSource() : null;
return new RangeMapFloatFunction(source, min, max, target, def); return new RangeMapFloatFunction(source, min, max, target, def);
} }
}); });

View File

@ -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,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,-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,-1,1)",-4,-1, 100,1, 0,-0.9230769f);
singleTest(field,"scale(\0,-10,1000)",-4,-10, 100,1000, 0,28.846153f); singleTest(field,"scale(\0,-10,1000)",-4,-10, 100,1000, 0,28.846153f);