SOLR-11572: Add recip Stream Evaluator to support reciprocal transformations

This commit is contained in:
Joel Bernstein 2018-10-30 09:41:59 -04:00
parent 5aa8ad5b14
commit 856e28d8cf
4 changed files with 79 additions and 1 deletions

View File

@ -272,6 +272,7 @@ public class Lang {
.withFunctionName("getRadius", GetRadiusEvaluator.class)
.withFunctionName("getSupportPoints", GetSupportPointsEvaluator.class)
.withFunctionName("pairSort", PairSortEvaluator.class)
.withFunctionName("recip", RecipEvaluator.class)
// Boolean Stream Evaluators
.withFunctionName("and", AndEvaluator.class)

View File

@ -0,0 +1,52 @@
/*
* 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.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.apache.commons.math3.analysis.function.Inverse;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class RecipEvaluator extends RecursiveNumericEvaluator implements OneValueWorker {
protected static final long serialVersionUID = 1L;
public RecipEvaluator(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{
Inverse inverse = new Inverse();
return inverse.value(((Number)value).doubleValue());
}
}
}

View File

@ -73,7 +73,7 @@ public class TestLang extends LuceneTestCase {
"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
"getSupportPoints", "pairSort", "log10", "plist"};
"getSupportPoints", "pairSort", "log10", "plist", "recip"};
@Test
public void testLang() {

View File

@ -1800,6 +1800,31 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertEquals(log.doubleValue(), 1.4842998393467859, 0.0);
}
@Test
public void testRecip() throws Exception {
String cexpr = "let(echo=true, a=array(10, 20, 30), b=recip(a), c=recip(30.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);
assertEquals(tuples.size(), 1);
Tuple tuple = tuples.get(0);
List<Number> logs = (List<Number>)tuple.get("b");
assertEquals(logs.size(), 3);
assertEquals(logs.get(0).doubleValue(), .1, 0.0);
assertEquals(logs.get(1).doubleValue(), .05, 0.0);
assertEquals(logs.get(2).doubleValue(), 0.03333333333333333, 0.0);
Number log = (Number)tuple.get("c");
assertEquals(log.doubleValue(), 0.03278688524590164, 0.0);
}
@Test
public void testPow() throws Exception {
String cexpr = "let(echo=true, a=array(10, 20, 30), b=pow(a, 2), c=pow(2, a), d=pow(10, 3), e=pow(a, array(1, 2, 3)))";