CopyField
contains all the information of a valid copy fields in an index.
+ *
+ * @since solr 1.4
+ */
+public class CopyField {
+
+ private final SchemaField source;
+ private final SchemaField destination;
+ private final int maxChars;
+ public static final int UNLIMITED = 0;
+
+ public CopyField(final SchemaField source, final SchemaField destination) {
+ this(source, destination, UNLIMITED);
+ }
+
+ /**
+ * @param source The SchemaField of the source field.
+ * @param destination The SchemaField of the destination field.
+ * @param maxChars Maximum number of chars in source field to copy to destination field.
+ * If equal to 0, there is no limit.
+ */
+ public CopyField(final SchemaField source, final SchemaField destination,
+ final int maxChars) {
+ if (source == null || destination == null) {
+ throw new IllegalArgumentException(
+ "Source or Destination SchemaField can't be NULL.");
+ }
+ if (maxChars < 0) {
+ throw new IllegalArgumentException(
+ "Attribute maxChars can't have a negative value.");
+ }
+ this.source = source;
+ this.destination = destination;
+ this.maxChars = maxChars;
+ }
+
+ public String getLimitedValue( final String val ){
+ return maxChars == UNLIMITED || val.length() < maxChars ?
+ val : val.substring( 0, maxChars );
+ }
+
+ /**
+ * @return source SchemaField
+ */
+ public SchemaField getSource() {
+ return source;
+ }
+
+ /**
+ * @return destination SchemaField
+ */
+ public SchemaField getDestination() {
+ return destination;
+ }
+
+ /**
+ * @return tha maximum number of chars in source field to copy to destination field.
+ */
+ public int getMaxChars() {
+ return maxChars;
+ }
+}
diff --git a/src/java/org/apache/solr/schema/IndexSchema.java b/src/java/org/apache/solr/schema/IndexSchema.java
index c12ec766d2d..5b53ba015bf 100644
--- a/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/src/java/org/apache/solr/schema/IndexSchema.java
@@ -621,8 +621,18 @@ public final class IndexSchema {
String source = DOMUtil.getAttr(attrs,"source","copyField definition");
String dest = DOMUtil.getAttr(attrs,"dest", "copyField definition");
+ String maxChars = DOMUtil.getAttr(attrs, "maxChars");
+ int maxCharsInt = CopyField.UNLIMITED;
+ if (maxChars != null) {
+ try {
+ maxCharsInt = Integer.parseInt(maxChars);
+ } catch (NumberFormatException e) {
+ log.warn("Couldn't parse maxChars attribute for copyField from "
+ + source + " to " + dest + " as integer. The whole field will be copied.");
+ }
+ }
- registerCopyField(source, dest);
+ registerCopyField(source, dest, maxCharsInt);
}
for (Map.Entry
* NOTE: this function is not thread safe. However, it is safe to use within the standard
@@ -655,12 +670,12 @@ public final class IndexSchema {
*
* @see SolrCoreAware
*/
- public void registerCopyField( String source, String dest )
+ public void registerCopyField( String source, String dest, int maxChars )
{
boolean sourceIsPattern = isWildCard(source);
boolean destIsPattern = isWildCard(dest);
- log.debug("copyField source='"+source+"' dest='"+dest+"'");
+ log.debug("copyField source='"+source+"' dest='"+dest+"' maxChars='"+maxChars);
SchemaField d = getFieldOrNull(dest);
if(d == null){
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "copyField destination :'"+dest+"' does not exist" );
@@ -678,10 +693,10 @@ public final class IndexSchema {
if( df == null ) {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "copyField dynamic destination must match a dynamicField." );
}
- registerDynamicCopyField(new DynamicDestCopy(source, df ));
+ registerDynamicCopyField(new DynamicDestCopy(source, df, maxChars ));
}
else {
- registerDynamicCopyField(new DynamicCopy(source, d));
+ registerDynamicCopyField(new DynamicCopy(source, d, maxChars));
}
}
else if( destIsPattern ) {
@@ -692,13 +707,12 @@ public final class IndexSchema {
// retrieve the field to force an exception if it doesn't exist
SchemaField f = getField(source);
- SchemaField[] destArr = copyFields.get(source);
- if (destArr==null) {
- destArr=new SchemaField[]{d};
- } else {
- destArr = (SchemaField[])append(destArr,d);
+ ListCopyField
works.
+ * It uses its own special schema file.
+ *
+ * @since solr 1.4
+ */
+public class CopyFieldTest extends AbstractSolrTestCase {
+
+ @Override
+ public String getSchemaFile() {
+ return "schema-copyfield-test.xml";
+ }
+
+ @Override
+ public String getSolrConfigFile() {
+ return "solrconfig.xml";
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testCopyFieldSchemaFieldSchemaField() {
+ try {
+ new CopyField(new SchemaField("source", new TextField()), null);
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ try {
+ new CopyField(null, new SchemaField("destination", new TextField()));
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ try {
+ new CopyField(null, null);
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ }
+
+ @Test
+ public void testCopyFieldSchemaFieldSchemaFieldInt() {
+ try {
+ new CopyField(null,
+ new SchemaField("destination", new TextField()), 1000);
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ try {
+ new CopyField(new SchemaField("source", new TextField()), null,
+ 1000);
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ try {
+ new CopyField(null, null, 1000);
+ fail("CopyField failed with null SchemaField argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains("can't be NULL"));
+ }
+ try {
+ new CopyField(new SchemaField("source", new TextField()),
+ new SchemaField("destination", new TextField()), -1000);
+ fail("CopyField failed with negative length argument.");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getLocalizedMessage().contains(
+ "can't have a negative value"));
+ }
+ new CopyField(new SchemaField("source", new TextField()),
+ new SchemaField("destination", new TextField()), CopyField.UNLIMITED);
+ }
+
+ @Test
+ public void testGetSource() {
+ final CopyField copyField = new CopyField(new SchemaField("source",
+ new TextField()), new SchemaField("destination",
+ new TextField()), 1000);
+ assertEquals("source", copyField.getSource().name);
+ }
+
+ @Test
+ public void testGetDestination() {
+ final CopyField copyField = new CopyField(new SchemaField("source",
+ new TextField()), new SchemaField("destination",
+ new TextField()), 1000);
+ assertEquals("destination", copyField.getDestination().name);
+ }
+
+ @Test
+ public void testGetMaxChars() {
+ final CopyField copyField = new CopyField(new SchemaField("source",
+ new TextField()), new SchemaField("destination",
+ new TextField()), 1000);
+ assertEquals(1000, copyField.getMaxChars());
+ }
+
+ @Test
+ public void testCopyFieldFunctionality()
+ {
+ SolrCore core = h.getCore();
+ assertU(adoc("id", "10", "title", "test copy field", "text_en", "this is a simple test of the copy field functionality"));
+ assertU(commit());
+
+ Map