From 64a4ca57a89ad05085e60571fcea3b532de28e8e Mon Sep 17 00:00:00 2001 From: Chris Troullis Date: Sun, 25 Aug 2019 18:57:17 -0400 Subject: [PATCH] SOLR-13699 - maxChars no longer working on CopyField with Javabin --- .../apache/solr/update/DocumentBuilder.java | 4 +-- .../solr/collection1/conf/schema.xml | 4 +++ .../solr/update/DocumentBuilderTest.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java b/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java index 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 field Object 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.xml index 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 @@ + + I am your default sim diff --git a/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java b/solr/core/src/test/org/apache/solr/update/DocumentBuilderTest.java index 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")); + } + }