SOLR-11808: Add sumSq Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-12-31 13:48:02 -05:00
parent fbc8508e11
commit 692e5243e9
3 changed files with 74 additions and 0 deletions

View File

@ -287,6 +287,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("integrate", IntegrateEvaluator.class)
.withFunctionName("density", DensityEvaluator.class)
.withFunctionName("mannWhitney", MannWhitneyUEvaluator.class)
.withFunctionName("sumSq", SumSqEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,56 @@
/*
* 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.List;
import java.util.Locale;
import org.apache.commons.math3.stat.StatUtils;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class SumSqEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
private static final long serialVersionUID = 1;
public SumSqEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object value) throws IOException {
if(null == value){
return value;
}
else if(!(value instanceof List<?>)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a List",toExpression(constructingFactory), value.getClass().getSimpleName()));
}
List<Number> list = (List<Number>)value;
if(0 == list.size()){
return list;
}
double[] vec = new double[list.size()];
for(int i=0; i< vec.length; i++) {
vec[i] = list.get(i).doubleValue();
}
return StatUtils.sumSq(vec);
}
}

View File

@ -7549,6 +7549,23 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(out.get(2).doubleValue(), 10.0, .0);
}
@Test
public void testSumSq() throws Exception {
String cexpr = "sumSq(array(-3,-2.5, 10))";
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);
Number sumSq = (Number)tuples.get(0).get("return-value");
assertEquals(sumSq.doubleValue(), 115.25D, 0.0D);
}
@Test
public void testMonteCarlo() throws Exception {
String cexpr = "let(a=constantDistribution(10), b=constantDistribution(20), c=monteCarlo(add(sample(a), sample(b)), 10))";