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 cbb99108dff..f5ccbc8f2f3 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -191,6 +191,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("finddelay", FindDelayEvaluator.class) .withFunctionName("sequence", SequenceEvaluator.class) .withFunctionName("array", ArrayEvaluator.class) + .withFunctionName("hist", HistogramEvaluator.class) // metrics .withFunctionName("min", MinMetric.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HistogramEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HistogramEvaluator.java new file mode 100644 index 00000000000..c6916984ecd --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HistogramEvaluator.java @@ -0,0 +1,98 @@ +/* + * 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.math3.random.EmpiricalDistribution; +import org.apache.commons.math3.stat.descriptive.SummaryStatistics; +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 HistogramEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public HistogramEvaluator(StreamExpression expression, StreamFactory factory) throws IOException { + super(expression, factory); + } + + public List evaluate(Tuple tuple) throws IOException { + + StreamEvaluator colEval1 = subEvaluators.get(0); + + List numbers1 = (List)colEval1.evaluate(tuple); + double[] column1 = new double[numbers1.size()]; + + for(int i=0; i binList = new ArrayList(); + + List summaries = empiricalDistribution.getBinStats(); + for(SummaryStatistics statisticalSummary : summaries) { + Map map = new HashMap(); + map.put("max", statisticalSummary.getMax()); + map.put("mean", statisticalSummary.getMean()); + map.put("min", statisticalSummary.getMin()); + map.put("stdev", statisticalSummary.getStandardDeviation()); + map.put("sum", statisticalSummary.getSum()); + map.put("N", statisticalSummary.getN()); + map.put("var", statisticalSummary.getVariance()); + binList.add(map); + } + + return binList; + } + + + + @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 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 c570d95c733..6c15197e31e 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 @@ -5227,6 +5227,53 @@ public class StreamExpressionTest extends SolrCloudTestCase { } } + @Test + public void testHist() throws Exception { + String expr = "hist(sequence(100, 0, 1), 10)"; + ModifiableSolrParams paramsLoc = new ModifiableSolrParams(); + paramsLoc.set("expr", expr); + 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 tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List hist = (List)tuples.get(0).get("return-value"); + assertTrue(hist.size() == 10); + for(int i=0; i)tuples.get(0).get("return-value"); + assertTrue(hist.size() == 5); + for(int i=0; i tuples = getTuples(solrStream); assertTrue(tuples.size() == 1); - List out = (List)tuples.get(0).get("out"); + List out = (List)tuples.get(0).get("return-value"); assertTrue(out.size() == 6); assertTrue(out.get(0).intValue() == 1); assertTrue(out.get(1).intValue() == 2); @@ -5764,7 +5811,7 @@ public class StreamExpressionTest extends SolrCloudTestCase { solrStream.setStreamContext(context); tuples = getTuples(solrStream); assertTrue(tuples.size() == 1); - out = (List)tuples.get(0).get("out"); + out = (List)tuples.get(0).get("return-value"); assertTrue(out.size() == 6); assertTrue(out.get(0).doubleValue() == 1.122D); assertTrue(out.get(1).doubleValue() == 2.222D);