Skip to content

Apache Lucene open-source search software

SOLR-11568: Add matrix Stream Evaluator to support efficient matrix operations

Joel Bernsteine0239b9e84ab
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 51e057520a1..046a7f5c8a3 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -268,6 +268,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class)
.withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
.withFunctionName("loess", LoessEvaluator.class)
+ .withFunctionName("matrix", MatrixEvaluator.class)
// Boolean Stream Evaluators
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java
new file mode 100644
index 00000000000..abde021b9c1
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/Matrix.java
@@ -0,0 +1,59 @@
+/*
+ * 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.util.List;
+import java.util.ArrayList;
+
+import java.util.Iterator;
+
+public class Matrix implements Iterable {
+
+ private double[][] data;
+
+ public Matrix(double[][] data) {
+ this.data = data;
+ }
+
+ public Iterator iterator() {
+ return new MatrixIterator(data);
+ }
+
+ private static class MatrixIterator implements Iterator {
+
+ private double[][] d;
+ private int index;
+
+ public MatrixIterator(double[][] data) {
+ d = data;
+ }
+
+ public Object next() {
+ double[] row = d[index++];
+ List list = new ArrayList();
+ for(double value : row) {
+ list.add(value);
+ }
+
+ return list;
+ }
+
+ public boolean hasNext() {
+ return index < d.length;
+ }
+ }
+}
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MatrixEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MatrixEvaluator.java
new file mode 100644
index 00000000000..f17af331a80
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MatrixEvaluator.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 org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class MatrixEvaluator extends RecursiveNumericListEvaluator implements ManyValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public MatrixEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+
+ if(containedEvaluators.size() < 1){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting at least one value but found %d",expression,containedEvaluators.size()));
+ }
+ }
+
+ @Override
+ public Object doWork(Object... values) throws IOException {
+ double[][] data = new double[values.length][];
+ for(int i=0; i<values.length; i++) {
+ List<Number> vec = (List<Number>)values[i];
+ double[] array = new double[vec.size()];
+ for(int j=0; j<array.length; j++) {
+ array[j] = vec.get(j).doubleValue();
+ }
+ data[i] = array;
+ }
+
+ return new Matrix(data);
+ }
+}
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 ac06b58359e..a035f3e46ad 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
@@ -6038,6 +6038,33 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
+ @Test
+ public void testMatrix() throws Exception {
+ String cexpr = "matrix(array(1, 2, 3), rev(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("return-value");
+
+ List<Number> array1 = out.get(0);
+ assertEquals(array1.size(), 3);
+ assertEquals(array1.get(0).doubleValue(), 1.0, 0.0);
+ assertEquals(array1.get(1).doubleValue(), 2.0, 0.0);
+ assertEquals(array1.get(2).doubleValue(), 3.0, 0.0);
+
+ List<Number> array2 = out.get(1);
+ assertEquals(array2.size(), 3);
+ assertEquals(array2.get(0).doubleValue(), 6.0, 0.0);
+ assertEquals(array2.get(1).doubleValue(), 5.0, 0.0);
+ assertEquals(array2.get(2).doubleValue(), 4.0, 0.0);
+ }
+
@Test
public void testAddAll() throws Exception {
String cexpr = "addAll(array(1, 2, 3), array(4.5, 5.5, 6.5), array(7,8,9))";