Apache Lucene open-source search software
SOLR-11808: Add sumSq Stream Evaluator
Joel Bernstein692e5243e9ae
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex ae15260c0ac..73dea3c2471 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -287,6 +287,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("integrate", IntegrateEvaluator.class).withFunctionName("density", DensityEvaluator.class).withFunctionName("mannWhitney", MannWhitneyUEvaluator.class)+ .withFunctionName("sumSq", SumSqEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumSqEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumSqEvaluator.javanew file mode 100644index 00000000000..a2097ec0f68--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumSqEvaluator.java@@ -0,0 +1,56 @@+/*+ * 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.List;+import java.util.Locale;++import org.apache.commons.math3.stat.StatUtils;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class SumSqEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ private static final long serialVersionUID = 1;++ public SumSqEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if(null == value){+ return value;+ }+ else if(!(value instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a List",toExpression(constructingFactory), value.getClass().getSimpleName()));+ }++ List<Number> list = (List<Number>)value;++ if(0 == list.size()){+ return list;+ }++ double[] vec = new double[list.size()];+ for(int i=0; i< vec.length; i++) {+ vec[i] = list.get(i).doubleValue();+ }++ return StatUtils.sumSq(vec);+ }+}\ 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 4af8e43c5f8..2662fd23dd7 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@@ -7549,6 +7549,23 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals(out.get(2).doubleValue(), 10.0, .0);}+ @Test+ public void testSumSq() throws Exception {+ String cexpr = "sumSq(array(-3,-2.5, 10))";+ 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);+ Number sumSq = (Number)tuples.get(0).get("return-value");+ assertEquals(sumSq.doubleValue(), 115.25D, 0.0D);+ }++@Testpublic void testMonteCarlo() throws Exception {String cexpr = "let(a=constantDistribution(10), b=constantDistribution(20), c=monteCarlo(add(sample(a), sample(b)), 10))";