From aade0000a6c4dee480222d42b77457ce9d74d3dd Mon Sep 17 00:00:00 2001 From: Grant Ingersoll Date: Thu, 11 Jul 2013 15:31:58 +0000 Subject: [PATCH] SOLR-5010: single field handling git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1502247 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/schema/CopyFieldCollectionResource.java | 15 ++++++++++++++- .../solr/rest/schema/FieldCollectionResource.java | 15 ++++++++++++++- .../apache/solr/rest/schema/FieldResource.java | 14 +++++++++++++- .../schema/TestManagedSchemaFieldResource.java | 14 +++++++++++--- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/rest/schema/CopyFieldCollectionResource.java b/solr/core/src/java/org/apache/solr/rest/schema/CopyFieldCollectionResource.java index c12fb601631..8c8f8e96fd9 100644 --- a/solr/core/src/java/org/apache/solr/rest/schema/CopyFieldCollectionResource.java +++ b/solr/core/src/java/org/apache/solr/rest/schema/CopyFieldCollectionResource.java @@ -33,6 +33,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -136,7 +137,19 @@ public class CopyFieldCollectionResource extends BaseFieldResource implements GE log.error(message); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message); } - List destinations = (List)map.get(IndexSchema.DESTINATION); + Object dest = map.get(IndexSchema.DESTINATION); + List destinations = null; + if (dest != null) { + if (dest instanceof List){ + destinations = (List)dest; + } else if (dest instanceof String){ + destinations = Collections.singletonList(dest.toString()); + } else { + String message = "Invalid '" + IndexSchema.DESTINATION + "' type."; + log.error(message); + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message); + } + } if (destinations == null) { malformed.add(fieldName); } else { diff --git a/solr/core/src/java/org/apache/solr/rest/schema/FieldCollectionResource.java b/solr/core/src/java/org/apache/solr/rest/schema/FieldCollectionResource.java index 110ed277062..e9a4254de76 100644 --- a/solr/core/src/java/org/apache/solr/rest/schema/FieldCollectionResource.java +++ b/solr/core/src/java/org/apache/solr/rest/schema/FieldCollectionResource.java @@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -155,7 +156,19 @@ public class FieldCollectionResource extends BaseFieldResource implements GETabl throw new SolrException(ErrorCode.BAD_REQUEST, message); } // copyFields:"comma separated list of destination fields" - List copyTo = (List) map.get(IndexSchema.COPY_FIELDS); + Object copies = map.get(IndexSchema.COPY_FIELDS); + List copyTo = null; + if (copies != null) { + if (copies instanceof List){ + copyTo = (List) copies; + } else if (copies instanceof String){ + copyTo = Collections.singletonList(copies.toString()); + } else { + String message = "Invalid '" + IndexSchema.COPY_FIELDS + "' type."; + log.error(message); + throw new SolrException(ErrorCode.BAD_REQUEST, message); + } + } if (copyTo != null) { map.remove(IndexSchema.COPY_FIELDS); copyFields.put(fieldName, copyTo); diff --git a/solr/core/src/java/org/apache/solr/rest/schema/FieldResource.java b/solr/core/src/java/org/apache/solr/rest/schema/FieldResource.java index 0a3a32e25c6..3e8564aa975 100644 --- a/solr/core/src/java/org/apache/solr/rest/schema/FieldResource.java +++ b/solr/core/src/java/org/apache/solr/rest/schema/FieldResource.java @@ -146,7 +146,19 @@ public class FieldResource extends BaseFieldResource implements GETable, PUTable throw new SolrException(ErrorCode.BAD_REQUEST, message); } else { ManagedIndexSchema oldSchema = (ManagedIndexSchema) getSchema(); - List copyFieldNames = (List) map.get(IndexSchema.COPY_FIELDS); + Object copies = map.get(IndexSchema.COPY_FIELDS); + List copyFieldNames = null; + if (copies != null) { + if (copies instanceof List) { + copyFieldNames = (List) copies; + } else if (copies instanceof String) { + copyFieldNames = Collections.singletonList(copies.toString()); + } else { + String message = "Invalid '" + IndexSchema.COPY_FIELDS + "' type."; + log.error(message); + throw new SolrException(ErrorCode.BAD_REQUEST, message); + } + } if (copyFieldNames != null) { map.remove(IndexSchema.COPY_FIELDS); } diff --git a/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java b/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java index 4dd0b1a4ec2..24a4d5f705e 100644 --- a/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java +++ b/solr/core/src/test/org/apache/solr/rest/schema/TestManagedSchemaFieldResource.java @@ -114,6 +114,9 @@ public class TestManagedSchemaFieldResource extends RestTestBase { assertJPut("/schema/fields/fieldB", "{\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[\"fieldA\"]}", "/responseHeader/status==0"); + assertJPut("/schema/fields/fieldC", + "{\"type\":\"text\",\"stored\":\"false\", \"copyFields\":\"fieldA\"}", + "/responseHeader/status==0"); assertQ("/schema/fields/fieldB?indent=on&wt=xml", "count(/response/lst[@name='field']) = 1", @@ -121,12 +124,15 @@ public class TestManagedSchemaFieldResource extends RestTestBase { assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldB", "count(/response/arr[@name='copyFields']/lst) = 1" ); + assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldC", + "count(/response/arr[@name='copyFields']/lst) = 1" + ); //fine to pass in empty list, just won't do anything assertJPut("/schema/fields/fieldD", "{\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[]}", "/responseHeader/status==0"); //some bad usages - assertJPut("/schema/fields/fieldC", + assertJPut("/schema/fields/fieldF", "{\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[\"some_nonexistent_field_ignore_exception\"]}", "/error/msg==\"copyField dest :\\'some_nonexistent_field_ignore_exception\\' is not an explicit field and doesn\\'t match a dynamicField.\""); } @@ -185,7 +191,9 @@ public class TestManagedSchemaFieldResource extends RestTestBase { assertJPost("/schema/fields", "[{\"name\":\"fieldD\",\"type\":\"text\",\"stored\":\"false\"}," + "{\"name\":\"fieldE\",\"type\":\"text\",\"stored\":\"false\"}," - + " {\"name\":\"fieldF\",\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[\"fieldD\",\"fieldE\"]}]", + + " {\"name\":\"fieldF\",\"type\":\"text\",\"stored\":\"false\", \"copyFields\":[\"fieldD\",\"fieldE\"]}," + + " {\"name\":\"fieldG\",\"type\":\"text\",\"stored\":\"false\", \"copyFields\":\"fieldD\"}"//single + + "]", "/responseHeader/status==0"); assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldF", "count(/response/arr[@name='copyFields']/lst) = 2" @@ -214,7 +222,7 @@ public class TestManagedSchemaFieldResource extends RestTestBase { + "{\"name\":\"fieldD\",\"type\":\"text\",\"stored\":\"false\"}," + " {\"name\":\"fieldE\",\"type\":\"text\",\"stored\":\"false\"}]", "/responseHeader/status==0"); - assertJPost("/schema/copyfields", "[{\"source\":\"fieldA\", \"dest\":[\"fieldB\"]},{\"source\":\"fieldD\", \"dest\":[\"fieldC\", \"fieldE\"]}]", "/responseHeader/status==0"); + assertJPost("/schema/copyfields", "[{\"source\":\"fieldA\", \"dest\":\"fieldB\"},{\"source\":\"fieldD\", \"dest\":[\"fieldC\", \"fieldE\"]}]", "/responseHeader/status==0"); assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldA", "count(/response/arr[@name='copyFields']/lst) = 1"); assertQ("/schema/copyfields/?indent=on&wt=xml&source.fl=fieldD",