Apache Lucene open-source search software
SOLR-12401: Add getValue() and setValue() Stream Evaluators
Joel11cfb864894e
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 941c1a1a9f2..a01a841e120 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@@ -244,6 +244,8 @@ public class Lang {.withFunctionName("chebyshev", ChebyshevEvaluator.class).withFunctionName("ones", OnesEvaluator.class).withFunctionName("zeros", ZerosEvaluator.class)+ .withFunctionName("getValue", GetValueEvaluator.class)+ .withFunctionName("setValue", SetValueEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetValueEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetValueEvaluator.javanew file mode 100644index 00000000000..1be563958e3--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetValueEvaluator.java@@ -0,0 +1,50 @@+/*+ * 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.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;+import org.apache.solr.client.solrj.io.Tuple;++public class GetValueEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public GetValueEvaluator(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 values but found %d",expression,containedEvaluators.size()));+ }+ }++ @Override+ public Object doWork(Object value1, Object value2) throws IOException {++ if(value1 instanceof Tuple) {+ Tuple tuple = (Tuple)value1;+ String key = (String)value2;+ key = key.replace("\"", "");+ return tuple.get(key);+ } else {+ throw new IOException("The getValue function expects a Tuple as the first parameter");+ }+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.javaindex b94c5dc58d5..0278839e06c 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java@@ -22,6 +22,8 @@ import java.math.BigDecimal;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;+import java.util.HashMap;+import java.util.Map;import java.util.List;import java.util.Locale;import java.util.Set;@@ -128,6 +130,17 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorkerelse if(value instanceof List){// normalize each value in the listreturn ((List<?>)value).stream().map(innerValue -> normalizeOutputType(innerValue)).collect(Collectors.toList());+ } else if(value instanceof Tuple && value.getClass().getEnclosingClass() == null) {+ //If its a tuple and not a inner class that has extended tuple, which is done in a number of cases so that mathematical models+ //can be contained within a tuple.++ Tuple tuple = (Tuple)value;+ Map map = new HashMap();+ for(Object o : tuple.fields.keySet()) {+ Object v = tuple.fields.get(o);+ map.put(o, normalizeOutputType(v));+ }+ return new Tuple(map);}else{// anything else can just be returned as isdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SetValueEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SetValueEvaluator.javanew file mode 100644index 00000000000..9ffc32f3342--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SetValueEvaluator.java@@ -0,0 +1,58 @@+/*+ * 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 java.util.Map;+import java.util.HashMap;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;+import org.apache.solr.client.solrj.io.Tuple;++public class SetValueEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public SetValueEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);++ if(3 != containedEvaluators.size()){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 3 values but found %d",expression,containedEvaluators.size()));+ }+ }++ @Override+ public Object doWork(Object... values) throws IOException {+ if(values[0] instanceof Tuple) {+ Tuple tuple = (Tuple)values[0];+ String key = (String)values[1];+ Object value = values[2];+ if(value instanceof String) {+ value = ((String)value).replace("\"", "");+ }+ key = key.replace("\"", "");+ Map map = new HashMap();+ map.putAll(tuple.fields);+ map.put(key, value);+ return new Tuple(map);+ } else {+ throw new IOException("The setValue function expects a Tuple as the first parameter");+ }+ }+}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 1e824daeb31..22b432f3d55 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@@ -69,7 +69,7 @@ public class TestLang extends LuceneTestCase {TemporalEvaluatorDayOfQuarter.FUNCTION_NAME, "abs", "add", "div", "mult", "sub", "log", "pow","mod", "ceil", "floor", "sin", "asin", "sinh", "cos", "acos", "cosh", "tan", "atan", "tanh", "round", "sqrt","cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan",- "earthMovers", "canberra", "chebyshev", "ones", "zeros"};+ "earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue"};@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 f936fee2688..ce1ad765485 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@@ -2285,6 +2285,37 @@ public class MathExpressionTest extends SolrCloudTestCase {assertEquals(row2.get(5).doubleValue(), 606.0, 0.0);}++ @Test+ public void testSetAndGetValue() throws Exception {+ String cexpr = "let(echo=true," ++ " a=describe(array(1,2,3,4,5,6,7))," ++ " b=getValue(a, geometricMean)," ++ " c=setValue(a, \"test\", add(b, 1))," ++ " d=getValue(c, test)," ++ " e=setValue(c, blah, array(8.11,9.55,10.1))," ++ " f=getValue(e, \"blah\"))";+ 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 mean = (Number)tuples.get(0).get("b");+ assertEquals(mean.doubleValue(), 3.3800151591412964, 0.0);+ Number mean1 = (Number)tuples.get(0).get("d");+ assertEquals(mean1.doubleValue(), 4.3800151591412964, 0.0);+ List<Number> vals = (List<Number>)tuples.get(0).get("f");+ assertEquals(vals.size(), 3);+ assertEquals(vals.get(0).doubleValue(), 8.11, 0);+ assertEquals(vals.get(1).doubleValue(), 9.55, 0);+ assertEquals(vals.get(2).doubleValue(), 10.1, 0);+ }++@Testpublic void testEbeDivide() throws Exception {String cexpr = "ebeDivide(array(2,4,6,8,10,12),array(1,2,3,4,5,6))";