You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by da...@apache.org on 2018/10/31 09:30:25 UTC
[23/25] lucene-solr:jira/http2: SOLR-11572: Add recip Stream
Evaluator to support reciprocal transformations
SOLR-11572: Add recip Stream Evaluator to support reciprocal transformations
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/856e28d8
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/856e28d8
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/856e28d8
Branch: refs/heads/jira/http2
Commit: 856e28d8cf07cc34bc1361784bf00e7aceb3af97
Parents: 5aa8ad5
Author: Joel Bernstein <jb...@apache.org>
Authored: Tue Oct 30 09:41:59 2018 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Tue Oct 30 09:42:24 2018 -0400
----------------------------------------------------------------------
.../org/apache/solr/client/solrj/io/Lang.java | 1 +
.../client/solrj/io/eval/RecipEvaluator.java | 52 ++++++++++++++++++++
.../apache/solr/client/solrj/io/TestLang.java | 2 +-
.../solrj/io/stream/MathExpressionTest.java | 25 ++++++++++
4 files changed, 79 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/856e28d8/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 2be48e3..1777467 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)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/856e28d8/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecipEvaluator.java
----------------------------------------------------------------------
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.java
new file mode 100644
index 0000000..2daaac7
--- /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());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/856e28d8/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 e06b973..960eb50 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"};
@Test
public void testLang() {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/856e28d8/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 b5f9c0a..4642388 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);
+ }
+
+
@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)))";