SOLR-11567: Add triangularDistribution Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-10-31 19:44:02 -04:00
parent 42c49c25d6
commit 5633268a45
3 changed files with 81 additions and 0 deletions

View File

@ -271,6 +271,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("matrix", MatrixEvaluator.class)
.withFunctionName("transpose", TransposeEvaluator.class)
.withFunctionName("unit", UnitEvaluator.class)
.withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)
// Boolean Stream Evaluators

View File

@ -0,0 +1,46 @@
/*
* 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 org.apache.commons.math3.distribution.TriangularDistribution;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class TriangularDistributionEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
private static final long serialVersionUID = 1;
public TriangularDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object... values) throws IOException {
if (values.length != 3) {
throw new IOException("Triangular distribution requires three numeric parameters low, mode, high");
}
double low = ((Number) values[0]).doubleValue();
double mode = ((Number) values[1]).doubleValue();
double high = ((Number) values[2]).doubleValue();
return new TriangularDistribution(low, mode, high);
}
}

View File

@ -7025,6 +7025,40 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
@Test
public void testTriangularDistribution() throws Exception {
String cexpr = "let(echo=true, " +
"a=describe(sample(triangularDistribution(10, 15, 30),10000)), " +
"b=describe(sample(triangularDistribution(10, 25, 30),10000)), " +
")";
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);
Map a = (Map)tuples.get(0).get("a");
Map b = (Map)tuples.get(0).get("b");
Number sa = (Number)a.get("skewness");
Number sb = (Number)b.get("skewness");
Number mina = (Number)a.get("min");
Number maxa = (Number)a.get("max");
assertTrue(sa.doubleValue() > 0);
assertTrue(sb.doubleValue() < 0);
assertEquals(mina.doubleValue(), 10, .5);
assertEquals(maxa.doubleValue(), 30, .5);
}
@Test
public void testMean() throws Exception {
String cexpr = "mean(array(1,2,3,4,5))";