You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2017/06/05 18:36:26 UTC
[12/41] lucene-solr:branch_6x: SOLR-10724: Add describe Stream
Evaluator
SOLR-10724: Add describe Stream Evaluator
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/17dac2bc
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/17dac2bc
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/17dac2bc
Branch: refs/heads/branch_6x
Commit: 17dac2bce386cf365c61df7599571a54c6033538
Parents: 8d507f0
Author: Joel Bernstein <jb...@apache.org>
Authored: Mon May 22 18:41:08 2017 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Mon Jun 5 13:45:23 2017 -0400
----------------------------------------------------------------------
.../org/apache/solr/handler/StreamHandler.java | 1 +
.../solrj/io/stream/DescribeEvaluator.java | 92 ++++++++++++++++++++
.../solrj/io/stream/StreamExpressionTest.java | 55 ++++++++++++
3 files changed, 148 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17dac2bc/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 a2a49a0..9fd7c8b 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -183,6 +183,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("copyOfRange", CopyOfRangeEvaluator.class)
.withFunctionName("percentile", PercentileEvaluator.class)
.withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class)
+ .withFunctionName("describe", DescribeEvaluator.class)
// metrics
.withFunctionName("min", MinMetric.class)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17dac2bc/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/DescribeEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/DescribeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/DescribeEvaluator.java
new file mode 100644
index 0000000..e086ebf
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/DescribeEvaluator.java
@@ -0,0 +1,92 @@
+/*
+ * 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.stream;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.eval.ComplexEvaluator;
+import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
+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 DescribeEvaluator extends ComplexEvaluator implements Expressible {
+
+ private static final long serialVersionUID = 1;
+
+ public DescribeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ public Tuple evaluate(Tuple tuple) throws IOException {
+
+ if(subEvaluators.size() != 1) {
+ throw new IOException("describe expects 1 column as a parameters");
+ }
+
+ StreamEvaluator colEval = subEvaluators.get(0);
+
+ List<Number> numbers = (List<Number>)colEval.evaluate(tuple);
+ DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
+
+ for(Number n : numbers) {
+ descriptiveStatistics.addValue(n.doubleValue());
+ }
+
+
+ Map map = new HashMap();
+
+ map.put("max", descriptiveStatistics.getMax());
+ map.put("mean", descriptiveStatistics.getMean());
+ map.put("min", descriptiveStatistics.getMin());
+ map.put("stdev", descriptiveStatistics.getStandardDeviation());
+ map.put("sum", descriptiveStatistics.getSum());
+ map.put("N", descriptiveStatistics.getN());
+ map.put("var", descriptiveStatistics.getVariance());
+ map.put("kurtosis", descriptiveStatistics.getKurtosis());
+ map.put("skewness", descriptiveStatistics.getSkewness());
+ map.put("popVar", descriptiveStatistics.getPopulationVariance());
+ map.put("geometricMean", descriptiveStatistics.getGeometricMean());
+ map.put("sumsq", descriptiveStatistics.getSumsq());
+
+ return new Tuple(map);
+ }
+
+
+ @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/17dac2bc/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 1632818..a881e53 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
@@ -5819,6 +5819,61 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertTrue(prediction == 600.0D);
}
+ @Test
+ public void testDescribe() throws Exception {
+ UpdateRequest updateRequest = new UpdateRequest();
+
+
+ updateRequest.add(id, "1", "price_f", "100.0", "col_s", "a", "order_i", "1");
+ updateRequest.add(id, "2", "price_f", "200.0", "col_s", "a", "order_i", "2");
+ updateRequest.add(id, "3", "price_f", "300.0", "col_s", "a", "order_i", "3");
+ updateRequest.add(id, "4", "price_f", "100.0", "col_s", "a", "order_i", "4");
+ updateRequest.add(id, "5", "price_f", "200.0", "col_s", "a", "order_i", "5");
+ updateRequest.add(id, "6", "price_f", "400.0", "col_s", "a", "order_i", "6");
+ updateRequest.add(id, "7", "price_f", "600.0", "col_s", "a", "order_i", "7");
+
+
+ updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
+
+ String expr1 = "search("+COLLECTIONORALIAS+", q=\"col_s:a\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
+
+ String cexpr = "let(a="+expr1+", b=col(a, price_f), tuple(stats=describe(b)))";
+
+ 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);
+ Map stats = (Map)tuple.get("stats");
+ Number min = (Number)stats.get("min");
+ Number max = (Number)stats.get("max");
+ Number mean = (Number)stats.get("mean");
+ Number stdev = (Number)stats.get("stdev");
+ Number popVar = (Number)stats.get("popVar");
+ Number skewness = (Number)stats.get("skewness");
+ Number kurtosis = (Number)stats.get("kurtosis");
+ Number var = (Number)stats.get("var");
+ Number geometricMean = (Number)stats.get("geometricMean");
+ Number N = (Number)stats.get("N");
+ assertEquals(min.doubleValue(), 100.0D, 0.0);
+ assertEquals(max.doubleValue(), 600.0D, 0.0);
+ assertEquals(N.doubleValue(), 7.0D, 0.0);
+ assertEquals(mean.doubleValue(), 271.42D, 0.5);
+ assertEquals(popVar.doubleValue(), 27755.10, 0.5);
+ assertEquals(kurtosis.doubleValue(), .70D, 0.5);
+ assertEquals(skewness.doubleValue(), 1.07D, 0.5);
+ assertEquals(var.doubleValue(), 32380.95D, 0.5);
+ assertEquals(geometricMean .doubleValue(), 224.56D, 0.5);
+ assertEquals(stdev .doubleValue(), 179.94D, 0.5);
+ }
+
@Test
public void testLength() throws Exception {