Apache Solr open-source search software
SOLR-11339: Add Canberra, Chebyshev, Earth Movers and Manhattan Distance Stream Evaluators
Joel Bernsteine8dee89c878f
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex bfbf4fbf700..252def224fb 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -254,6 +254,10 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("spearmansCorr", SpearmansCorrelationEvaluator.class).withFunctionName("describe", DescribeEvaluator.class).withFunctionName("distance", EuclideanDistanceEvaluator.class)+ .withFunctionName("manhattanDistance", ManhattanDistanceEvaluator.class)+ .withFunctionName("earthMoversDistance", EarthMoversDistanceEvaluator.class)+ .withFunctionName("canberraDistance", CanberraDistanceEvaluator.class)+ .withFunctionName("chebyshevDistance", ChebyshevDistanceEvaluator.class).withFunctionName("empiricalDistribution", EmpiricalDistributionEvaluator.class).withFunctionName("finddelay", FindDelayEvaluator.class).withFunctionName("hist", HistogramEvaluator.class)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CanberraDistanceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CanberraDistanceEvaluator.javanew file mode 100644index 00000000000..2271a3463a8--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CanberraDistanceEvaluator.java@@ -0,0 +1,56 @@+/*+ * 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.math.BigDecimal;+import java.util.List;+import java.util.Locale;++import org.apache.commons.math3.ml.distance.CanberraDistance;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class CanberraDistanceEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public CanberraDistanceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first, Object second) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }+ if(null == second){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ }+ if(!(first instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if(!(second instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ CanberraDistance distance = new CanberraDistance();+ return distance.compute(+ ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),+ ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()+ );+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ChebyshevDistanceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ChebyshevDistanceEvaluator.javanew file mode 100644index 00000000000..3a9294e8b5b--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ChebyshevDistanceEvaluator.java@@ -0,0 +1,56 @@+/*+ * 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.math.BigDecimal;+import java.util.List;+import java.util.Locale;++import org.apache.commons.math3.ml.distance.ChebyshevDistance;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class ChebyshevDistanceEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public ChebyshevDistanceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first, Object second) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }+ if(null == second){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ }+ if(!(first instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if(!(second instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ ChebyshevDistance distance = new ChebyshevDistance();+ return distance.compute(+ ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),+ ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()+ );+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EarthMoversDistanceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EarthMoversDistanceEvaluator.javanew file mode 100644index 00000000000..0769d205666--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EarthMoversDistanceEvaluator.java@@ -0,0 +1,56 @@+/*+ * 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.math.BigDecimal;+import java.util.List;+import java.util.Locale;++import org.apache.commons.math3.ml.distance.EarthMoversDistance;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class EarthMoversDistanceEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public EarthMoversDistanceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first, Object second) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }+ if(null == second){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ }+ if(!(first instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if(!(second instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ EarthMoversDistance distance = new EarthMoversDistance();+ return distance.compute(+ ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),+ ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()+ );+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ManhattanDistanceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ManhattanDistanceEvaluator.javanew file mode 100644index 00000000000..c0a8ed163d8--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ManhattanDistanceEvaluator.java@@ -0,0 +1,56 @@+/*+ * 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.math.BigDecimal;+import java.util.List;+import java.util.Locale;++import org.apache.commons.math3.ml.distance.ManhattanDistance;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class ManhattanDistanceEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {+ protected static final long serialVersionUID = 1L;++ public ManhattanDistanceEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object first, Object second) throws IOException{+ if(null == first){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));+ }+ if(null == second){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ }+ if(!(first instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }+ if(!(second instanceof List<?>)){+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list of numbers",toExpression(constructingFactory), first.getClass().getSimpleName()));+ }++ ManhattanDistance distance = new ManhattanDistance();+ return distance.compute(+ ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),+ ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()+ );+ }+}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.javaindex 47ccd8696b7..01d657736fc 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@@ -5608,7 +5608,10 @@ public class StreamExpressionTest extends SolrCloudTestCase {"field=\"test_dt\", " +"count(*), sum(price_f), max(price_f), min(price_f))";- String cexpr = "let(a="+expr+", b=select("+expr+",mult(-1, count(*)) as nvalue), c=col(a, count(*)), d=col(b, nvalue), tuple(colc=c, cold=d, cov=cov(c,d), dist=distance(c,d)))";+ String cexpr = "let(a="+expr+", b=select("+expr+",mult(-1, count(*)) as nvalue), c=col(a, count(*)), d=col(b, nvalue), " ++ "tuple(colc=c, cold=d, cov=cov(c,d), dist=distance(c,d), " ++ "mdist=manhattanDistance(c,d), edist=earthMoversDistance(c, d), cdist=canberraDistance(c,d)," ++ "chdist=chebyshevDistance(c,d)))";ModifiableSolrParams paramsLoc = new ModifiableSolrParams();paramsLoc.set("expr", cexpr);@@ -5623,7 +5626,10 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertTrue(tuples.size() == 1);assertTrue(tuples.get(0).getDouble("cov").equals(-625.0D));assertTrue(tuples.get(0).getDouble("dist").equals(264.5751311064591D));-+ assertTrue(tuples.get(0).getDouble("mdist").equals(500.0D));+ assertTrue(tuples.get(0).getDouble("cdist").equals(4.0D));+ assertTrue(tuples.get(0).getDouble("chdist").equals(200.0D));+ assertTrue(tuples.get(0).getDouble("edist").equals(1400.0D));}