SOLR-10813: Add arraySort Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-06-05 11:18:57 -04:00
parent 6a9830cdc0
commit 78d95014e7
3 changed files with 102 additions and 1 deletions

View File

@ -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)

View File

@ -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<Number> 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<Number> numbers1 = (List<Number>)colEval1.evaluate(tuple);
List<Number> numbers2 = new ArrayList();
numbers2.addAll(numbers1);
Collections.sort(numbers2, new Comparator<Number>() {
@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());
}
}

View File

@ -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<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(), 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 {