Apache Lucene open-source search software
SOLR-10767: Add movingAvg Stream Evaluator
Joel Bernstein191c2ba5f54a
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex 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.javanew file mode 100644index 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<Number> evaluate(Tuple tuple) throws IOException {+ StreamEvaluator colEval = subEvaluators.get(0);+ StreamEvaluator windowEval = subEvaluators.get(1);++ int window = ((Number)windowEval.evaluate(tuple)).intValue();+ List<Number> numbers = (List<Number>)colEval.evaluate(tuple);++ if(window > numbers.size()) {+ throw new IOException("The window size cannot be larger then the array");+ }++ List<Number> moving = new ArrayList();++ DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(window);+ for(int i=0; i<numbers.size(); i++) {+ descriptiveStatistics.addValue(numbers.get(i).doubleValue());+ if(descriptiveStatistics.getN() >= 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 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 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<Tuple> tuples = getTuples(solrStream);+ assertTrue(tuples.size() == 1);+ List<Number> out = (List<Number>)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);+ }+@Testpublic void testScale() throws Exception {