Apache Lucene open-source search software
SOLR-11594: Add precision Stream Evaluator
Joel Bernstein6eea7f70a09f
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex 65e87bd37bb..f6b9686604c 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -272,6 +272,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("transpose", TransposeEvaluator.class).withFunctionName("unit", UnitEvaluator.class).withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)+ .withFunctionName("precision", PrecisionEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrecisionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrecisionEvaluator.javanew file mode 100644index 00000000000..532f2ea40c4--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrecisionEvaluator.java@@ -0,0 +1,51 @@+/*+ * 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 java.util.stream.Collectors;+import org.apache.commons.math3.util.Precision;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class PrecisionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public PrecisionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);++ if(2 != containedEvaluators.size()){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 2 value but found %d",expression,containedEvaluators.size()));+ }+ }++ @Override+ public Object doWork(Object value, Object value2) {+ if(null == value){+ return null;+ }+ else if(value instanceof List){+ return ((List<?>)value).stream().map(innerValue -> doWork(innerValue, ((Number)value2).intValue())).collect(Collectors.toList());+ }+ else{+ return Precision.round(((Number)value).doubleValue(), ((Number)value2).intValue());+ }+ }+}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.javaindex 9c3111626b7..5fe860893ed 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@@ -7089,7 +7089,28 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals(row3.get(2).longValue(), 16);}+ @Test+ public void testPrecision() throws Exception {+ String cexpr = "let(echo=true, a=precision(array(1.44445, 1, 2.00006), 4), b=precision(1.44445, 4))";+ 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> nums = (List<Number>)tuples.get(0).get("a");+ assertTrue(nums.size() == 3);+ assertEquals(nums.get(0).doubleValue(), 1.4445, 0.0);+ assertEquals(nums.get(1).doubleValue(), 1, 0.0);+ assertEquals(nums.get(2).doubleValue(), 2.0001, 0.0);+ double num = tuples.get(0).getDouble("b");+ assertEquals(num, 1.4445, 0.0);+ }@Testpublic void testMean() throws Exception {