lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbern...@apache.org
Subject lucene-solr:branch_7x: SOLR-11594: Add precision Stream Evaluator
Date Wed, 01 Nov 2017 17:53:08 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x ff35365b5 -> 1691a04ec


SOLR-11594: Add precision Stream Evaluator


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

Branch: refs/heads/branch_7x
Commit: 1691a04ec908e8b07229a917a09f27b2c7610c1b
Parents: ff35365
Author: Joel Bernstein <jbernste@apache.org>
Authored: Wed Nov 1 13:34:56 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Wed Nov 1 13:49:10 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../solrj/io/eval/PrecisionEvaluator.java       | 51 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 21 ++++++++
 3 files changed, 73 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1691a04e/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index 65e87bd..f6b9686 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 Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1691a04e/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrecisionEvaluator.java
----------------------------------------------------------------------
diff --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.java
new file mode 100644
index 0000000..532f2ea
--- /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());
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1691a04e/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
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.java
index 9c31116..5fe8608 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);
+  }
 
   @Test
   public void testMean() throws Exception {


Mime
View raw message