From 3edb23471b4801d34b0b9a675606115b4f28463b Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Mon, 30 Oct 2017 20:02:23 -0400 Subject: [PATCH] SOLR-11565: Add unit Stream Evaluator to support unitizing of vectors --- .../apache/solr/handler/StreamHandler.java | 1 + .../client/solrj/io/eval/UnitEvaluator.java | 77 +++++++++++++++++++ .../solrj/io/stream/StreamExpressionTest.java | 34 ++++++++ 3 files changed, 112 insertions(+) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/UnitEvaluator.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 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 Evaluators 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 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 values = (List)value; + double[] doubles = new double[values.size()]; + for(int i=0; i 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.java index 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 tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + List> out = (List>)tuples.get(0).get("a"); + assertEquals(out.size(), 2); + List 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 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 array3 = (List)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 {