diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java index 9229dcf9370..455576cf018 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java @@ -281,6 +281,7 @@ public class Lang { .withFunctionName("rtrim", RightShiftEvaluator.class) .withFunctionName("repeat", RepeatEvaluator.class) .withFunctionName("natural", NaturalEvaluator.class) + .withFunctionName("movingMAD", MovingMADEvaluator.class) // Boolean Stream Evaluators diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMADEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMADEvaluator.java new file mode 100644 index 00000000000..6bbe8ba29df --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMADEvaluator.java @@ -0,0 +1,71 @@ +/* + * 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 java.util.Locale; + +import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; +import org.apache.solr.client.solrj.io.stream.expr.StreamExpression; +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; + +public class MovingMADEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker { + protected static final long serialVersionUID = 1L; + + public MovingMADEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{ + super(expression, factory); + } + + @Override + public Object doWork(Object first, Object second) throws IOException{ + if(null == first){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory))); + } + if(null == second){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory))); + } + if(!(first instanceof List)){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a List",toExpression(constructingFactory), first.getClass().getSimpleName())); + } + if(!(second instanceof Number)){ + throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName())); + } + + List values = (List)first; + int window = ((Number)second).intValue(); + + List moving = new ArrayList<>(); + DescriptiveStatistics slider = new DescriptiveStatistics(window); + for(Object value : values){ + slider.addValue(((Number)value).doubleValue()); + if(slider.getN() >= window){ + double[] doubles = slider.getValues(); + double mean = slider.getMean(); + double total = 0; + for(double d : doubles) { + total+=Math.abs(d-mean); + } + moving.add(total/window); + } + } + + return moving; + } + +} diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java index 2632e1482b0..040a1d90163 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java @@ -74,7 +74,7 @@ public class TestLang extends LuceneTestCase { "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate", "getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius", "getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export", - "zplot", "natural", "repeat"}; + "zplot", "natural", "repeat", "movingMAD"}; @Test public void testLang() { diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java index 45698d9dfd3..7102987c6c4 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java @@ -4313,6 +4313,26 @@ public class MathExpressionTest extends SolrCloudTestCase { assertEquals((double) out.get(3), 5.5, .0); } + @Test + public void testMovingMAD() throws Exception { + String cexpr = "movingMAD(array(1,2,3,4,5,6,9), 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).doubleValue(), 1, .0); + assertEquals((double) out.get(1).doubleValue(), 1, .0); + assertEquals((double) out.get(2).doubleValue(), 1, .0); + assertEquals((double) out.get(3).doubleValue(), 1.5, .0); + } + @Test public void testMannWhitney() throws Exception { String cexpr = "mannWhitney(array(0.15,0.10,0.11,0.24,0.08,0.08,0.10,0.10,0.10,0.12,0.04,0.07), " +