Apache Lucene open-source search software
SOLR-11572: Add recip Stream Evaluator to support reciprocal transformations
Joel Bernstein856e28d8cf07
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.javaindex 2be48e31b3f..17774679aa5 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java@@ -272,6 +272,7 @@ public class Lang {.withFunctionName("getRadius", GetRadiusEvaluator.class).withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class).withFunctionName("pairSort", PairSortEvaluator.class)+ .withFunctionName("recip", RecipEvaluator.class)// Boolean Stream Evaluators.withFunctionName("and", AndEvaluator.class)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecipEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecipEvaluator.javanew file mode 100644index 00000000000..2daaac7df21--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecipEvaluator.java@@ -0,0 +1,52 @@+/*+ * 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.analysis.function.Inverse;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class RecipEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {+ protected static final long serialVersionUID = 1L;++ public RecipEvaluator(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){+ if(null == value) {+ return null;+ }+ else if(value instanceof List) {+ return ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());+ }+ else{+ Inverse inverse = new Inverse();+ return inverse.value(((Number)value).doubleValue());+ }+ }+}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.javaindex e06b9739256..960eb50ef3f 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java@@ -73,7 +73,7 @@ public class TestLang extends LuceneTestCase {"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors","convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate","getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",- "getSupportPoints", "pairSort", "log10", "plist"};+ "getSupportPoints", "pairSort", "log10", "plist", "recip"};@Testpublic void testLang() {diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.javaindex b5f9c0a96f2..4642388500d 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java@@ -1800,6 +1800,31 @@ public class MathExpressionTest extends SolrCloudTestCase {assertEquals(log.doubleValue(), 1.4842998393467859, 0.0);}++ @Test+ public void testRecip() throws Exception {+ String cexpr = "let(echo=true, a=array(10, 20, 30), b=recip(a), c=recip(30.5))";+ 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);+ assertEquals(tuples.size(), 1);+ Tuple tuple = tuples.get(0);+ List<Number> logs = (List<Number>)tuple.get("b");+ assertEquals(logs.size(), 3);+ assertEquals(logs.get(0).doubleValue(), .1, 0.0);+ assertEquals(logs.get(1).doubleValue(), .05, 0.0);+ assertEquals(logs.get(2).doubleValue(), 0.03333333333333333, 0.0);++ Number log = (Number)tuple.get("c");+ assertEquals(log.doubleValue(), 0.03278688524590164, 0.0);+ }++@Testpublic void testPow() throws Exception {String cexpr = "let(echo=true, a=array(10, 20, 30), b=pow(a, 2), c=pow(2, a), d=pow(10, 3), e=pow(a, array(1, 2, 3)))";