Apache Lucene open-source search software
SOLR-11565: Add unit Stream Evaluator to support unitizing of vectors
Joel Bernstein3edb23471b48
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex 21a17bc5b24..4a5ef331d7a 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 Evaluatorsdiff --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.javanew file mode 100644index 00000000000..8be990d55f0--- /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");+ }+ }+}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.javaindex cbf96f5aca9..8196cf5edd5 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);+ }++@Testpublic void testAddAll() throws Exception {