You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2018/05/28 17:35:06 UTC

lucene-solr:master: SOLR-12401: Add getValue() and setValue() Stream Evaluators

Repository: lucene-solr
Updated Branches:
  refs/heads/master d03973063 -> 11cfb8648


SOLR-12401: Add getValue() and setValue() Stream Evaluators


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/11cfb864
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/11cfb864
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/11cfb864

Branch: refs/heads/master
Commit: 11cfb864894e99cc5953bae19ab29d07599a4e15
Parents: d039730
Author: Joel <jo...@alfresco.com>
Authored: Mon May 28 13:29:26 2018 -0400
Committer: Joel <jo...@alfresco.com>
Committed: Mon May 28 13:29:26 2018 -0400

----------------------------------------------------------------------
 .../org/apache/solr/client/solrj/io/Lang.java   |  2 +
 .../client/solrj/io/eval/GetValueEvaluator.java | 50 +++++++++++++++++
 .../solrj/io/eval/RecursiveEvaluator.java       | 13 +++++
 .../client/solrj/io/eval/SetValueEvaluator.java | 58 ++++++++++++++++++++
 .../apache/solr/client/solrj/io/TestLang.java   |  2 +-
 .../solrj/io/stream/MathExpressionTest.java     | 31 +++++++++++
 6 files changed, 155 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
----------------------------------------------------------------------
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.java
index 941c1a1..a01a841 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 Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetValueEvaluator.java
----------------------------------------------------------------------
diff --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.java
new file mode 100644
index 0000000..1be5639
--- /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");
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java
----------------------------------------------------------------------
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.java
index b94c5dc..0278839 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, ValueWorker
     else if(value instanceof List){
       // normalize each value in the list
       return ((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 is

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SetValueEvaluator.java
----------------------------------------------------------------------
diff --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.java
new file mode 100644
index 0000000..9ffc32f
--- /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");
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
----------------------------------------------------------------------
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.java
index 1e824da..22b432f 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"};
 
   @Test
   public void testLang() {

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/11cfb864/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
----------------------------------------------------------------------
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.java
index f936fee..ce1ad76 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);
+  }
+
+
   @Test
   public void testEbeDivide() throws Exception {
     String cexpr = "ebeDivide(array(2,4,6,8,10,12),array(1,2,3,4,5,6))";