Apache Solr open-source search software
SOLR-13699 - maxChars no longer working on CopyField with Javabin
Chris Troullis64a4ca57a89a
diff --git a/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java b/solr/core/src/java/org/apache/solr/update/DocumentBuilder.javaindex 90694ff62b2..543a4cbf5e7 100644--- a/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java+++ b/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java@@ -196,8 +196,8 @@ public class DocumentBuilder {// Perhaps trim the length of a copy fieldObject val = v;- if( val instanceof String && cf.getMaxChars() > 0 ) {- val = cf.getLimitedValue((String)val);+ if( val instanceof CharSequence && cf.getMaxChars() > 0 ) {+ val = cf.getLimitedValue(val.toString());}addField(out, destinationField, val,diff --git a/solr/core/src/test-files/solr/collection1/conf/schema.xml b/solr/core/src/test-files/solr/collection1/conf/schema.xmlindex 31fd9e501c9..e0a96cca05c 100644--- a/solr/core/src/test-files/solr/collection1/conf/schema.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema.xml@@ -651,6 +651,8 @@<!-- EnumType --><field name="severity" type="severityType" docValues="true" indexed="true" stored="true" multiValued="false"/>+ <field name="max_chars" type="string" indexed="false" stored="true"/>+<!-- Dynamic field definitions. If a field name is not found, dynamicFieldswill be used if the name matches any of the patterns.@@ -850,6 +852,8 @@<copyField source="*_dynamic" dest="dynamic_*"/><copyField source="*_path" dest="*_ancestor"/>+ <copyField source="title" dest="max_chars" maxChars="10"/>+<!-- example of a custom similarity --><similarity class="solr.CustomSimilarityFactory"><str name="echo">I am your default sim</str>diff --git a/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java b/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.javaindex bf819a002c4..1b0794b652c 100644--- a/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java+++ b/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java@@ -30,6 +30,7 @@ import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.SolrInputField;+import org.apache.solr.common.util.ByteArrayUtf8CharSequence;import org.apache.solr.core.SolrCore;import org.apache.solr.schema.FieldType;import org.junit.After;@@ -274,4 +275,28 @@ public class DocumentBuilderTest extends SolrTestCaseJ4 {assertTrue(field.stringValue().startsWith("STORED V3|"));}+ @Test+ public void testCopyFieldMaxChars() {+ SolrCore core = h.getCore();++ String testValue = "this is more than 10 characters";+ String truncatedValue = "this is mo";++ //maxChars with a string value+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField( "title", testValue);++ Document out = DocumentBuilder.toDocument(doc, core.getLatestSchema());+ assertEquals(testValue, out.get("title"));+ assertEquals(truncatedValue, out.get("max_chars"));++ //maxChars with a ByteArrayUtf8CharSequence+ doc = new SolrInputDocument();+ doc.addField( "title", new ByteArrayUtf8CharSequence(testValue));++ out = DocumentBuilder.toDocument(doc, core.getLatestSchema());+ assertEquals(testValue, out.get("title"));+ assertEquals(truncatedValue, out.get("max_chars"));+ }+}