Apache Lucene open-source search software
SOLR-11354: Add factorial and movingMedian Stream Evaluators
Joel Bernstein565b6c6e5ac3
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex bde5f3b6b9c..25566a589d5 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -297,6 +297,9 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("sumDifference", SumDifferenceEvaluator.class).withFunctionName("meanDifference", MeanDifferenceEvaluator.class).withFunctionName("primes", PrimesEvaluator.class)+ .withFunctionName("factorial", FactorialEvaluator.class)+ .withFunctionName("movingMedian", MovingMedianEvaluator.class)+ .withFunctionName("sphere", SphereEvaluator.class)// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.javanew file mode 100644index 00000000000..bd8f88d8559--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.java@@ -0,0 +1,54 @@+/*+ * 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.math.BigDecimal;+import java.util.List;+import java.util.Locale;+import java.util.stream.Collectors;++import org.apache.commons.math3.util.CombinatoricsUtils;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class FactorialEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {+ protected static final long serialVersionUID = 1L;++ public FactorialEvaluator(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){+++ if(null == value){+ return null;+ }+ else if(value instanceof List){+ return ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());+ }+ else{+ return CombinatoricsUtils.factorialDouble(((BigDecimal)value).intValue());+ }+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMedianEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMedianEvaluator.javanew file mode 100644index 00000000000..4ae69fb44a6--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MovingMedianEvaluator.java@@ -0,0 +1,68 @@+/*+ * 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.stat.descriptive.DescriptiveStatistics;+import org.apache.commons.math3.stat.descriptive.rank.Percentile;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class MovingMedianEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public MovingMedianEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first, Object second) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }+ if(null == second){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ }+ if(!(first instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a List",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if(!(second instanceof Number)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ List<?> values = (List<?>)first;+ int window = ((Number)second).intValue();++ List<Number> moving = new ArrayList<>();+ DescriptiveStatistics slider = new DescriptiveStatistics(window);+ Percentile percentile = new Percentile();+ for(Object value : values){+ slider.addValue(((Number)value).doubleValue());+ if(slider.getN() >= window){+ double median = percentile.evaluate(slider.getValues(), 50);+ moving.add(median);+ }+ }++ return moving;+ }++}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LetStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LetStream.javaindex 6dd73f5fdd0..05ca8511f11 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LetStream.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/LetStream.java@@ -48,8 +48,17 @@ public class LetStream extends TupleStream implements Expressible {List<StreamExpressionNamedParameter> namedParams = factory.getNamedOperands(expression);//Get all the named params+ boolean echo = false;+ String currentName = null;for(StreamExpressionParameter np : namedParams) {String name = ((StreamExpressionNamedParameter)np).getName();+ currentName = name;++ if(name.equals("echo")) {+ echo = true;+ continue;+ }+StreamExpressionParameter param = ((StreamExpressionNamedParameter)np).getParameter();if(factory.isEvaluator((StreamExpression)param)) {StreamEvaluator evaluator = factory.constructEvaluator((StreamExpression) param);@@ -60,11 +69,20 @@ public class LetStream extends TupleStream implements Expressible {}}- if(streamExpressions.size() != 1){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting 1 stream but found %d",expression, streamExpressions.size()));+ if(streamExpressions.size() > 0) {+ stream = factory.constructStream(streamExpressions.get(0));+ } else {+ StreamExpression tupleExpression = new StreamExpression("tuple");+ if(!echo) {+ tupleExpression.addParameter(new StreamExpressionNamedParameter(currentName, currentName));+ } else {+ Set<String> names = letParams.keySet();+ for(String name : names) {+ tupleExpression.addParameter(new StreamExpressionNamedParameter(name, name));+ }+ }+ stream = factory.constructStream(tupleExpression);}-- stream = factory.constructStream(streamExpressions.get(0));}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.javaindex 6fa5d670540..51a9737d3d7 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@@ -6411,7 +6411,7 @@ public class StreamExpressionTest extends SolrCloudTestCase {List<Tuple> tuples = getTuples(solrStream);assertTrue(tuples.size() == 1);Number dotProduct = (Number)tuples.get(0).get("return-value");- assertTrue(dotProduct.doubleValue()== 182);+ assertTrue(dotProduct.doubleValue() == 182);}@@ -6499,10 +6499,30 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertTrue(tuples.size() == 1);List<Number> out = (List<Number>)tuples.get(0).get("return-value");assertTrue(out.size()==4);- assertEquals((double)out.get(0), 2.5, .0);- assertEquals((double)out.get(1), 3.5, .0);- assertEquals((double)out.get(2), 4.5, .0);- assertEquals((double)out.get(3), 5.5, .0);+ assertEquals((double) out.get(0), 2.5, .0);+ assertEquals((double) out.get(1), 3.5, .0);+ assertEquals((double) out.get(2), 4.5, .0);+ assertEquals((double) out.get(3), 5.5, .0);+ }+++ @Test+ public void testMovingMedian() throws Exception {+ String cexpr = "movingMedian(array(1,2,6,9,10,12,15), 5)";+ 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<Number> out = (List<Number>)tuples.get(0).get("return-value");+ assertTrue(out.size()==3);+ assertEquals(out.get(0).doubleValue(), 6.0, .0);+ assertEquals(out.get(1).doubleValue(), 9.0, .0);+ assertEquals(out.get(2).doubleValue(), 10.0, .0);}