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


##########
solr/core/src/java/org/apache/solr/schema/NVector.java:
##########
@@ -0,0 +1,263 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class NVector extends CoordinateFieldType {
+
+    @Override
+    protected void init(IndexSchema schema, Map<String, String> args) {
+        super.init(schema, args);
+        dimension = 3;
+        createSuffixCache(3);
+    }
+
+    @Override
+    public List<IndexableField> createFields(SchemaField field, Object value) {
+        String externalVal = value.toString();
+        String[] point = parseCommaSeparatedList(externalVal, dimension);
+        String[] nvector = NVectorUtil.latLongToNVector(point);
+
+        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
+     * @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) throws SolrException {
+        //TODO: Should we support sparse vectors?
+        String[] out = new String[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] = externalVal.trim();
+            i = 1;
+        } else if (idx > 0) {//if it is zero, that is an error
+            //Parse out a comma separated list of values, as in: 
73.5,89.2,7773.4
+            for (; i < dimension; i++) {
+                while (start < end && externalVal.charAt(start) == ' ') 
start++;
+                while (end > start && externalVal.charAt(end - 1) == ' ') 
end--;
+                if (start == end) {
+                    break;
+                }
+                out[i] = externalVal.substring(start, end);
+                start = idx + 1;
+                end = externalVal.indexOf(',', start);

Review Comment:
   I've added a separator init param and parse doubles using NumberFormat with 
default locale



-- 
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