lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject [30/50] [abbrv] lucene-solr:feature/autoscaling: SOLR-11415: Add betaDistribution Stream Evaluator
Date Tue, 03 Oct 2017 13:39:11 GMT
SOLR-11415: Add betaDistribution Stream Evaluator


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/633af794
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/633af794
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/633af794

Branch: refs/heads/feature/autoscaling
Commit: 633af79406566b2323f589c83640f13b196a0312
Parents: 97f246b
Author: Joel Bernstein <jbernste@apache.org>
Authored: Fri Sep 29 09:57:23 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Fri Sep 29 09:57:45 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  2 +
 .../io/eval/BetaDistributionEvaluator.java      | 48 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 45 ++++++++++++++++++
 3 files changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/633af794/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index 127c44a..16a31fb 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -307,6 +307,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("logNormalDistribution", LogNormalDistributionEvaluator.class)
         .withFunctionName("zipFDistribution", ZipFDistributionEvaluator.class)
         .withFunctionName("gammaDistribution", GammaDistributionEvaluator.class)
+        .withFunctionName("betaDistribution", BetaDistributionEvaluator.class)
+
         // Boolean Stream Evaluators
 
         .withFunctionName("and", AndEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/633af794/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/BetaDistributionEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/BetaDistributionEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/BetaDistributionEvaluator.java
new file mode 100644
index 0000000..288ecb9
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/BetaDistributionEvaluator.java
@@ -0,0 +1,48 @@
+/*
+ * 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.BetaDistribution;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class BetaDistributionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker
{
+
+  private static final long serialVersionUID = 1;
+
+  public BetaDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException {
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object first, Object second) throws IOException{
+    if(null == first){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found
for the first value",toExpression(constructingFactory)));
+    }
+    if(null == second){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found
for the second value",toExpression(constructingFactory)));
+    }
+
+    Number shape1 = (Number)first;
+    Number shape2 = (Number)second;
+
+    return new BetaDistribution(shape1.doubleValue(), shape2.doubleValue());
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/633af794/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
diff --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.java
index 6f11382..949d224 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
@@ -6399,6 +6399,51 @@ public class StreamExpressionTest extends SolrCloudTestCase {
   }
 
 
+  @Test
+  public void testBetaDistribution() throws Exception {
+    String cexpr = "let(a=sample(betaDistribution(1, 5), 50000), b=hist(a, 11), c=col(b,
N))";
+    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> counts = (List<Number>)tuples.get(0).get("c");
+
+    int lastCount = Integer.MAX_VALUE;
+    for(Number number : counts) {
+      int current = number.intValue();
+      if(current > lastCount) {
+        throw new Exception("This beta distribution should be descending");
+      } else {
+        lastCount = current;
+      }
+    }
+
+    cexpr = "let(a=sample(betaDistribution(5, 1), 50000), b=hist(a, 11), c=col(b, N))";
+    paramsLoc = new ModifiableSolrParams();
+    paramsLoc.set("expr", cexpr);
+    paramsLoc.set("qt", "/stream");
+    solrStream = new SolrStream(url, paramsLoc);
+    context = new StreamContext();
+    solrStream.setStreamContext(context);
+    tuples = getTuples(solrStream);
+    assertTrue(tuples.size() == 1);
+    counts = (List<Number>)tuples.get(0).get("c");
+
+    lastCount = Integer.MIN_VALUE;
+    for(Number number : counts) {
+      int current = number.intValue();
+      if(current < lastCount) {
+        throw new Exception("This beta distribution should be ascending");
+      } else {
+        lastCount = current;
+      }
+    }
+  }
 
   @Test
   public void testEnumeratedDistribution() throws Exception {


Mime
View raw message