Skip to content

Apache Lucene open-source search software

SOLR-11350: Add primes Stream Evaluator

Joel Bernstein1544839309d6
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 f7e59c3e8ca..bde5f3b6b9c 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -296,6 +296,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("probability", ProbabilityEvaluator.class)
.withFunctionName("sumDifference", SumDifferenceEvaluator.class)
.withFunctionName("meanDifference", MeanDifferenceEvaluator.class)
+ .withFunctionName("primes", PrimesEvaluator.class)
// Boolean Stream Evaluators
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrimesEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrimesEvaluator.java
new file mode 100644
index 00000000000..94a36287375
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PrimesEvaluator.java
@@ -0,0 +1,58 @@
+/*
+ * 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.List;
+import java.util.Locale;
+
+import org.apache.commons.math3.primes.Primes;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class PrimesEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public PrimesEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+
+ if(2 != containedEvaluators.size()){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting two values but found %d",expression, containedEvaluators.size()));
+ }
+ }
+
+ @Override
+ public Object doWork(Object... values) throws IOException {
+ if(2 != values.length){
+ throw new IOException(String.format(Locale.ROOT,"%s(...) only works with 2 values but %d were provided", constructingFactory.getFunctionName(getClass()), values.length));
+ }
+
+ int sizeNum = ((Number)values[0]).intValue();
+ int startNum = ((Number)values[1]).intValue();
+ List<Number> primes = new ArrayList();
+
+ for(int i=0; i< sizeNum; i++) {
+ int prime = Primes.nextPrime(startNum);
+ primes.add(prime);
+ startNum = prime;
+ ++startNum;
+ }
+
+ return primes;
+ }
+}
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 9b2b18618b6..6fa5d670540 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
@@ -5857,6 +5857,35 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
+ @Test
+ public void testPrimes() throws Exception {
+ String cexpr = "primes(10, 0)";
+ 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);
+ List<Number> asort = (List<Number>)tuple.get("return-value");
+ assertEquals(asort.size(), 10);
+ assertEquals(asort.get(0).intValue(), 2);
+ assertEquals(asort.get(1).intValue(), 3);
+ assertEquals(asort.get(2).intValue(), 5);
+ assertEquals(asort.get(3).intValue(), 7);
+ assertEquals(asort.get(4).intValue(), 11);
+ assertEquals(asort.get(5).intValue(), 13);
+ assertEquals(asort.get(6).intValue(), 17);
+ assertEquals(asort.get(7).intValue(), 19);
+ assertEquals(asort.get(8).intValue(), 23);
+ assertEquals(asort.get(9).intValue(), 29);
+ }
+
@Test
public void testAscend() throws Exception {
String cexpr = "asc(array(11.5, 12.3, 4, 3, 1, 0))";