Apache Solr open-source search software
SOLR-10724: Add describe Stream Evaluator
Joel Bernstein2d184581a7e7
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex a2a49a0e108..9fd7c8b1950 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)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.javanew file mode 100644index 00000000000..e086ebf2df4--- /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 filediff --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 16328188db8..a881e537388 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);+ }+@Testpublic void testLength() throws Exception {