mirror of https://github.com/apache/lucene.git
SOLR-10767: Add movingAvg Stream Evaluator
This commit is contained in:
parent
366cd7cab7
commit
191c2ba5f5
|
@ -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)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue