mirror of https://github.com/apache/lucene.git
SOLR-11225: Add cumulativeProbability Stream Evaluator
This commit is contained in:
parent
3f3a71ad65
commit
b3127ec582
|
@ -224,6 +224,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
|
||||||
.withFunctionName("kolmogorovSmirnov", KolmogorovSmirnovEvaluator.class)
|
.withFunctionName("kolmogorovSmirnov", KolmogorovSmirnovEvaluator.class)
|
||||||
.withFunctionName("ks", KolmogorovSmirnovEvaluator.class)
|
.withFunctionName("ks", KolmogorovSmirnovEvaluator.class)
|
||||||
.withFunctionName("asc", AscEvaluator.class)
|
.withFunctionName("asc", AscEvaluator.class)
|
||||||
|
.withFunctionName("cumulativeProbability", CumulativeProbabilityEvaluator.class)
|
||||||
|
|
||||||
// Boolean Stream Evaluators
|
// Boolean Stream Evaluators
|
||||||
.withFunctionName("and", AndEvaluator.class)
|
.withFunctionName("and", AndEvaluator.class)
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
* 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.Locale;
|
||||||
|
|
||||||
|
import org.apache.commons.math3.distribution.RealDistribution;
|
||||||
|
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 CumulativeProbabilityEvaluator extends ComplexEvaluator implements Expressible {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1;
|
||||||
|
|
||||||
|
public CumulativeProbabilityEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
|
||||||
|
super(expression, factory);
|
||||||
|
|
||||||
|
if(2 != subEvaluators.size()){
|
||||||
|
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting two values (regression result and a number) but found %d",expression,subEvaluators.size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Number evaluate(Tuple tuple) throws IOException {
|
||||||
|
StreamEvaluator a = subEvaluators.get(0);
|
||||||
|
StreamEvaluator b = subEvaluators.get(1);
|
||||||
|
Number number1 = (Number)b.evaluate(tuple);
|
||||||
|
RealDistribution rd= (RealDistribution)a.evaluate(tuple);
|
||||||
|
|
||||||
|
return rd.cumulativeProbability(number1.doubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -5287,6 +5287,23 @@ public class StreamExpressionTest extends SolrCloudTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCumulativeProbability() throws Exception {
|
||||||
|
String expr = "cumulativeProbability(normalDistribution(500, 40), 500)";
|
||||||
|
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
|
||||||
|
paramsLoc.set("expr", expr);
|
||||||
|
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 number = (Number)tuples.get(0).get("return-value");
|
||||||
|
assertTrue(number.doubleValue() == .5D);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue