Skip to content

Apache Lucene open-source search software

SOLR-10662: Add length Stream Evaluator

Joel Bernstein680dcc140d5c
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 1a990860d7f..bc17f9b18b5 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -174,6 +174,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("conv", ConvolutionEvaluator.class)
.withFunctionName("normalize", NormalizeEvaluator.class)
.withFunctionName("rev", ReverseEvaluator.class)
+ .withFunctionName("length", LengthEvaluator.class)
// metrics
.withFunctionName("min", MinMetric.class)
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LengthEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LengthEvaluator.java
new file mode 100644
index 00000000000..3ff0db3b30f
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LengthEvaluator.java
@@ -0,0 +1,60 @@
+/*
+ * 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.stream;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.eval.ComplexEvaluator;
+import org.apache.solr.client.solrj.io.eval.StreamEvaluator;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class LengthEvaluator extends ComplexEvaluator implements Expressible {
+
+ private static final long serialVersionUID = 1;
+
+ public LengthEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ public Number evaluate(Tuple tuple) throws IOException {
+ StreamEvaluator colEval1 = subEvaluators.get(0);
+ List<Number> numbers = (List<Number>)colEval1.evaluate(tuple);
+ return numbers.size();
+ }
+
+ @Override
+ public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException {
+ StreamExpression expression = new StreamExpression(factory.getFunctionName(getClass()));
+ return expression;
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) throws IOException {
+ return new Explanation(nodeId.toString())
+ .withExpressionType(ExpressionType.EVALUATOR)
+ .withFunctionName(factory.getFunctionName(getClass()))
+ .withImplementingClass(getClass().getName())
+ .withExpression(toExpression(factory).toString());
+ }
+}
\ No newline at end of file
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 2594d3323bb..4ba7ab46734 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
@@ -5453,6 +5453,58 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertTrue(prediction == 600.0D);
}
+
+ @Test
+ public void testLength() throws Exception {
+ UpdateRequest updateRequest = new UpdateRequest();
+
+
+ updateRequest.add(id, "1", "price_f", "100.0", "col_s", "a", "order_i", "1");
+ updateRequest.add(id, "2", "price_f", "200.0", "col_s", "a", "order_i", "2");
+ updateRequest.add(id, "3", "price_f", "300.0", "col_s", "a", "order_i", "3");
+ updateRequest.add(id, "4", "price_f", "100.0", "col_s", "a", "order_i", "4");
+ updateRequest.add(id, "5", "price_f", "200.0", "col_s", "a", "order_i", "5");
+ updateRequest.add(id, "6", "price_f", "400.0", "col_s", "a", "order_i", "6");
+ updateRequest.add(id, "7", "price_f", "600.0", "col_s", "a", "order_i", "7");
+
+ updateRequest.add(id, "8", "price_f", "200.0", "col_s", "b", "order_i", "1");
+ updateRequest.add(id, "9", "price_f", "400.0", "col_s", "b", "order_i", "2");
+ updateRequest.add(id, "10", "price_f", "600.0", "col_s", "b", "order_i", "3");
+ updateRequest.add(id, "11", "price_f", "200.0", "col_s", "b", "order_i", "4");
+ updateRequest.add(id, "12", "price_f", "400.0", "col_s", "b", "order_i", "5");
+ updateRequest.add(id, "13", "price_f", "800.0", "col_s", "b", "order_i", "6");
+ updateRequest.add(id, "14", "price_f", "1200.0", "col_s", "b", "order_i", "7");
+ updateRequest.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
+
+ String expr1 = "search("+COLLECTIONORALIAS+", q=\"col_s:a\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
+ String expr2 = "search("+COLLECTIONORALIAS+", q=\"col_s:b\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
+
+ String cexpr = "let(a="+expr1+", b="+expr2+", c=col(a, price_f), d=col(b, price_f), e=regress(c, d), tuple(regress=e, p=predict(e, 300), l=length(d)))";
+
+ 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);
+ Tuple tuple = tuples.get(0);
+ Map regression = (Map)tuple.get("regress");
+ double slope = (double)regression.get("slope");
+ double intercept= (double) regression.get("intercept");
+ double length = tuple.getDouble("l");
+
+ assertTrue(slope == 2.0D);
+ assertTrue(intercept == 0.0D);
+ double prediction = tuple.getDouble("p");
+ assertTrue(prediction == 600.0D);
+ assertTrue(length == 7);
+ }
+
@Test
public void testNormalize() throws Exception {
UpdateRequest updateRequest = new UpdateRequest();