lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbern...@apache.org
Subject [2/2] lucene-solr:master: SOLR-12862: Add log10 Stream Evaluator and allow the pow Stream Evaluator to accept a vector of exponents
Date Mon, 15 Oct 2018 19:50:54 GMT
SOLR-12862: Add log10 Stream Evaluator and allow the pow Stream Evaluator to accept a vector
of exponents


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

Branch: refs/heads/master
Commit: 6c0fbe5a9d544060c42c4a1ec241a71c47d14bb8
Parents: 1ccd555
Author: Joel Bernstein <jbernste@apache.org>
Authored: Mon Oct 15 15:09:40 2018 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Mon Oct 15 15:47:03 2018 -0400

----------------------------------------------------------------------
 .../org/apache/solr/client/solrj/io/Lang.java   |  1 +
 .../client/solrj/io/eval/Log10Evaluator.java    | 50 +++++++++++++++++
 .../client/solrj/io/eval/PowerEvaluator.java    | 55 ++++++++++++++----
 .../solrj/io/stream/MathExpressionTest.java     | 59 ++++++++++++++++++++
 4 files changed, 153 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6c0fbe5a/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 75131ca..7cc842f 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
@@ -303,6 +303,7 @@ public class Lang {
         .withFunctionName("mult", MultiplyEvaluator.class)
         .withFunctionName("sub", SubtractEvaluator.class)
         .withFunctionName("log", NaturalLogEvaluator.class)
+        .withFunctionName("log10", Log10Evaluator.class)
         .withFunctionName("pow", PowerEvaluator.class)
         .withFunctionName("mod", ModuloEvaluator.class)
         .withFunctionName("ceil", CeilingEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6c0fbe5a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Log10Evaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Log10Evaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Log10Evaluator.java
new file mode 100644
index 0000000..d8bc95d
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Log10Evaluator.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.List;
+import java.util.Locale;
+import java.util.stream.Collectors;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class Log10Evaluator extends RecursiveNumericEvaluator implements OneValueWorker {
+  protected static final long serialVersionUID = 1L;
+
+  public Log10Evaluator(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{
+      return Math.log10(((Number)value).doubleValue());
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6c0fbe5a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PowerEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PowerEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PowerEvaluator.java
index 38d71c0..4de0906 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PowerEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PowerEvaluator.java
@@ -14,6 +14,7 @@
  * 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;
@@ -27,7 +28,7 @@ import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
 public class PowerEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
   protected static final long serialVersionUID = 1L;
   
-  public PowerEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+  public PowerEvaluator(StreamExpression expression, StreamFactory factory) throws IOException
{
     super(expression, factory);
 
     if(2 != containedEvaluators.size()){
@@ -36,26 +37,56 @@ public class PowerEvaluator extends RecursiveNumericEvaluator implements
TwoValu
   }
 
   @Override
-  public Object doWork(Object first, Object second) throws IOException{
+  public Object doWork(Object first, Object second) throws IOException {
     
-    if(null == first || null == second){
+    if(null == first || null == second) {
       return null;
     }
 
     if(first instanceof Number) {
       Number value = (Number) first;
-      Number exponent = (Number) second;
-      return Math.pow(value.doubleValue(), exponent.doubleValue());
-    } else {
+      if(second instanceof Number) {
+        Number exponent = (Number) second;
+        return Math.pow(value.doubleValue(), exponent.doubleValue());
+      } else if(second instanceof List)  {
+        List<Number> exponents = (List<Number>) second;
+        List<Number> pows = new ArrayList();
+        for(Number exponent : exponents) {
+          pows.add(Math.pow(value.doubleValue(), exponent.doubleValue()));
+        }
+        return pows;
+      } else {
+        throw new IOException("The second parameter to the pow function must either be a
scalar or list of scalars");
+      }
+    } else if(first instanceof List) {
       List<Number> values = (List<Number>) first;
-      Number exponent = (Number) second;
+      if(second instanceof Number) {
+        Number exponent = (Number) second;
 
-      List<Number> out = new ArrayList(values.size());
-      for(Number value : values) {
-        out.add(Math.pow(value.doubleValue(), exponent.doubleValue()));
-      }
+        List<Number> out = new ArrayList(values.size());
+        for (Number value : values) {
+          out.add(Math.pow(value.doubleValue(), exponent.doubleValue()));
+        }
+
+        return out;
+      } else if(second instanceof List) {
 
-      return out;
+        List<Number> out = new ArrayList(values.size());
+        List<Number> exponents = (List<Number>)second;
+        if(values.size() != exponents.size()) {
+          throw new IOException("The pow function requires vectors of equal size if two vectors
are provided.");
+        }
+
+        for(int i=0; i<exponents.size(); i++) {
+          out.add(Math.pow(values.get(i).doubleValue(), exponents.get(i).doubleValue()));
+        }
+
+        return out;
+      } else {
+        throw new IOException("The second parameter to the pow function must either be a
scalar or list of scalars");
+      }
+    } else {
+      throw new IOException("The first parameter to the pow function must either be a scalar
or list of scalars");
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6c0fbe5a/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 45633e3..78fc2ce 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
@@ -1761,6 +1761,65 @@ public class MathExpressionTest extends SolrCloudTestCase {
   }
 
   @Test
+  public void testLog10() throws Exception {
+    String cexpr = "let(echo=true, a=array(10, 20, 30), b=log10(a), c=log10(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(), 1.3010299956639813, 0.0);
+    assertEquals(logs.get(2).doubleValue(), 1.4771212547196624, 0.0);
+
+    Number log = (Number)tuple.get("c");
+    assertEquals(log.doubleValue(), 1.4842998393467859, 0.0);
+  }
+
+  @Test
+  public 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)))";
+    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> pows = (List<Number>)tuple.get("b");
+    assertEquals(pows.size(), 3);
+    assertEquals(pows.get(0).doubleValue(), 100, 0.0);
+    assertEquals(pows.get(1).doubleValue(), 400, 0.0);
+    assertEquals(pows.get(2).doubleValue(), 900, 0.0);
+
+    pows = (List<Number>)tuple.get("c");
+    assertEquals(pows.size(), 3);
+    assertEquals(pows.get(0).doubleValue(), 1024, 0.0);
+    assertEquals(pows.get(1).doubleValue(), 1048576, 0.0);
+    assertEquals(pows.get(2).doubleValue(), 1073741824, 0.0);
+
+    double p = tuple.getDouble("d");
+    assertEquals(p, 1000, 0.0);
+
+    pows = (List<Number>)tuple.get("e");
+    assertEquals(pows.size(), 3);
+    assertEquals(pows.get(0).doubleValue(), 10, 0.0);
+    assertEquals(pows.get(1).doubleValue(), 400, 0.0);
+    assertEquals(pows.get(2).doubleValue(), 27000, 0.0);
+
+  }
+
+  @Test
   public void testTermVectors() throws Exception {
     // Test termVectors with only documents and default termVector settings
     String cexpr = "let(echo=true," +


Mime
View raw message