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


##########
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:
   should this extend from `PointType` instead? is there more that we get from 
it?



##########
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 {
+
+    String DEFAULT_SEPARATOR = ",";
+    String separator = DEFAULT_SEPARATOR;
+
+    @Override
+    protected void init(IndexSchema schema, Map<String, String> args) {
+        super.init(schema, args);
+        separator = args.getOrDefault("separator", DEFAULT_SEPARATOR);
+        dimension = 3;
+        createSuffixCache(3);
+    }
+
+    @Override
+    public List<IndexableField> createFields(SchemaField field, Object value) {
+        String externalVal = value.toString();
+        String[] point = parseCommaSeparatedList(externalVal, dimension, 
separator);
+        String[] nvector;
+        try {
+            NumberFormat format = 
NumberFormat.getInstance(Locale.getDefault());
+            format.setParseIntegerOnly(false);
+            nvector = NVectorUtil.latLongToNVector(point, format);
+        } catch (ParseException e) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST,
+                    "format exception parsing: "+externalVal);
+        }
+        List<IndexableField> f = new ArrayList<>((dimension * 2) + 1);
+
+        if (field.indexed()) {
+            for (int i = 0; i < dimension; i++) {
+                SchemaField sf = subField(field, i, schema);
+                f.addAll(sf.createFields(nvector[i]));
+            }
+        }
+
+        if (field.stored()) {
+            f.add(createField(field.getName(), externalVal, StoredField.TYPE));
+        }
+        return f;
+    }
+
+    @Override
+    public ValueSource getValueSource(SchemaField field, QParser parser) {
+        ArrayList<ValueSource> vs = new ArrayList<>(dimension);
+        for (int i = 0; i < dimension; i++) {
+            SchemaField sub = subField(field, i, schema);
+            vs.add(sub.getType().getValueSource(sub, parser));
+        }
+        return new NVectorValueSource(vs);
+    }
+
+    /**
+     * Given a string containing <i>dimension</i> values encoded in it, 
separated by commas, return a
+     * String array of length <i>dimension</i> containing the values.
+     *
+     * @param externalVal The value to parse
+     * @param dimension   The expected number of values for the point
+     * @param separator   The separator between values
+     * @return An array of the values that make up the point (aka vector)
+     * @throws SolrException if the dimension specified does not match the 
number found
+     */
+    public static String[] parseCommaSeparatedList(String externalVal, int 
dimension, String separator)

Review Comment:
   Can we have some unit tests for this? Parsing is one of those thing that 
seems easy but there are always edge cases and it's easy to introduce 
regressions.



##########
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 {
+
+    String DEFAULT_SEPARATOR = ",";

Review Comment:
   private static final



##########
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 = lat * (Math.PI / 180);
+    double lonRad = lon * (Math.PI / 180);

Review Comment:
   `Math.toRadians`



##########
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 = lat * (Math.PI / 180);
+    double lonRad = lon * (Math.PI / 180);
+    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)
+    };
+  }
+
+  public static double[] NVectorToLatLong(String[] n) {
+    return NVectorToLatLong(
+        new double[] {
+          Double.parseDouble(n[0]), Double.parseDouble(n[1]), 
Double.parseDouble(n[2])
+        });
+  }
+
+  public static double NVectorDotProduct(double[] a, double[] b) {
+    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
+  }
+
+  public static double NVectorDist(double[] a, double[] b) {
+    return NVectorDist(a, b, EARTH_MEAN_RADIUS_KM);
+  }
+
+  public static double NVectorDist(double[] a, double[] b, double radius) {
+    return radius * (pip2 - SloppyMath.asin(a[0] * b[0] + a[1] * b[1] + a[2] * 
b[2]));

Review Comment:
   ```suggestion
       return NVectorDist(NVectorDotProduct(a, b), radius);
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to