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 d4029ffef72..7889bf7df30 100644 --- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java @@ -195,6 +195,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware, .withFunctionName("hist", HistogramEvaluator.class) .withFunctionName("anova", AnovaEvaluator.class) .withFunctionName("movingAvg", MovingAverageEvaluator.class) + .withFunctionName("arraySort", ArraySortEvaluator.class) // metrics .withFunctionName("min", MinMetric.class) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArraySortEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArraySortEvaluator.java new file mode 100644 index 00000000000..dabc615d736 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArraySortEvaluator.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.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.apache.solr.client.solrj.io.Tuple; +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 ArraySortEvaluator extends ComplexEvaluator implements Expressible { + + private static final long serialVersionUID = 1; + + public ArraySortEvaluator(StreamExpression expression, StreamFactory factory) throws IOException { + super(expression, factory); + } + + public List evaluate(Tuple tuple) throws IOException { + + if(subEvaluators.size() != 1) { + throw new IOException("Array sort evaluator expects 1 parameters found: "+subEvaluators.size()); + } + + StreamEvaluator colEval1 = subEvaluators.get(0); + + List numbers1 = (List)colEval1.evaluate(tuple); + List numbers2 = new ArrayList(); + numbers2.addAll(numbers1); + Collections.sort(numbers2, new Comparator() { + @Override + public int compare(Number o1, Number o2) { + Double d1 = o1.doubleValue(); + Double d2 = o2.doubleValue(); + return d1.compareTo(d2); + } + }); + return numbers2; + } + + @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 8ca52d1ae57..3466af29270 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 @@ -5808,9 +5808,32 @@ public class StreamExpressionTest extends SolrCloudTestCase { tuple = tuples.get(0); p = tuple.getDouble("return-value"); assertEquals(p, 2.4, 0.001); - } + @Test + public void testArraySort() throws Exception { + String cexpr = "arraySort(array(11.5, 12.3, 4, 3, 1, 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 tuples = getTuples(solrStream); + assertTrue(tuples.size() == 1); + Tuple tuple = tuples.get(0); + List asort = (List)tuple.get("return-value"); + assertEquals(asort.size(), 6); + assertEquals(asort.get(0).doubleValue(), 0, 0.0); + assertEquals(asort.get(1).doubleValue(), 1, 0.0); + assertEquals(asort.get(2).doubleValue(), 3, 0.0); + assertEquals(asort.get(3).doubleValue(), 4, 0.0); + assertEquals(asort.get(4).doubleValue(), 11.5, 0.0); + assertEquals(asort.get(5).doubleValue(), 12.3, 0.0); + } @Test public void testCumulativeProbability() throws Exception {