lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject [40/50] [abbrv] lucene-solr:jira/solr-11285-sim: SOLR-11565: Add unit Stream Evaluator to support unitizing of vectors
Date Tue, 31 Oct 2017 14:02:22 GMT
SOLR-11565: Add unit Stream Evaluator to support unitizing of vectors


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

Branch: refs/heads/jira/solr-11285-sim
Commit: 3edb23471b4801d34b0b9a675606115b4f28463b
Parents: 489ca23
Author: Joel Bernstein <jbernste@apache.org>
Authored: Mon Oct 30 20:02:23 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Mon Oct 30 20:17:22 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../client/solrj/io/eval/UnitEvaluator.java     | 77 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 34 +++++++++
 3 files changed, 112 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3edb2347/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 21a17bc..4a5ef33 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -270,6 +270,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("loess", LoessEvaluator.class)
         .withFunctionName("matrix", MatrixEvaluator.class)
         .withFunctionName("transpose", TransposeEvaluator.class)
+        .withFunctionName("unit", UnitEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3edb2347/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/UnitEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/UnitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/UnitEvaluator.java
new file mode 100644
index 0000000..8be990d
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/UnitEvaluator.java
@@ -0,0 +1,77 @@
+/*
+ * 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.commons.math3.linear.RealVector;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import java.util.List;
+import java.util.ArrayList;
+
+public class UnitEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+  protected static final long serialVersionUID = 1L;
+
+  public UnitEvaluator(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) throws IOException{
+    if(null == value){
+      return null;
+    } else if(value instanceof Matrix) {
+      Matrix matrix = (Matrix) value;
+      double[][] data = matrix.getData();
+      double[][] unitData = new double[data.length][];
+      for(int i=0; i<data.length; i++) {
+        double[] row = data[i];
+        ArrayRealVector vector = new ArrayRealVector(row);
+        double[] unitRow = vector.unitVector().toArray();
+        unitData[i] = unitRow;
+      }
+
+      return new Matrix(unitData);
+    } else if(value instanceof List) {
+      List<Number> values = (List<Number>)value;
+      double[] doubles = new double[values.size()];
+      for(int i=0; i<doubles.length; i++) {
+       doubles[i] = values.get(i).doubleValue();
+      }
+
+      ArrayRealVector vector = new ArrayRealVector(doubles);
+      RealVector unitVector = vector.unitVector();
+      List<Number> unitList = new ArrayList(doubles.length);
+      double[] unitArray = unitVector.toArray();
+      for(double d : unitArray) {
+        unitList.add(d);
+      }
+
+      return unitList;
+    } else {
+      throw new IOException("The unit function expects either a numeric array or matrix as
a parameter");
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3edb2347/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 cbf96f5..8196cf5 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
@@ -6096,6 +6096,40 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertEquals(array3.get(1).doubleValue(), 6.0, 0.0);
   }
 
+  @Test
+  public void testUnit() throws Exception {
+    String cexpr = "let(echo=true, a=unit(matrix(array(1,2,3), array(4,5,6))), b=unit(array(4,5,6)))";
+    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<List<Number>> out = (List<List<Number>>)tuples.get(0).get("a");
+    assertEquals(out.size(), 2);
+    List<Number> array1 = out.get(0);
+    assertEquals(array1.size(), 3);
+    assertEquals(array1.get(0).doubleValue(), 0.2672612419124244, 0.0);
+    assertEquals(array1.get(1).doubleValue(), 0.5345224838248488, 0.0);
+    assertEquals(array1.get(2).doubleValue(), 0.8017837257372732, 0.0);
+
+    List<Number> array2 = out.get(1);
+    assertEquals(array2.size(), 3);
+    assertEquals(array2.get(0).doubleValue(), 0.4558423058385518, 0.0);
+    assertEquals(array2.get(1).doubleValue(), 0.5698028822981898, 0.0);
+    assertEquals(array2.get(2).doubleValue(), 0.6837634587578276, 0.0);
+
+    List<Number> array3 = (List<Number>)tuples.get(0).get("b");
+    assertEquals(array3.size(), 3);
+    assertEquals(array2.get(0).doubleValue(), 0.4558423058385518, 0.0);
+    assertEquals(array2.get(1).doubleValue(), 0.5698028822981898, 0.0);
+    assertEquals(array2.get(2).doubleValue(), 0.6837634587578276, 0.0);
+  }
+
+
 
   @Test
   public void testAddAll() throws Exception {


Mime
View raw message