Apache Lucene open-source search software
SOLR-11697: Add geometricDistribution Stream Evaluator
Joel Bernstein70767b109f4e
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex a0a83c1e6d3..9446463c600 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -279,6 +279,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("diff", TimeDifferencingEvaluator.class).withFunctionName("corrPValues", CorrelationSignificanceEvaluator.class).withFunctionName("normalizeSum", NormalizeSumEvaluator.class)+ .withFunctionName("geometricDistribution", GeometricDistributionEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GeometricDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GeometricDistributionEvaluator.javanew file mode 100644index 00000000000..5874af3ac9c--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GeometricDistributionEvaluator.java@@ -0,0 +1,44 @@+/*+ * 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.GeometricDistribution;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class GeometricDistributionEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {++ private static final long serialVersionUID = 1;++ public GeometricDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }++ Number prob = (Number)first;++ return new GeometricDistribution(prob.doubleValue());+ }+}\ No newline at end of filediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PoissonDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PoissonDistributionEvaluator.javaindex 864a039d5ad..d3e89a9a8ad 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PoissonDistributionEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PoissonDistributionEvaluator.java@@ -39,6 +39,6 @@ public class PoissonDistributionEvaluator extends RecursiveNumericEvaluator implNumber mean = (Number)first;- return new PoissonDistribution(mean.intValue());+ return new PoissonDistribution(mean.doubleValue());}}\ No newline at end of filediff --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.javaindex 8f3c3f5fb32..c9fa396a195 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@@ -6765,6 +6765,45 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals(cprob.doubleValue(), 0.5265621985303708, 0.0);}+ @Test+ public void testGeometricDistribution() throws Exception {+ String cexpr = "let(a=geometricDistribution(.2)," ++ " b=geometricDistribution(.5)," ++ " c=geometricDistribution(.8)," ++ " d=sample(a, 10000)," ++ " e=sample(b, 10000)," ++ " f=sample(c, 10000)," ++ " g=freqTable(d)," ++ " h=freqTable(e)," ++ " i=freqTable(f)," ++ " tuple(g=g, h=h, i=i))";++ 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<Map> listg = (List<Map>)tuples.get(0).get("g");+ Map mapg = listg.get(0);+ double pctg = (double) mapg.get("pct");+ assertEquals(pctg, .2, .02 );++ List<Map> listh = (List<Map>)tuples.get(0).get("h");+ Map maph = listh.get(0);+ double pcth = (double)maph.get("pct");+ assertEquals(pcth, .5, .02 );++ List<Map> listi = (List<Map>)tuples.get(0).get("i");+ Map mapi = listi.get(0);+ double pcti = (double)mapi.get("pct");+ assertEquals(pcti, .8, .02 );+ }+@Testpublic void testBinomialDistribution() throws Exception {String cexpr = "let(a=binomialDistribution(100, .50)," +