SOLR-10724: Add describe Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-05-22 18:41:08 -04:00
parent 59aa5d7515
commit 2d184581a7
3 changed files with 148 additions and 0 deletions

View File

@ -183,6 +183,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("copyOfRange", CopyOfRangeEvaluator.class)
.withFunctionName("percentile", PercentileEvaluator.class)
.withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class)
.withFunctionName("describe", DescribeEvaluator.class)
// metrics
.withFunctionName("min", MinMetric.class)

View File

@ -0,0 +1,92 @@
/*
* 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.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
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 DescribeEvaluator extends ComplexEvaluator implements Expressible {
private static final long serialVersionUID = 1;
public DescribeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
public Tuple evaluate(Tuple tuple) throws IOException {
if(subEvaluators.size() != 1) {
throw new IOException("describe expects 1 column as a parameters");
}
StreamEvaluator colEval = subEvaluators.get(0);
List<Number> numbers = (List<Number>)colEval.evaluate(tuple);
DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
for(Number n : numbers) {
descriptiveStatistics.addValue(n.doubleValue());
}
Map map = new HashMap();
map.put("max", descriptiveStatistics.getMax());
map.put("mean", descriptiveStatistics.getMean());
map.put("min", descriptiveStatistics.getMin());
map.put("stdev", descriptiveStatistics.getStandardDeviation());
map.put("sum", descriptiveStatistics.getSum());
map.put("N", descriptiveStatistics.getN());
map.put("var", descriptiveStatistics.getVariance());
map.put("kurtosis", descriptiveStatistics.getKurtosis());
map.put("skewness", descriptiveStatistics.getSkewness());
map.put("popVar", descriptiveStatistics.getPopulationVariance());
map.put("geometricMean", descriptiveStatistics.getGeometricMean());
map.put("sumsq", descriptiveStatistics.getSumsq());
return new Tuple(map);
}
@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

@ -5819,6 +5819,61 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertTrue(prediction == 600.0D);
}
@Test
public void testDescribe() 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.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
String expr1 = "search("+COLLECTIONORALIAS+", q=\"col_s:a\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
String cexpr = "let(a="+expr1+", b=col(a, price_f), tuple(stats=describe(b)))";
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 stats = (Map)tuple.get("stats");
Number min = (Number)stats.get("min");
Number max = (Number)stats.get("max");
Number mean = (Number)stats.get("mean");
Number stdev = (Number)stats.get("stdev");
Number popVar = (Number)stats.get("popVar");
Number skewness = (Number)stats.get("skewness");
Number kurtosis = (Number)stats.get("kurtosis");
Number var = (Number)stats.get("var");
Number geometricMean = (Number)stats.get("geometricMean");
Number N = (Number)stats.get("N");
assertEquals(min.doubleValue(), 100.0D, 0.0);
assertEquals(max.doubleValue(), 600.0D, 0.0);
assertEquals(N.doubleValue(), 7.0D, 0.0);
assertEquals(mean.doubleValue(), 271.42D, 0.5);
assertEquals(popVar.doubleValue(), 27755.10, 0.5);
assertEquals(kurtosis.doubleValue(), .70D, 0.5);
assertEquals(skewness.doubleValue(), 1.07D, 0.5);
assertEquals(var.doubleValue(), 32380.95D, 0.5);
assertEquals(geometricMean .doubleValue(), 224.56D, 0.5);
assertEquals(stdev .doubleValue(), 179.94D, 0.5);
}
@Test
public void testLength() throws Exception {