You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2017/06/07 14:11:38 UTC
[34/50] [abbrv] lucene-solr:feature/autoscaling: SOLR-10696: Add
cumulative probability function
SOLR-10696: Add cumulative probability function
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/99ca13f9
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/99ca13f9
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/99ca13f9
Branch: refs/heads/feature/autoscaling
Commit: 99ca13f90fa04fb804e20bbdf51bf6cfc4ea3a34
Parents: f275e3b
Author: Joel Bernstein <jb...@apache.org>
Authored: Fri Jun 2 15:19:38 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Mon Jun 5 11:19:17 2017 -0400
----------------------------------------------------------------------
.../org/apache/solr/handler/StreamHandler.java | 1 +
.../io/eval/CumulativeProbabilityEvaluator.java | 66 ++++++++++++++++++++
.../solrj/io/eval/PercentileEvaluator.java | 24 ++++---
.../solrj/io/stream/StreamExpressionTest.java | 65 +++++++++++++++++--
4 files changed, 143 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/99ca13f9/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index f79f86e..d4029ff 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -187,6 +187,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("copyOfRange", CopyOfRangeEvaluator.class)
.withFunctionName("percentile", PercentileEvaluator.class)
.withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class)
+ .withFunctionName("cumulativeProbability", CumulativeProbabilityEvaluator.class)
.withFunctionName("describe", DescribeEvaluator.class)
.withFunctionName("finddelay", FindDelayEvaluator.class)
.withFunctionName("sequence", SequenceEvaluator.class)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/99ca13f9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java
new file mode 100644
index 0000000..469e983
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CumulativeProbabilityEvaluator.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.client.solrj.io.eval;
+
+import java.io.IOException;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class CumulativeProbabilityEvaluator extends ComplexEvaluator implements Expressible {
+
+ private static final long serialVersionUID = 1;
+
+ public CumulativeProbabilityEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ public Number evaluate(Tuple tuple) throws IOException {
+
+ if(subEvaluators.size() != 2) {
+ throw new IOException("Cumulative probability expects 2 parameters: an emperical distribution and a number");
+ }
+
+ StreamEvaluator r = subEvaluators.get(0);
+ StreamEvaluator d = subEvaluators.get(1);
+
+ EmpiricalDistributionEvaluator.EmpiricalDistributionTuple e = (EmpiricalDistributionEvaluator.EmpiricalDistributionTuple)r.evaluate(tuple);
+ Number n = (Number)d.evaluate(tuple);
+ return e.percentile(n.doubleValue());
+ }
+
+ @Override
+ public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {
+ StreamExpression expression = new StreamExpression(factory.getFunctionName(getClass()));
+ return expression;
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) throws IOException {
+ return new Explanation(nodeId.toString())
+ .withExpressionType(ExpressionType.EVALUATOR)
+ .withFunctionName(factory.getFunctionName(getClass()))
+ .withImplementingClass(getClass().getName())
+ .withExpression(toExpression(factory).toString());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/99ca13f9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java
index 19d423d..7867048 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PercentileEvaluator.java
@@ -17,6 +17,9 @@
package org.apache.solr.client.solrj.io.eval;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.List;
import java.io.IOException;
import org.apache.solr.client.solrj.io.Tuple;
@@ -26,6 +29,7 @@ import org.apache.solr.client.solrj.io.stream.expr.Expressible;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.commons.math3.stat.descriptive.rank.Percentile;
public class PercentileEvaluator extends ComplexEvaluator implements Expressible {
@@ -36,17 +40,23 @@ public class PercentileEvaluator extends ComplexEvaluator implements Expressible
}
public Number evaluate(Tuple tuple) throws IOException {
-
if(subEvaluators.size() != 2) {
- throw new IOException("Percentile expects 2 parameters: a regression result and a number");
+ throw new IOException("Percentile expects 2 parameters: an array and a number");
}
- StreamEvaluator r = subEvaluators.get(0);
- StreamEvaluator d = subEvaluators.get(1);
+ StreamEvaluator colEval = subEvaluators.get(0);
+ List<Number> column = (List<Number>)colEval.evaluate(tuple);
+
+ double[] data = new double[column.size()];
+ for(int i=0; i<data.length; i++) {
+ data[i] = column.get(i).doubleValue();
+ }
- EmpiricalDistributionEvaluator.EmpiricalDistributionTuple e = (EmpiricalDistributionEvaluator.EmpiricalDistributionTuple)r.evaluate(tuple);
- Number n = (Number)d.evaluate(tuple);
- return e.percentile(n.doubleValue());
+ Percentile percentile = new Percentile();
+ percentile.setData(data);
+ StreamEvaluator numEval = subEvaluators.get(1);
+ Number num = (Number)numEval.evaluate(tuple);
+ return percentile.evaluate(num.doubleValue());
}
@Override
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/99ca13f9/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
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.java
index 3957915..8ca52d1 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
@@ -5759,8 +5759,61 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
+
+ @Test
+ public void testPercentile() throws Exception {
+ String cexpr = "percentile(array(1,2,3,4,5,6,7,8,9,10,11), 50)";
+ 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);
+ Tuple tuple = tuples.get(0);
+ double p = tuple.getDouble("return-value");
+ assertEquals(p, 6, 0.0);
+
+
+ cexpr = "percentile(array(11,10,3,4,5,6,7,8,9,2,1), 50)";
+ paramsLoc = new ModifiableSolrParams();
+ paramsLoc.set("expr", cexpr);
+ paramsLoc.set("qt", "/stream");
+
+ solrStream = new SolrStream(url, paramsLoc);
+
+ context = new StreamContext();
+ solrStream.setStreamContext(context);
+ tuples = getTuples(solrStream);
+ assertTrue(tuples.size() == 1);
+ tuple = tuples.get(0);
+ p = tuple.getDouble("return-value");
+ assertEquals(p, 6, 0.0);
+
+ cexpr = "percentile(array(11,10,3,4,5,6,7,8,9,2,1), 20)";
+ paramsLoc = new ModifiableSolrParams();
+ paramsLoc.set("expr", cexpr);
+ paramsLoc.set("qt", "/stream");
+
+ solrStream = new SolrStream(url, paramsLoc);
+
+ context = new StreamContext();
+ solrStream.setStreamContext(context);
+ tuples = getTuples(solrStream);
+ assertTrue(tuples.size() == 1);
+ tuple = tuples.get(0);
+ p = tuple.getDouble("return-value");
+ assertEquals(p, 2.4, 0.001);
+
+ }
+
+
@Test
- public void testPercentiles() throws Exception {
+ public void testCumulativeProbability() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();
int i=0;
@@ -5773,11 +5826,11 @@ public class StreamExpressionTest extends SolrCloudTestCase {
String expr = "search("+COLLECTIONORALIAS+", q=\"*:*\", fl=\"price_f\", sort=\"price_f asc\", rows=\"200\")";
String cexpr = "let(a="+expr+", c=col(a, price_f), e=empiricalDistribution(c), " +
- "tuple(p1=percentile(e, 88), " +
- "p2=percentile(e, 2), " +
- "p3=percentile(e, 99), " +
- "p4=percentile(e, 77), " +
- "p5=percentile(e, 98)))";
+ "tuple(p1=cumulativeProbability(e, 88), " +
+ "p2=cumulativeProbability(e, 2), " +
+ "p3=cumulativeProbability(e, 99), " +
+ "p4=cumulativeProbability(e, 77), " +
+ "p5=cumulativeProbability(e, 98)))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr);