You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by GitBox <gi...@apache.org> on 2022/08/08 16:10:08 UTC

[GitHub] [solr] madrob commented on a diff in pull request #940: SOLR-16292 : NVector for alternative great-circle distance calculations

madrob commented on code in PR #940:
URL: https://github.com/apache/solr/pull/940#discussion_r940411276


##########
solr/core/src/java/org/apache/solr/util/NVectorUtil.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.util;
+
+import org.apache.lucene.util.SloppyMath;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+import static org.locationtech.spatial4j.distance.DistanceUtils.EARTH_MEAN_RADIUS_KM;
+
+public class NVectorUtil {
+
+  private static final double pip2 = Math.PI/2;
+
+  public static double[] latLongToNVector(double lat, double lon) {
+    double latRad = Math.toRadians(lat);
+    double lonRad = Math.toRadians(lon);
+    double x = Math.cos(latRad) * Math.cos(lonRad);
+    double y = Math.cos(latRad) * Math.sin(lonRad);
+    double z = Math.sin(latRad);
+    return new double[] {x, y, z};
+  }
+
+  public static String[] latLongToNVector(String lat, String lon) {
+    double[] nvec = latLongToNVector(Double.parseDouble(lat), Double.parseDouble(lon));
+    return new String[] {
+      Double.toString(nvec[0]), Double.toString(nvec[1]), Double.toString(nvec[2])
+    };
+  }
+
+  public static String[] latLongToNVector(String[] latlon) {
+    return latLongToNVector(latlon[0], latlon[1]);
+  }
+
+  public static String[] latLongToNVector(String[] point, NumberFormat formatter) throws ParseException {
+    double[] nvec = latLongToNVector(formatter.parse(point[0]).doubleValue(),formatter.parse(point[1]).doubleValue());
+    return new String[] {
+            Double.toString(nvec[0]), Double.toString(nvec[1]), Double.toString(nvec[2])
+    };
+  }
+
+  public static double[] NVectorToLatLong(double[] n) {

Review Comment:
   nit: method names should start with lowercase (here and others)



##########
solr/core/src/java/org/apache/solr/util/NVectorUtil.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.util;
+
+import org.apache.lucene.util.SloppyMath;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+import static org.locationtech.spatial4j.distance.DistanceUtils.EARTH_MEAN_RADIUS_KM;
+
+public class NVectorUtil {

Review Comment:
   can we add javadoc for the class and methods?



##########
solr/core/src/java/org/apache/solr/util/NVectorUtil.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.util;
+
+import org.apache.lucene.util.SloppyMath;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+import static org.locationtech.spatial4j.distance.DistanceUtils.EARTH_MEAN_RADIUS_KM;
+
+public class NVectorUtil {
+
+  private static final double pip2 = Math.PI/2;
+
+  public static double[] latLongToNVector(double lat, double lon) {
+    double latRad = Math.toRadians(lat);
+    double lonRad = Math.toRadians(lon);
+    double x = Math.cos(latRad) * Math.cos(lonRad);
+    double y = Math.cos(latRad) * Math.sin(lonRad);
+    double z = Math.sin(latRad);
+    return new double[] {x, y, z};
+  }
+
+  public static String[] latLongToNVector(String lat, String lon) {
+    double[] nvec = latLongToNVector(Double.parseDouble(lat), Double.parseDouble(lon));
+    return new String[] {
+      Double.toString(nvec[0]), Double.toString(nvec[1]), Double.toString(nvec[2])
+    };
+  }
+
+  public static String[] latLongToNVector(String[] latlon) {
+    return latLongToNVector(latlon[0], latlon[1]);
+  }
+
+  public static String[] latLongToNVector(String[] point, NumberFormat formatter) throws ParseException {
+    double[] nvec = latLongToNVector(formatter.parse(point[0]).doubleValue(),formatter.parse(point[1]).doubleValue());
+    return new String[] {
+            Double.toString(nvec[0]), Double.toString(nvec[1]), Double.toString(nvec[2])
+    };
+  }
+
+  public static double[] NVectorToLatLong(double[] n) {
+    return new double[] {
+      Math.asin(n[2]) * (180 / Math.PI), Math.atan(n[1] / n[0]) * (180 / Math.PI)

Review Comment:
   Math.toDegrees



##########
solr/core/src/test/org/apache/solr/search/function/distance/NVectorDistTest.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.search.function.distance;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Locale;
+
+public class NVectorDistTest extends SolrTestCaseJ4 {
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    System.setProperty("enable.update.log", "false"); // schema12 doesn't support _version_
+    initCore("solrconfig-nvector.xml", "schema-nvector.xml");
+  }
+
+  @Override
+  public void setUp() throws Exception {
+    Locale.setDefault(Locale.ENGLISH);//for parsing lat/log correctly in this test
+    super.setUp();
+    assertU(delQ("*:*"));
+    assertU(commit());
+  }
+
+  @Test
+  public void testNVector() throws Exception {
+    assertU(adoc("id", "0", "nvector", "52.02471051274793, -0.49007556238612354"));
+    assertU(commit());
+    assertJQ(
+        req("defType", "lucene", "q", "*:*", "fl", "id,nvector*", "sort", "id asc"),
+        "/response/docs/[0]== {"
+            + "'id':'0',"
+            + "'nvector_0_d1':0.6152990562577377,"
+            + "'nvector_1_d1':-0.005263047078845837,"
+            + "'nvector_2_d1':0.7882762026750415,"
+            + "'nvector':'52.02471051274793, -0.49007556238612354'}");
+
+    assertJQ(
+        req(
+            "defType", "lucene",
+            "q", "*:*",
+            "nvd", "nvdist(52.01966071979866, -0.4983083573742952,nvector)",

Review Comment:
   I missed where 'nvdist' is defined as a function. Do we need to add to `ValueSourceParser` static init block?



##########
solr/core/src/java/org/apache/solr/schema/NVectorField.java:
##########
@@ -0,0 +1,285 @@
+/*
+ * 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.schema;
+
+import org.apache.lucene.document.StoredField;
+import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.queries.function.FunctionValues;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.queries.function.valuesource.MultiValueSource;
+import org.apache.lucene.search.SortField;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.response.TextResponseWriter;
+import org.apache.solr.search.QParser;
+import org.apache.solr.uninverting.UninvertingReader;
+import org.apache.solr.util.NVectorUtil;
+
+import java.io.IOException;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+public class NVectorField extends CoordinateFieldType {

Review Comment:
   Ok, fair enough about not extending PointType. As for range queries... does NVector efficiently support shape intersections? I'm under the impression that it doesn't - that's part of the way that we get such a speed up on distance...



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org