Apache Lucene open-source search software
SOLR-11674: Support ranges in the probability Stream Evaluator
Joel Bernstein7acccd515761
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ProbabilityEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ProbabilityEvaluator.javaindex f0c25cb5846..092760c152c 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ProbabilityEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ProbabilityEvaluator.java@@ -20,10 +20,11 @@ import java.io.IOException;import java.util.Locale;import org.apache.commons.math3.distribution.IntegerDistribution;+import org.apache.commons.math3.distribution.AbstractRealDistribution;import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;-public class ProbabilityEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {+public class ProbabilityEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {protected static final long serialVersionUID = 1L;public ProbabilityEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{@@ -31,22 +32,55 @@ public class ProbabilityEvaluator extends RecursiveObjectEvaluator implements Tw}@Override- public Object doWork(Object first, Object second) throws IOException{- if(null == first){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));- }- if(null == second){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));- }- if(!(first instanceof IntegerDistribution)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a IntegerDistribution",toExpression(constructingFactory), first.getClass().getSimpleName()));- }- if(!(second instanceof Number)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName()));- }+ public Object doWork(Object... values) throws IOException{++ Object first = null;+ Object second = null;+ Object third = null;++ if(values.length == 2) {+ first = values[0];+ second = values[1];- IntegerDistribution d = (IntegerDistribution) first;- Number predictOver = (Number) second;- return d.probability(predictOver.intValue());+ if (null == first) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));+ }+ if (null == second) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));+ }+ if (!(first instanceof IntegerDistribution)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a IntegerDistributionm for probability at a specific value.", toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if (!(second instanceof Number)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ IntegerDistribution d = (IntegerDistribution) first;+ Number predictOver = (Number) second;+ return d.probability(predictOver.intValue());++ } else if(values.length == 3) {+ first = values[0];+ second = values[1];+ third = values[2];++ if (!(first instanceof AbstractRealDistribution)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a RealDistribution for probability ranges", toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if (!(second instanceof Number)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ if (!(third instanceof Number)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number", toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ AbstractRealDistribution realDistribution = (AbstractRealDistribution)first;+ Number start = (Number) second;+ Number end = (Number) third;+ return realDistribution.probability(start.doubleValue(), end.doubleValue());+ } else {+ throw new IOException("The probability function expects 2 or 3 parameters");+ }}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.javaindex a2b6b586a05..bfee198fa98 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java@@ -6431,6 +6431,23 @@ public class StreamExpressionTest extends SolrCloudTestCase {}@Test+ public void testProbabilityRange() throws Exception {+ String cexpr = "let(a=normalDistribution(500, 20), " ++ "b=probability(a, 520, 530))";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertTrue(tuples.size() == 1);+ Number prob = (Number)tuples.get(0).get("b");+ assertEquals(prob.doubleValue(), 0.09184805266259899, 0.0);+ }++ @Testpublic void testDistributions() throws Exception {String cexpr = "let(a=normalDistribution(10, 2), " +"b=sample(a, 250), " +