Apache Lucene open-source search software
SOLR-12971: Add pivot Stream Evaluator to pivot facet co-occurrence counts into a matrix
Joel Bernstein80efc239995b
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.javaindex 17774679aa5..200f07b5fde 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@@ -273,6 +273,8 @@ public class Lang {.withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class).withFunctionName("pairSort", PairSortEvaluator.class).withFunctionName("recip", RecipEvaluator.class)+ .withFunctionName("pivot", PivotEvaluator.class)+// Boolean Stream Evaluators.withFunctionName("and", AndEvaluator.class)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PivotEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PivotEvaluator.javanew file mode 100644index 00000000000..919db149fcd--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PivotEvaluator.java@@ -0,0 +1,111 @@+/*+ * 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.ArrayList;+import java.util.Locale;+import java.util.List;+import java.util.Set;+import java.util.TreeSet;+import java.util.Map;+import java.util.HashMap;++import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;+import org.apache.solr.client.solrj.io.Tuple;++public class PivotEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public PivotEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);++ if(4 != containedEvaluators.size()){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 4 values but found %d",expression,containedEvaluators.size()));+ }+ }++ @Override+ public Object doWork(Object[] values) throws IOException {+ if(values.length != 4) {+ throw new IOException("The pivot function requires four parameters.");+ }++ Object value1 = values[0];+ Object value2 = values[1];+ Object value3 = values[2];+ Object value4 = values[3];++ if(value1 instanceof List) {+ List<Tuple> tuples = (List<Tuple>)value1;+ String x = (String)value2;+ x = x.replace("\"", "");+ String y = (String)value3;+ y = y.replace("\"", "");++ String vlabel = (String)value4;+ vlabel = vlabel.replace("\"", "");++ Set<String> xset = new TreeSet();+ Set<String> yset = new TreeSet();++ for(int i=0; i<tuples.size(); i++) {+ Tuple tuple = tuples.get(i);+ xset.add(tuple.getString(x));+ yset.add(tuple.getString(y));+ }++ double[][] data = new double[xset.size()][yset.size()];++ List<String> xlabels = new ArrayList(xset.size());+ Map<String, Integer> xindexes = new HashMap();+ int xindex = 0;+ for (String xlabel :xset) {+ xlabels.add(xlabel);+ xindexes.put(xlabel, xindex);+ ++xindex;+ }++ List<String> ylabels = new ArrayList(yset.size());+ Map<String, Integer> yindexes = new HashMap();+ int yindex = 0;+ for (String ylabel : yset) {+ ylabels.add(ylabel);+ yindexes.put(ylabel, yindex);+ ++yindex;+ }++ for(Tuple tuple : tuples) {+ String xlabel = tuple.getString(x);+ String ylabel = tuple.getString(y);+ int xi = xindexes.get(xlabel);+ int yi = yindexes.get(ylabel);+ double val = tuple.getDouble(vlabel);+ data[xi][yi] = val;+ }++ Matrix matrix = new Matrix(data);+ matrix.setRowLabels(xlabels);+ matrix.setColumnLabels(ylabels);+ return matrix;+ } else {+ throw new IOException("The getValue function expects a list of tuples as the first parameter");+ }+ }+}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.javaindex 960eb50ef3f..98a6d45f3c3 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", "recip"};+ "getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot"};@Testpublic void testLang() {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.javaindex 4642388500d..9c93cedfeb0 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@@ -2163,6 +2163,50 @@ public class MathExpressionTest extends SolrCloudTestCase {assertEquals(termVectors.get(0).size(), 0);}+ @Test+ public void testPivot() throws Exception {+ String cexpr = "let(echo=true," ++ " a=list(tuple(fx=x1, fy=f1, fv=add(1,1)), " ++ " tuple(fx=x1, fy=f2, fv=add(1,3)), " ++ " tuple(fx=x2, fy=f1, fv=add(1,7)), " ++ " tuple(fx=x3, fy=f1, fv=add(1,4))," ++ " tuple(fx=x3, fy=f3, fv=add(1,7)))," ++ " b=pivot(a, fx, fy, fv)," ++ " c=getRowLabels(b)," ++ " d=getColumnLabels(b))";+ 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);+ List<List<Number>> matrix = (List<List<Number>>)tuples.get(0).get("b");+ List<Number> row1 = matrix.get(0);+ assertEquals(row1.get(0).doubleValue(), 2.0,0);+ assertEquals(row1.get(1).doubleValue(), 4.0,0);+ assertEquals(row1.get(2).doubleValue(), 0,0);+ List<Number> row2 = matrix.get(1);+ assertEquals(row2.get(0).doubleValue(), 8.0,0);+ assertEquals(row2.get(1).doubleValue(), 0,0);+ assertEquals(row2.get(2).doubleValue(), 0,0);+ List<Number> row3 = matrix.get(2);+ assertEquals(row3.get(0).doubleValue(), 5.0,0);+ assertEquals(row3.get(1).doubleValue(), 0,0);+ assertEquals(row3.get(2).doubleValue(), 8.0,0);++ List<String> rowLabels = (List<String>)tuples.get(0).get("c");+ assertEquals(rowLabels.get(0), "x1");+ assertEquals(rowLabels.get(1), "x2");+ assertEquals(rowLabels.get(2), "x3");+ List<String> columnLabels = (List<String>)tuples.get(0).get("d");+ assertEquals(columnLabels.get(0), "f1");+ assertEquals(columnLabels.get(1), "f2");+ assertEquals(columnLabels.get(2), "f3");+ }+@Testpublic void testEbeSubtract() throws Exception {String cexpr = "let(echo=true," +