Apache Solr open-source search software
SOLR-1302: some slight refactoring for more reusable distance calculations
Grant Ingersoll33df44a6ef62
diff --git a/src/java/org/apache/solr/search/function/distance/DistanceUtils.java b/src/java/org/apache/solr/search/function/distance/DistanceUtils.javaindex 2af30752add..1cc1721d798 100644--- a/src/java/org/apache/solr/search/function/distance/DistanceUtils.java+++ b/src/java/org/apache/solr/search/function/distance/DistanceUtils.java@@ -27,6 +27,66 @@ public class DistanceUtils {public static final double DEGREES_TO_RADIANS = Math.PI / 180.0;public static final double RADIANS_TO_DEGREES = 180.0 / Math.PI;+ /**+ * Calculate the p-norm (i.e. length) between two vectors+ *+ * @param vec1 The first vector+ * @param vec2 The second vector+ * @param power The power (2 for Euclidean distance, 1 for manhattan, etc.)+ * @return The length.+ * <p/>+ * See http://en.wikipedia.org/wiki/Lp_space+ * @see #vectorDistance(double[], double[], double, double)+ */+ public static double vectorDistance(double[] vec1, double[] vec2, double power) {+ return vectorDistance(vec1, vec2, power, 1.0 / power);+ }++ /**+ * Calculate the p-norm (i.e. length) between two vectors+ *+ * @param vec1 The first vector+ * @param vec2 The second vector+ * @param power The power (2 for Euclidean distance, 1 for manhattan, etc.)+ * @param oneOverPower If you've precalculated oneOverPower and cached it, use this method to save one division operation over {@link #vectorDistance(double[], double[], double)}.+ * @return The length.+ */+ public static double vectorDistance(double[] vec1, double[] vec2, double power, double oneOverPower) {+ double result = 0;++ if (power == 0) {+ for (int i = 0; i < vec1.length; i++) {+ result += vec1[i] - vec2[i] == 0 ? 0 : 1;+ }++ } else if (power == 1.0) {+ for (int i = 0; i < vec1.length; i++) {+ result += vec1[i] - vec2[i];+ }+ } else if (power == 2.0) {+ result = Math.sqrt(squaredEuclideanDistance(vec1, vec2));+ } else if (power == Integer.MAX_VALUE || Double.isInfinite(power)) {//infininte norm?+ for (int i = 0; i < vec1.length; i++) {+ result = Math.max(vec1[i], vec2[i]);+ }+ } else {+ for (int i = 0; i < vec1.length; i++) {+ result += Math.pow(vec1[i] - vec2[i], power);+ }+ result = Math.pow(result, oneOverPower);+ }+ return result;+ }++ public static double squaredEuclideanDistance(double[] vec1, double[] vec2) {+ double result = 0;+ for (int i = 0; i < vec1.length; i++) {+ double v = vec1[i] - vec2[i];+ result += v * v;+ }+ return result;+ }+/*** @param x1 The x coordinate of the first point* @param y1 The y coordinate of the first point@@ -91,6 +151,46 @@ public class DistanceUtils {return out;}+ /**+ * Given a string containing <i>dimension</i> values encoded in it, separated by commas, return a double array of length <i>dimension</i>+ * containing the values.+ *+ * @param out A preallocated array. Must be size dimension. If it is not it will be resized.+ * @param externalVal The value to parse+ * @param dimension The expected number of values for the point+ * @return An array of the values that make up the point (aka vector)+ * @throws {@link SolrException} if the dimension specified does not match the number of values in the externalValue.+ */+ public static double[] parsePointDouble(double[] out, String externalVal, int dimension) {+ if (out == null || out.length != dimension) out = new double[dimension];+ int idx = externalVal.indexOf(',');+ int end = idx;+ int start = 0;+ int i = 0;+ if (idx == -1 && dimension == 1 && externalVal.length() > 0) {//we have a single point, dimension better be 1+ out[0] = Double.parseDouble(externalVal.trim());+ i = 1;+ } else if (idx > 0) {//if it is zero, that is an error+ //Parse out a comma separated list of point values, as in: 73.5,89.2,7773.4+ for (; i < dimension; i++) {+ //TODO: abstract common code with other parsePoint+ while (start < end && externalVal.charAt(start) == ' ') start++;+ while (end > start && externalVal.charAt(end - 1) == ' ') end--;+ out[i] = Double.parseDouble(externalVal.substring(start, end));+ start = idx + 1;+ end = externalVal.indexOf(',', start);+ if (end == -1) {+ end = externalVal.length();+ }+ }+ }+ if (i != dimension) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "incompatible dimension (" + dimension ++ ") and values (" + externalVal + "). Only " + i + " values specified");+ }+ return out;+ }+/*** extract (by calling {@link #parsePoint(String[], String, int)} and validate the latitude and longitude contained* in the String by making sure the latitude is between 90 & -90 and longitude is between -180 and 180.@@ -105,7 +205,7 @@ public class DistanceUtils {if (latLon == null) {latLon = new double[2];}- String[] toks = DistanceUtils.parsePoint(null, latLonStr, 2);+ double[] toks = DistanceUtils.parsePointDouble(null, latLonStr, 2);latLon[0] = Double.valueOf(toks[0]);if (latLon[0] < -90.0 || latLon[0] > 90.0) {diff --git a/src/java/org/apache/solr/search/function/distance/SquaredEuclideanFunction.java b/src/java/org/apache/solr/search/function/distance/SquaredEuclideanFunction.javaindex 7a7ce96211a..881cdbd256e 100644--- a/src/java/org/apache/solr/search/function/distance/SquaredEuclideanFunction.java+++ b/src/java/org/apache/solr/search/function/distance/SquaredEuclideanFunction.java@@ -23,8 +23,7 @@ import org.apache.solr.search.function.MultiValueSource;/*** While not strictly a distance, the Sq. Euclidean Distance is often all that is needed in many applications* that require a distance, thus saving a sq. rt. calculation- *- **/+ */public class SquaredEuclideanFunction extends VectorDistanceFunction {protected String name = "sqedist";@@ -42,16 +41,13 @@ public class SquaredEuclideanFunction extends VectorDistanceFunction {* @param doc The doc to score*/protected double distance(int doc, DocValues dv1, DocValues dv2) {- double result = 0;- double [] vals1 = new double[source1.dimension()];- double [] vals2 = new double[source1.dimension()];++ double[] vals1 = new double[source1.dimension()];+ double[] vals2 = new double[source1.dimension()];dv1.doubleVal(doc, vals1);dv2.doubleVal(doc, vals2);- for (int i = 0; i < vals1.length; i++) {- double v = vals1[i] - vals2[i];- result += v * v;- }- return result;++ return DistanceUtils.squaredEuclideanDistance(vals1, vals2);}@Overridediff --git a/src/java/org/apache/solr/search/function/distance/VectorDistanceFunction.java b/src/java/org/apache/solr/search/function/distance/VectorDistanceFunction.javaindex e1737555c4c..860528a6201 100644--- a/src/java/org/apache/solr/search/function/distance/VectorDistanceFunction.java+++ b/src/java/org/apache/solr/search/function/distance/VectorDistanceFunction.java@@ -20,8 +20,8 @@ import org.apache.lucene.index.IndexReader;import org.apache.lucene.search.Searcher;import org.apache.solr.common.SolrException;import org.apache.solr.search.function.DocValues;-import org.apache.solr.search.function.ValueSource;import org.apache.solr.search.function.MultiValueSource;+import org.apache.solr.search.function.ValueSource;import java.io.IOException;import java.util.Map;@@ -62,45 +62,18 @@ public class VectorDistanceFunction extends ValueSource {/*** Calculate the distance*- * @param doc The current doc+ * @param doc The current doc* @param dv1 The values from the first MultiValueSource* @param dv2 The values from the second MultiValueSource* @return The distance*/protected double distance(int doc, DocValues dv1, DocValues dv2) {- double result = 0;//Handle some special cases:- double [] vals1 = new double[source1.dimension()];- double [] vals2 = new double[source1.dimension()];+ double[] vals1 = new double[source1.dimension()];+ double[] vals2 = new double[source1.dimension()];dv1.doubleVal(doc, vals1);dv2.doubleVal(doc, vals2);- if (power == 0) {- for (int i = 0; i < vals1.length; i++) {- result += vals1[i] - vals2[i] == 0 ? 0 :1;- }-- } else if (power == 1.0) {- for (int i = 0; i < vals1.length; i++) {- result += vals1[i] - vals2[i];- }- } else if (power == 2.0) {- for (int i = 0; i < vals1.length; i++) {- double v = vals1[i] - vals2[i];- result += v * v;- }- result = Math.sqrt(result);- } else if (power == Integer.MAX_VALUE || Double.isInfinite(power)) {//infininte norm?- for (int i = 0; i < vals1.length; i++) {- result = Math.max(vals1[i], vals2[i]);- }- } else {- for (int i = 0; i < vals1.length; i++) {- result += Math.pow(vals1[i] - vals2[i], power);- }- result = Math.pow(result, oneOverPower);- }-- return result;+ return DistanceUtils.vectorDistance(vals1, vals2, power, oneOverPower);}@Override@@ -111,7 +84,6 @@ public class VectorDistanceFunction extends ValueSource {final DocValues vals2 = source2.getValues(context, reader);-return new DocValues() {@Overridepublic byte byteVal(int doc) {@@ -120,7 +92,7 @@ public class VectorDistanceFunction extends ValueSource {@Overridepublic short shortVal(int doc) {- return (short)doubleVal(doc);+ return (short) doubleVal(doc);}public float floatVal(int doc) {