mirror of https://github.com/apache/lucene.git
SOLR-10684: Add finddelay Stream Evaluator
This commit is contained in:
parent
cd567b985a
commit
47781e3938
|
@ -185,6 +185,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
|
|||
.withFunctionName("percentile", PercentileEvaluator.class)
|
||||
.withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class)
|
||||
.withFunctionName("describe", DescribeEvaluator.class)
|
||||
.withFunctionName("finddelay", FindDelayEvaluator.class)
|
||||
|
||||
// metrics
|
||||
.withFunctionName("min", MinMetric.class)
|
||||
|
|
|
@ -90,7 +90,9 @@ public class Tuple implements Cloneable, MapWriter {
|
|||
}
|
||||
|
||||
if(o instanceof Long) {
|
||||
return (Long)o;
|
||||
return (Long) o;
|
||||
} else if (o instanceof Number) {
|
||||
return ((Number)o).longValue();
|
||||
} else {
|
||||
//Attempt to parse the long
|
||||
return Long.parseLong(o.toString());
|
||||
|
|
|
@ -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.List;
|
||||
|
||||
import org.apache.commons.math3.util.MathArrays;
|
||||
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 FindDelayEvaluator extends ComplexEvaluator implements Expressible {
|
||||
|
||||
private static final long serialVersionUID = 1;
|
||||
|
||||
public FindDelayEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
|
||||
super(expression, factory);
|
||||
}
|
||||
|
||||
public Number evaluate(Tuple tuple) throws IOException {
|
||||
StreamEvaluator colEval1 = subEvaluators.get(0);
|
||||
StreamEvaluator colEval2 = subEvaluators.get(1);
|
||||
|
||||
List<Number> numbers1 = (List<Number>)colEval1.evaluate(tuple);
|
||||
List<Number> numbers2 = (List<Number>)colEval2.evaluate(tuple);
|
||||
double[] column1 = new double[numbers1.size()];
|
||||
double[] column2 = new double[numbers2.size()];
|
||||
|
||||
for(int i=0; i<numbers1.size(); i++) {
|
||||
column1[i] = numbers1.get(i).doubleValue();
|
||||
}
|
||||
|
||||
//Reverse the second column.
|
||||
//The convolve function will reverse it back.
|
||||
//This allows correlation to be represented using the convolution math.
|
||||
int rIndex=0;
|
||||
for(int i=numbers2.size()-1; i>=0; i--) {
|
||||
column2[rIndex++] = numbers2.get(i).doubleValue();
|
||||
}
|
||||
|
||||
double[] convolution = MathArrays.convolve(column1, column2);
|
||||
double max = -Double.MAX_VALUE;
|
||||
double maxIndex = -1;
|
||||
|
||||
for(int i=0; i< convolution.length; i++) {
|
||||
double abs = Math.abs(convolution[i]);
|
||||
if(abs > max) {
|
||||
max = abs;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return (maxIndex+1)-column2.length;
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
|
@ -5864,6 +5864,100 @@ public class StreamExpressionTest extends SolrCloudTestCase {
|
|||
assertTrue(prediction == 600.0D);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFinddelay() throws Exception {
|
||||
UpdateRequest updateRequest = new UpdateRequest();
|
||||
|
||||
//Pad column 1 with three zeros.
|
||||
updateRequest.add(id, "10", "price_f", "0.0", "col_s", "a", "order_i", "0");
|
||||
updateRequest.add(id, "11", "price_f", "0.0", "col_s", "a", "order_i", "0");
|
||||
updateRequest.add(id, "12", "price_f", "0.0", "col_s", "a", "order_i", "0");
|
||||
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.add(id, "100", "price_f", "200.0", "col_s", "b", "order_i", "1");
|
||||
updateRequest.add(id, "101", "price_f", "400.0", "col_s", "b", "order_i", "2");
|
||||
updateRequest.add(id, "102", "price_f", "600.0", "col_s", "b", "order_i", "3");
|
||||
updateRequest.add(id, "103", "price_f", "200.0", "col_s", "b", "order_i", "4");
|
||||
updateRequest.add(id, "104", "price_f", "400.0", "col_s", "b", "order_i", "5");
|
||||
updateRequest.add(id, "105", "price_f", "800.0", "col_s", "b", "order_i", "6");
|
||||
updateRequest.add(id, "106", "price_f", "1200.0", "col_s", "b", "order_i", "7");
|
||||
|
||||
|
||||
updateRequest.add(id, "200", "price_f", "-200.0", "col_s", "c", "order_i", "1");
|
||||
updateRequest.add(id, "301", "price_f", "-400.0", "col_s", "c", "order_i", "2");
|
||||
updateRequest.add(id, "402", "price_f", "-600.0", "col_s", "c", "order_i", "3");
|
||||
updateRequest.add(id, "503", "price_f", "-200.0", "col_s", "c", "order_i", "4");
|
||||
updateRequest.add(id, "604", "price_f", "-400.0", "col_s", "c", "order_i", "5");
|
||||
updateRequest.add(id, "705", "price_f", "-800.0", "col_s", "c", "order_i", "6");
|
||||
updateRequest.add(id, "806", "price_f", "-1200.0", "col_s", "c", "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 expr2 = "search("+COLLECTIONORALIAS+", q=\"col_s:b\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
|
||||
|
||||
String cexpr = "let(a="+expr1+", b="+expr2+", c=col(a, price_f), d=col(b, price_f), tuple(delay=finddelay(c, d)))";
|
||||
|
||||
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);
|
||||
long delay = tuple.getLong("delay");
|
||||
assert(delay == 3);
|
||||
|
||||
expr1 = "search("+COLLECTIONORALIAS+", q=\"col_s:a\", fq=\"id:(1 2 3 4 5 6 7)\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
|
||||
expr2 = "search("+COLLECTIONORALIAS+", q=\"col_s:b\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
|
||||
|
||||
cexpr = "let(a="+expr1+", b="+expr2+", c=col(a, price_f), d=col(b, price_f), tuple(delay=finddelay(c, d)))";
|
||||
|
||||
paramsLoc = new ModifiableSolrParams();
|
||||
paramsLoc.set("expr", cexpr);
|
||||
paramsLoc.set("qt", "/stream");
|
||||
|
||||
solrStream = new SolrStream(url, paramsLoc);
|
||||
|
||||
solrStream.setStreamContext(context);
|
||||
tuples = getTuples(solrStream);
|
||||
assertTrue(tuples.size() == 1);
|
||||
tuple = tuples.get(0);
|
||||
delay = tuple.getLong("delay");
|
||||
assert(delay == 0);
|
||||
|
||||
//Test negative correlation.
|
||||
expr1 = "search("+COLLECTIONORALIAS+", q=\"col_s:a\", fq=\"id:(1 2 3 4 5 6 7 11 12)\",fl=\"price_f, order_i\", sort=\"order_i asc\")";
|
||||
expr2 = "search("+COLLECTIONORALIAS+", q=\"col_s:c\", fl=\"price_f, order_i\", sort=\"order_i asc\")";
|
||||
|
||||
cexpr = "let(a="+expr1+", b="+expr2+", c=col(a, price_f), d=col(b, price_f), tuple(delay=finddelay(c, d)))";
|
||||
|
||||
paramsLoc = new ModifiableSolrParams();
|
||||
paramsLoc.set("expr", cexpr);
|
||||
paramsLoc.set("qt", "/stream");
|
||||
|
||||
solrStream = new SolrStream(url, paramsLoc);
|
||||
|
||||
solrStream.setStreamContext(context);
|
||||
tuples = getTuples(solrStream);
|
||||
assertTrue(tuples.size() == 1);
|
||||
tuple = tuples.get(0);
|
||||
delay = tuple.getLong("delay");
|
||||
assert(delay == 2);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDescribe() throws Exception {
|
||||
UpdateRequest updateRequest = new UpdateRequest();
|
||||
|
|
Loading…
Reference in New Issue