Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-11400: Add logNormalDistribution Stream Evaluator

Joel Bernstein6b3447defd54
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 4f50ca9057b..481075908f3 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -29,6 +29,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import org.apache.commons.math3.distribution.LogNormalDistribution;
import org.apache.solr.client.solrj.io.ModelCache;
import org.apache.solr.client.solrj.io.SolrClientCache;
import org.apache.solr.client.solrj.io.Tuple;
@@ -303,6 +304,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("constantDistribution", ConstantDistributionEvaluator.class)
.withFunctionName("weibullDistribution", WeibullDistributionEvaluator.class)
.withFunctionName("mean", MeanEvaluator.class)
+ .withFunctionName("mode", ModeEvaluator.class)
+ .withFunctionName("logNormalDistribution", LogNormalDistributionEvaluator.class)
// Boolean Stream Evaluators
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LogNormalDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LogNormalDistributionEvaluator.java
new file mode 100644
index 00000000000..6b15bee7dfe
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LogNormalDistributionEvaluator.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.LogNormalDistribution;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class LogNormalDistributionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
+
+ private static final long serialVersionUID = 1;
+
+ public LogNormalDistributionEvaluator(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 shape = (Number)first;
+ Number scale = (Number)second;
+
+ return new LogNormalDistribution(scale.doubleValue(), shape.doubleValue());
+ }
+}
\ No newline at end of file
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ModeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ModeEvaluator.java
new file mode 100644
index 00000000000..b2529c64058
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ModeEvaluator.java
@@ -0,0 +1,64 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.commons.math3.stat.StatUtils;
+
+
+public class ModeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public ModeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+
+ if(1 != containedEvaluators.size()){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size()));
+ }
+ }
+
+ @Override
+ public Object doWork(Object value) throws IOException{
+ if(null == value){
+ throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is null", constructingFactory.getFunctionName(getClass())));
+ }
+ else if(value instanceof List){
+ List<Number> c = (List<Number>) value;
+ double[] data = new double[c.size()];
+ for(int i=0; i< c.size(); i++) {
+ data[i] = c.get(i).doubleValue();
+ }
+
+ double[] mode = StatUtils.mode(data);
+ List<Number> l = new ArrayList();
+ for(double d : mode) {
+ l.add(d);
+ }
+
+ return l;
+ }
+ else{
+ throw new IOException(String.format(Locale.ROOT, "Unable to find %s(...) because the value is not a collection, instead a %s was found", constructingFactory.getFunctionName(getClass()), value.getClass().getSimpleName()));
+ }
+ }
+}
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 44b8e54b5d0..318f8f6d839 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
@@ -6555,11 +6555,13 @@ public class StreamExpressionTest extends SolrCloudTestCase {
@Test
public void testWeibullDistribution() throws Exception {
String cexpr = "let(echo=true, " +
- "a=percentile(sample(weibullDistribution(1, 10),5000), 50), " +
- "b=percentile(sample(weibullDistribution(1, 50),5000), 50), " +
- "c=percentile(sample(weibullDistribution(1, 100),5000), 50)," +
- "d=percentile(sample(weibullDistribution(4, 10),5000), 50)," +
- "e=percentile(sample(weibullDistribution(8, 10),5000), 50))";
+ "a=describe(sample(weibullDistribution(.1, 10),10000)), " +
+ "b=describe(sample(weibullDistribution(.5, 10),10000)), " +
+ "c=describe(sample(weibullDistribution(1, 10),10000))," +
+ "d=describe(sample(weibullDistribution(6, 10),10000))," +
+ "e=mean(sample(weibullDistribution(1, 10),10000))," +
+ "f=mean(sample(weibullDistribution(1, 20),10000))," +
+ "g=mean(sample(weibullDistribution(1, 30),10000)))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
paramsLoc.set("expr", cexpr);
@@ -6570,17 +6572,68 @@ public class StreamExpressionTest extends SolrCloudTestCase {
solrStream.setStreamContext(context);
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
- Number a = (Number)tuples.get(0).get("a");
- Number b = (Number)tuples.get(0).get("b");
- Number c = (Number)tuples.get(0).get("c");
+ Map a = (Map)tuples.get(0).get("a");
+ Map b = (Map)tuples.get(0).get("b");
+ Map c = (Map)tuples.get(0).get("c");
+ Map d = (Map)tuples.get(0).get("d");
+
+ Number sa = (Number)a.get("skewness");
+ Number sb = (Number)b.get("skewness");
+ Number sc = (Number)c.get("skewness");
+ Number sd = (Number)d.get("skewness");
+
+ //Test shape change
+ assertTrue(sa.doubleValue() > sb.doubleValue());
+ assertTrue(sb.doubleValue() > sc.doubleValue());
+ assertTrue(sc.doubleValue() > sd.doubleValue());
+ assertTrue(sd.doubleValue() < 0.0);
+
+ //Test scale change
+
+ Number e = (Number)tuples.get(0).get("e");
+ Number f = (Number)tuples.get(0).get("f");
+ Number g = (Number)tuples.get(0).get("g");
+
+ assertTrue(e.doubleValue() < f.doubleValue());
+ assertTrue(f.doubleValue() < g.doubleValue());
+ }
+
+ @Test
+ public void testLogNormalDistribution() throws Exception {
+ String cexpr = "let(echo=true, " +
+ "a=describe(sample(logNormalDistribution(.1, 0),10000)), " +
+ "b=describe(sample(logNormalDistribution(.3, 0),10000)), " +
+ "c=describe(sample(logNormalDistribution(.6, 0),10000))," +
+ "d=mean(sample(logNormalDistribution(.3, 0),10000)), " +
+ "e=mean(sample(logNormalDistribution(.3, 2),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");
+ Map c = (Map)tuples.get(0).get("c");
+
+ Number sa = (Number)a.get("skewness");
+ Number sb = (Number)b.get("skewness");
+ Number sc = (Number)c.get("skewness");
+
+ assertTrue(sa.doubleValue() < sb.doubleValue());
+ assertTrue(sb.doubleValue() < sc.doubleValue());
+
Number d = (Number)tuples.get(0).get("d");
Number e = (Number)tuples.get(0).get("e");
- assertTrue(a.doubleValue() < b.doubleValue());
- assertTrue(b.doubleValue() < c.doubleValue());
- assertTrue(a.doubleValue() < d.doubleValue());
+
assertTrue(d.doubleValue() < e.doubleValue());
- }
+ }
@Test
public void testMean() throws Exception {