SOLR-12983: Create DocValues fields directly from byte[]
authorNoble Paul <noble@apache.org>
Wed, 9 Jan 2019 02:44:41 +0000 (13:44 +1100)
committerNoble Paul <noble@apache.org>
Wed, 9 Jan 2019 02:44:41 +0000 (13:44 +1100)
solr/core/src/java/org/apache/solr/schema/SortableTextField.java
solr/core/src/java/org/apache/solr/schema/StrField.java
solr/core/src/java/org/apache/solr/update/DocumentBuilder.java

index 1d2c21d..1d48c84 100644 (file)
@@ -29,6 +29,7 @@ import org.apache.lucene.queries.function.valuesource.SortedSetFieldSource;
 import org.apache.lucene.search.*;
 import org.apache.lucene.util.BytesRef;
 import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.ByteArrayUtf8CharSequence;
 import org.apache.solr.search.QParser;
 import org.apache.solr.uninverting.UninvertingReader.Type;
 
@@ -103,6 +104,13 @@ public class SortableTextField extends TextField {
     if (! field.hasDocValues()) {
       return Collections.singletonList(f);
     }
+    if (value instanceof ByteArrayUtf8CharSequence) {
+      ByteArrayUtf8CharSequence utf8 = (ByteArrayUtf8CharSequence) value;
+      if (utf8.size() < maxCharsForDocValues) {
+        BytesRef bytes = new BytesRef(utf8.getBuf(), utf8.offset(), utf8.size());
+        return getIndexableFields(field, f, bytes);
+      }
+    }
     final String origString = value.toString();
     final int origLegth = origString.length();
     final boolean truncate = maxCharsForDocValues < origLegth;
@@ -116,7 +124,11 @@ public class SortableTextField extends TextField {
          maxCharsForDocValues + " when useDocValuesAsStored=true (length=" + origLegth + ")");
     }
     final BytesRef bytes = new BytesRef(truncate ? origString.subSequence(0, maxCharsForDocValues) : origString);
-                                        
+
+    return getIndexableFields(field, f, bytes);
+  }
+
+  private static List<IndexableField> getIndexableFields(SchemaField field, IndexableField f, BytesRef bytes) {
     final IndexableField docval = field.multiValued()
       ? new SortedSetDocValuesField(field.getName(), bytes)
       : new SortedDocValuesField(field.getName(), bytes);
index a8ec62c..3413ce1 100644 (file)
@@ -31,6 +31,7 @@ import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.SortedSetSelector;
 import org.apache.lucene.util.BytesRef;
 import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.ByteArrayUtf8CharSequence;
 import org.apache.solr.response.TextResponseWriter;
 import org.apache.solr.search.QParser;
 import org.apache.solr.uninverting.UninvertingReader.Type;
@@ -48,7 +49,7 @@ public class StrField extends PrimitiveFieldType {
 
     if (field.hasDocValues()) {
       IndexableField docval;
-      final BytesRef bytes = new BytesRef(value.toString());
+      final BytesRef bytes = getBytesRef(value);
       if (field.multiValued()) {
         docval = new SortedSetDocValuesField(field.getName(), bytes);
       } else {
@@ -68,6 +69,13 @@ public class StrField extends PrimitiveFieldType {
     return Collections.singletonList(fval);
   }
 
+  public static BytesRef getBytesRef(Object value) {
+    if (value instanceof ByteArrayUtf8CharSequence) {
+      ByteArrayUtf8CharSequence utf8 = (ByteArrayUtf8CharSequence) value;
+      return new BytesRef(utf8.getBuf(), utf8.offset(), utf8.size());
+    } else return new BytesRef(value.toString());
+  }
+
 
   @Override
   public boolean isUtf8Field() {
index 6aef1b4..38b10b9 100644 (file)
@@ -166,7 +166,6 @@ public class DocumentBuilder {
           if( v == null ) {
             continue;
           }
-          v = ByteArrayUtf8CharSequence.convertCharSeq(v);
           hasField = true;
           if (sfield != null) {
             used = true;