lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cpoersc...@apache.org
Subject [1/4] lucene-solr:jira/solr-8668: SOLR-10767: Add movingAvg Stream Evaluator
Date Tue, 30 May 2017 16:08:25 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/jira/solr-8668 5b0b17396 -> 22e5e3ae8


SOLR-10767: Add movingAvg Stream Evaluator


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/191c2ba5
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/191c2ba5
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/191c2ba5

Branch: refs/heads/jira/solr-8668
Commit: 191c2ba5f54a87b267c6a897b329e82334e35209
Parents: 366cd7c
Author: Joel Bernstein <jbernste@apache.org>
Authored: Tue May 30 11:09:56 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Tue May 30 11:10:43 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../solrj/io/eval/MovingAverageEvaluator.java   | 79 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 20 +++++
 3 files changed, 100 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/191c2ba5/solr/core/src/java/org/apache/solr/handler/StreamHandler.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 2de8b15..f79f86e 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)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/191c2ba5/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingAverageEvaluator.java
----------------------------------------------------------------------
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 0000000..f84f6e2
--- /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 file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/191c2ba5/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
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 c5c459e..d82ca2a 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);
+  }
+
 
   @Test
   public void testScale() throws Exception {


Mime
View raw message