From 191c2ba5f54a87b267c6a897b329e82334e35209 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Tue, 30 May 2017 11:09:56 -0400 Subject: [PATCH] SOLR-10767: Add movingAvg Stream Evaluator --- .../apache/solr/handler/StreamHandler.java | 1 + .../solrj/io/eval/MovingAverageEvaluator.java | 79 +++++++++++++++++++ .../solrj/io/stream/StreamExpressionTest.java | 20 +++++ 3 files changed, 100 insertions(+) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingAverageEvaluator.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 2de8b158e7c..f79f86ee05e 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -193,6 +193,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("array", ArrayEvaluator.class) .withFunctionName("hist", HistogramEvaluator.class) .withFunctionName("anova", AnovaEvaluator.class) + .withFunctionName("movingAvg", MovingAverageEvaluator.class) // metrics .withFunctionName("min", MinMetric.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingAverageEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingAverageEvaluator.java new file mode 100644 index 00000000000..f84f6e2f6ae --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingAverageEvaluator.java @@ -0,0 +1,79 @@ +/* + * 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.List; + +import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; +import org.apache.commons.math3.util.MathArrays; +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 MovingAverageEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public MovingAverageEvaluator(StreamExpression expression, StreamFactory factory) throws IOException { + super(expression, factory); + } + + public List evaluate(Tuple tuple) throws IOException { + StreamEvaluator colEval = subEvaluators.get(0); + StreamEvaluator windowEval = subEvaluators.get(1); + + int window = ((Number)windowEval.evaluate(tuple)).intValue(); + List numbers = (List)colEval.evaluate(tuple); + + if(window > numbers.size()) { + throw new IOException("The window size cannot be larger then the array"); + } + + List moving = new ArrayList(); + + DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(window); + for(int i=0; i= window) { + moving.add(descriptiveStatistics.getMean()); + } + } + + return moving; + } + + @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 c5c459e9318..d82ca2a3de9 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 @@ -5838,6 +5838,26 @@ public class StreamExpressionTest extends SolrCloudTestCase { assertEquals((double)out.get("f-ratio"), 0.24169D, .0001); } + @Test + public void testMovingAverage() throws Exception { + String cexpr = "movingAvg(array(1,2,3,4,5,6,7), 4)"; + 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 tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List out = (List)tuples.get(0).get("return-value"); + assertTrue(out.size()==4); + assertEquals((double)out.get(0), 2.5, .0); + assertEquals((double)out.get(1), 3.5, .0); + assertEquals((double)out.get(2), 4.5, .0); + assertEquals((double)out.get(3), 5.5, .0); + } + @Test public void testScale() throws Exception {