Apache Solr open-source search software
SOLR-18152: Migrate Schema Designer to JAX-RS Apis. Fix some bugs. (#4203)
Eric Pugh0f787bfe8a09
diff --git a/changelog/unreleased/SOLR-18152-add-configset-download-zip-to-solrj-fix-schema-designer-bug.yml b/changelog/unreleased/SOLR-18152-add-configset-download-zip-to-solrj-fix-schema-designer-bug.ymlnew file mode 100644index 00000000000..018af08a7f7--- /dev/null+++ b/changelog/unreleased/SOLR-18152-add-configset-download-zip-to-solrj-fix-schema-designer-bug.yml@@ -0,0 +1,8 @@+# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc+title: Schema Designer sample-doc analysis now works correctly when analyze sample documents. More robust state management in SolrCloud.+type: fixed # added, changed, fixed, deprecated, removed, dependency_update, security, other+authors:+ - name: Eric Pugh+links:+ - name: SOLR-18152+ url: https://issues.apache.org/jira/browse/SOLR-18152diff --git a/solr/api/src/java/org/apache/solr/client/api/endpoint/SchemaDesignerApi.java b/solr/api/src/java/org/apache/solr/client/api/endpoint/SchemaDesignerApi.javanew file mode 100644index 00000000000..572891d6ec5--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/endpoint/SchemaDesignerApi.java@@ -0,0 +1,216 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.endpoint;++import static org.apache.solr.client.api.util.Constants.GENERIC_ENTITY_PROPERTY;++import io.swagger.v3.oas.annotations.Operation;+import io.swagger.v3.oas.annotations.extensions.Extension;+import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;+import io.swagger.v3.oas.annotations.parameters.RequestBody;+import jakarta.ws.rs.DELETE;+import jakarta.ws.rs.DefaultValue;+import jakarta.ws.rs.GET;+import jakarta.ws.rs.POST;+import jakarta.ws.rs.PUT;+import jakarta.ws.rs.Path;+import jakarta.ws.rs.PathParam;+import jakarta.ws.rs.QueryParam;+import java.io.InputStream;+import java.util.List;+import org.apache.solr.client.api.model.FlexibleSolrJerseyResponse;+import org.apache.solr.client.api.model.ListCollectionsResponse;+import org.apache.solr.client.api.model.SchemaDesignerAddRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerAddResponse;+import org.apache.solr.client.api.model.SchemaDesignerAnalyzeResponse;+import org.apache.solr.client.api.model.SchemaDesignerConfigsResponse;+import org.apache.solr.client.api.model.SchemaDesignerFileContentsResponse;+import org.apache.solr.client.api.model.SchemaDesignerInfoResponse;+import org.apache.solr.client.api.model.SchemaDesignerPublishResponse;+import org.apache.solr.client.api.model.SchemaDesignerResponse;+import org.apache.solr.client.api.model.SchemaDesignerSchemaDiffResponse;+import org.apache.solr.client.api.model.SchemaDesignerUpdateRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerUpdateResponse;+import org.apache.solr.client.api.model.SolrJerseyResponse;++/** V2 API definitions for the Solr Schema Designer. */+@Path("/schema-designer")+public interface SchemaDesignerApi {++ @GET+ @Path("/{configSet}")+ @Operation(+ summary = "Get info about a configSet being designed.",+ tags = {"schema-designer"})+ SchemaDesignerInfoResponse getInfo(@PathParam("configSet") String configSet) throws Exception;++ @POST+ @Path("/{configSet}/prep")+ @Operation(+ summary = "Prepare a mutable configSet copy for schema design.",+ tags = {"schema-designer"})+ SchemaDesignerResponse prepNewSchema(+ @PathParam("configSet") String configSet, @QueryParam("copyFrom") String copyFrom)+ throws Exception;++ @DELETE+ @Path("/{configSet}")+ @Operation(+ summary = "Clean up temporary resources for a schema being designed.",+ tags = {"schema-designer"})+ SolrJerseyResponse cleanupTempSchema(@PathParam("configSet") String configSet) throws Exception;++ @PUT+ @Path("/{configSet}/file")+ @Operation(+ summary = "Update the contents of a file in a configSet being designed.",+ tags = {"schema-designer"})+ SchemaDesignerFileContentsResponse updateFileContents(+ @PathParam("configSet") String configSet,+ @QueryParam("file") String file,+ @RequestBody(+ required = true,+ extensions = {+ @Extension(+ properties = {+ @ExtensionProperty(name = GENERIC_ENTITY_PROPERTY, value = "true")+ })+ })+ InputStream fileContents)+ throws Exception;++ @GET+ @Path("/{configSet}/sample")+ @Operation(+ summary = "Get a sample value and analysis for a field.",+ tags = {"schema-designer"})+ FlexibleSolrJerseyResponse getSampleValue(+ @PathParam("configSet") String configSet,+ @QueryParam("field") String fieldName,+ @QueryParam("uniqueKeyField") String idField,+ @QueryParam("docId") String docId)+ throws Exception;++ // TODO: this sub-resource belongs in ConfigsetsApi as GET+ // /configsets/{configSetName}/collections;+ // move it there in a follow-up so it is reusable outside the schema designer.+ @GET+ @Path("/{configSet}/collections")+ @Operation(+ summary = "List collections that use a given configSet.",+ tags = {"schema-designer"})+ ListCollectionsResponse listCollectionsForConfig(@PathParam("configSet") String configSet)+ throws Exception;++ @GET+ @Path("/configs")+ @Operation(+ summary = "List all configSets available for schema design.",+ description =+ "Returns a filtered, de-duplicated view of configSets enriched with a per-configSet"+ + " status: 0 = draft only (no published version yet), 1 = published but schema"+ + " designer is disabled for it, 2 = published and designer is enabled. Internal"+ + " mutable copies (._designer_ prefix) and the default configSet are excluded.",+ tags = {"schema-designer"})+ SchemaDesignerConfigsResponse listDesignerConfigs() throws Exception;++ @POST+ @Path("/{configSet}")+ @Operation(+ summary = "Add a new field, field type, or dynamic field to the schema being designed.",+ tags = {"schema-designer"})+ SchemaDesignerAddResponse addSchemaObject(+ @PathParam("configSet") String configSet,+ @QueryParam("schemaVersion") Integer schemaVersion,+ SchemaDesignerAddRequestBody requestBody)+ throws Exception;++ @PUT+ @Path("/{configSet}")+ @Operation(+ summary = "Update an existing field or field type in the schema being designed.",+ tags = {"schema-designer"})+ SchemaDesignerUpdateResponse updateSchemaObject(+ @PathParam("configSet") String configSet,+ @QueryParam("schemaVersion") Integer schemaVersion,+ SchemaDesignerUpdateRequestBody requestBody)+ throws Exception;++ @PUT+ @Path("/{configSet}/publish")+ @Operation(+ summary = "Publish the designed schema to a live configSet.",+ tags = {"schema-designer"})+ SchemaDesignerPublishResponse publish(+ @PathParam("configSet") String configSet,+ @QueryParam("schemaVersion") Integer schemaVersion,+ @QueryParam("newCollection") String newCollection,+ @QueryParam("reloadCollections") @DefaultValue("false") Boolean reloadCollections,+ @QueryParam("numShards") @DefaultValue("1") Integer numShards,+ @QueryParam("replicationFactor") @DefaultValue("1") Integer replicationFactor,+ @QueryParam("indexToCollection") @DefaultValue("false") Boolean indexToCollection,+ @QueryParam("cleanupTemp") @DefaultValue("true") Boolean cleanupTempParam,+ @QueryParam("disableDesigner") @DefaultValue("false") Boolean disableDesigner)+ throws Exception;++ /**+ * Analyzes sample documents to suggest a schema.+ *+ * <p>Sample documents are read from the HTTP request body (not declared as a parameter on this+ * interface — see {@code SchemaDesigner#loadSampleDocuments}) and dispatched to a parser based on+ * the {@code Content-Type} header.+ */+ @POST+ @Path("/{configSet}/analyze")+ @Operation(+ summary = "Analyze sample documents and suggest a schema.",+ description =+ "Sample documents are supplied in the request body. The Content-Type header selects the"+ + " parser: application/json, text/xml or application/xml, text/csv or"+ + " application/csv, or text/plain or application/octet-stream (treated as JSON"+ + " lines). Capped at 5MB and 1000 documents.",+ tags = {"schema-designer"})+ SchemaDesignerAnalyzeResponse analyze(+ @PathParam("configSet") String configSet,+ @QueryParam("schemaVersion") Integer schemaVersion,+ @QueryParam("copyFrom") String copyFrom,+ @QueryParam("uniqueKeyField") String uniqueKeyField,+ @QueryParam("languages") List<String> languages,+ @QueryParam("enableDynamicFields") Boolean enableDynamicFields,+ @QueryParam("enableFieldGuessing") Boolean enableFieldGuessing,+ @QueryParam("enableNestedDocs") Boolean enableNestedDocs)+ throws Exception;++ @GET+ @Path("/{configSet}/query")+ @Operation(+ summary = "Query the temporary collection used during schema design.",+ description =+ "All standard Solr query parameters (q, fq, fl, sort, facet.*, hl.*, etc.) are"+ + " forwarded directly to the temporary collection. The configSet path parameter"+ + " identifies which designer session to query; it is not a query parameter itself.",+ tags = {"schema-designer"})+ FlexibleSolrJerseyResponse query(@PathParam("configSet") String configSet) throws Exception;++ @GET+ @Path("/{configSet}/diff")+ @Operation(+ summary = "Get the diff between the designed schema and the published schema.",+ tags = {"schema-designer"})+ SchemaDesignerSchemaDiffResponse getSchemaDiff(@PathParam("configSet") String configSet)+ throws Exception;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddRequestBody.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddRequestBody.javanew file mode 100644index 00000000000..821cabc4246--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddRequestBody.java@@ -0,0 +1,45 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import io.swagger.v3.oas.annotations.media.Schema;+import java.util.Map;++/**+ * Request body for the Schema Designer add endpoint. Exactly one of the four fields should be+ * populated; the populated field's name is the action and its value carries the schema-object+ * attributes (e.g. for {@code addField}: {@code name}, {@code type}, {@code stored}, etc.).+ */+public class SchemaDesignerAddRequestBody {++ @Schema(name = "addField")+ @JsonProperty("add-field")+ public Map<String, Object> addField;++ @Schema(name = "addDynamicField")+ @JsonProperty("add-dynamic-field")+ public Map<String, Object> addDynamicField;++ @Schema(name = "addCopyField")+ @JsonProperty("add-copy-field")+ public Map<String, Object> addCopyField;++ @Schema(name = "addFieldType")+ @JsonProperty("add-field-type")+ public Map<String, Object> addFieldType;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddResponse.javanew file mode 100644index 00000000000..3d020e104a5--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAddResponse.java@@ -0,0 +1,35 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;++/** Response body for the Schema Designer {@code addSchemaObject} endpoint. */+public class SchemaDesignerAddResponse extends SchemaDesignerResponse {++ /** Name of the field that was added, when the request added a field. */+ @JsonProperty("field")+ public String field;++ /** Name of the dynamic field that was added, when the request added a dynamic field. */+ @JsonProperty("dynamicField")+ public String dynamicField;++ /** Name of the field type that was added, when the request added a field type. */+ @JsonProperty("fieldType")+ public String fieldType;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAnalyzeResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAnalyzeResponse.javanew file mode 100644index 00000000000..3a035aa67e2--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerAnalyzeResponse.java@@ -0,0 +1,27 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;++/** Response body for the Schema Designer {@code analyze} endpoint. */+public class SchemaDesignerAnalyzeResponse extends SchemaDesignerResponse {++ /** Source of the sample documents that were analyzed (e.g. "blob", "request"). */+ @JsonProperty("sampleSource")+ public String sampleSource;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerConfigsResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerConfigsResponse.javanew file mode 100644index 00000000000..8f802598082--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerConfigsResponse.java@@ -0,0 +1,31 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.Map;++/** Response body for the Schema Designer list-configs endpoint. */+public class SchemaDesignerConfigsResponse extends SolrJerseyResponse {++ /**+ * Map of configSet name to status: 0 = in-progress (temp only), 1 = disabled, 2 = enabled and+ * published.+ */+ @JsonProperty("configSets")+ public Map<String, Integer> configSets;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerFileContentsResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerFileContentsResponse.javanew file mode 100644index 00000000000..d5659945d01--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerFileContentsResponse.java@@ -0,0 +1,34 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;++/** Response body for the Schema Designer {@code updateFileContents} endpoint. */+public class SchemaDesignerFileContentsResponse extends SchemaDesignerResponse {++ /** Error message when the updated file (e.g. {@code solrconfig.xml}) fails validation. */+ @JsonProperty("updateFileError")+ public String updateFileError;++ /**+ * The raw file content submitted, returned when validation fails so the UI can display the+ * attempted content alongside the error.+ */+ @JsonProperty("fileContent")+ public String fileContent;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerInfoResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerInfoResponse.javanew file mode 100644index 00000000000..2e77b7066ae--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerInfoResponse.java@@ -0,0 +1,42 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.List;++/** Response body for the Schema Designer get-info endpoint. */+public class SchemaDesignerInfoResponse extends SchemaDesignerSettingsResponse {++ @JsonProperty("configSet")+ public String configSet;++ /** Whether the configSet has a published (live) version. */+ @JsonProperty("published")+ public Boolean published;++ @JsonProperty("schemaVersion")+ public Integer schemaVersion;++ /** Collections currently using this configSet. */+ @JsonProperty("collections")+ public List<String> collections;++ /** Number of sample documents stored for this configSet, if available. */+ @JsonProperty("numDocs")+ public Integer numDocs;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerPublishResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerPublishResponse.javanew file mode 100644index 00000000000..c04ab9e76f2--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerPublishResponse.java@@ -0,0 +1,43 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;++/** Response body for the Schema Designer publish endpoint. */+public class SchemaDesignerPublishResponse extends SolrJerseyResponse {++ @JsonProperty("configSet")+ public String configSet;++ @JsonProperty("schemaVersion")+ public Integer schemaVersion;++ /** The new collection created during publish, if requested. */+ @JsonProperty("newCollection")+ public String newCollection;++ /** Error message if indexing sample docs into the new collection failed. */+ @JsonProperty("updateError")+ public String updateError;++ @JsonProperty("updateErrorCode")+ public Integer updateErrorCode;++ @JsonProperty("errorDetails")+ public Object errorDetails;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerResponse.javanew file mode 100644index 00000000000..87179096130--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerResponse.java@@ -0,0 +1,94 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.List;+import java.util.Map;++/**+ * Base response body for Schema Designer endpoints that operate on a full schema: {@code+ * prepNewSchema} returns this directly; {@code updateFileContents}, {@code addSchemaObject}, {@code+ * updateSchemaObject}, and {@code analyze} return the more specific subclasses declared in this+ * package, adding fields particular to each.+ */+public class SchemaDesignerResponse extends SchemaDesignerSettingsResponse {++ // --- core schema identification ---++ @JsonProperty("configSet")+ public String configSet;++ @JsonProperty("schemaVersion")+ public Integer schemaVersion;++ /** The temporary mutable collection used during design (e.g. {@code ._designer_myConfig}). */+ @JsonProperty("tempCollection")+ public String tempCollection;++ /** Active replica core name for the temp collection, used for Luke API calls. */+ @JsonProperty("core")+ public String core;++ @JsonProperty("uniqueKeyField")+ public String uniqueKeyField;++ /** Collections currently using the published version of this configSet. */+ @JsonProperty("collectionsForConfig")+ public List<String> collectionsForConfig;++ // --- schema objects ---++ @JsonProperty("fields")+ public List<Map<String, Object>> fields;++ @JsonProperty("dynamicFields")+ public List<Map<String, Object>> dynamicFields;++ @JsonProperty("fieldTypes")+ public List<Map<String, Object>> fieldTypes;++ /** ConfigSet files available in ZooKeeper (excluding managed-schema and internal files). */+ @JsonProperty("files")+ public List<String> files;++ /** IDs of the first 100 sample documents (present when docs were loaded/analyzed). */+ @JsonProperty("docIds")+ public List<String> docIds;++ /** Total number of sample documents, or -1 when no docs were passed to the endpoint. */+ @JsonProperty("numDocs")+ public Integer numDocs;++ // --- error fields (set when sample-doc indexing fails) ---++ @JsonProperty("updateError")+ public String updateError;++ @JsonProperty("updateErrorCode")+ public Integer updateErrorCode;++ @JsonProperty("errorDetails")+ public Object errorDetails;++ /**+ * Analysis warning when field-type inference produced errors; set by {@code analyze}, {@code+ * updateFileContents}, and {@code updateSchemaObject}.+ */+ @JsonProperty("analysisError")+ public String analysisError;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSchemaDiffResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSchemaDiffResponse.javanew file mode 100644index 00000000000..3b6b2015c30--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSchemaDiffResponse.java@@ -0,0 +1,32 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.Map;++/** Response body for the Schema Designer get-schema-diff endpoint. */+public class SchemaDesignerSchemaDiffResponse extends SchemaDesignerSettingsResponse {++ /** The list of field-level differences between the designed schema and the source. */+ @JsonProperty("diff")+ public Map<String, Object> diff;++ /** The configSet used as the diff source (either the published configSet or copyFrom). */+ @JsonProperty("diff-source")+ public String diffSource;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSettingsResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSettingsResponse.javanew file mode 100644index 00000000000..bc3d4a3a402--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerSettingsResponse.java@@ -0,0 +1,45 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.List;++/** Base response for Schema Designer endpoints that surface the designer settings. */+public abstract class SchemaDesignerSettingsResponse extends SolrJerseyResponse {++ @JsonProperty("languages")+ public List<String> languages;++ @JsonProperty("enableFieldGuessing")+ public Boolean enableFieldGuessing;++ @JsonProperty("enableDynamicFields")+ public Boolean enableDynamicFields;++ @JsonProperty("enableNestedDocs")+ public Boolean enableNestedDocs;++ @JsonProperty("disabled")+ public Boolean disabled;++ @JsonProperty("publishedVersion")+ public Integer publishedVersion;++ @JsonProperty("copyFrom")+ public String copyFrom;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateRequestBody.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateRequestBody.javanew file mode 100644index 00000000000..54b9bb9e56c--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateRequestBody.java@@ -0,0 +1,48 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonAnyGetter;+import com.fasterxml.jackson.annotation.JsonAnySetter;+import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.HashMap;+import java.util.Map;++/**+ * Request body for the Schema Designer update endpoint: a flat field or field-type definition. The+ * {@code name} property is required; remaining schema attributes (e.g. {@code type}, {@code+ * indexed}, {@code stored}, {@code analyzer}, {@code copyDest}) are captured via the dynamic {@code+ * additionalProperties} map and forwarded to the Schema API.+ */+public class SchemaDesignerUpdateRequestBody {++ @JsonProperty public String name;++ // Non-final + public so the OpenAPI-generated SolrJ client can assign to it directly.+ // Accessed via @JsonAnyGetter / @JsonAnySetter for JSON (de)serialization.+ public Map<String, Object> additionalProperties = new HashMap<>();++ @JsonAnyGetter+ public Map<String, Object> getAdditionalProperties() {+ return additionalProperties;+ }++ @JsonAnySetter+ public void setAdditionalProperty(String key, Object value) {+ additionalProperties.put(key, value);+ }+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateResponse.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateResponse.javanew file mode 100644index 00000000000..15a371017e9--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaDesignerUpdateResponse.java@@ -0,0 +1,40 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import java.util.Map;++/** Response body for the Schema Designer {@code updateSchemaObject} endpoint. */+public class SchemaDesignerUpdateResponse extends SchemaDesignerResponse {++ /** The type of schema object that was updated: {@code "field"} or {@code "type"}. */+ @JsonProperty("updateType")+ public String updateType;++ /** The updated field definition; populated when {@code updateType} is {@code "field"}. */+ @JsonProperty("field")+ public Map<String, Object> field;++ /** The updated field-type definition; populated when {@code updateType} is {@code "type"}. */+ @JsonProperty("type")+ public Map<String, Object> type;++ /** Whether the temp collection needed to be rebuilt to apply this update. */+ @JsonProperty("rebuild")+ public Boolean rebuild;+}diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex 2afae17b49e..81f3b067fd0 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -124,7 +124,7 @@ import org.apache.solr.handler.admin.ZookeeperInfoHandler;import org.apache.solr.handler.admin.ZookeeperRead;import org.apache.solr.handler.admin.ZookeeperStatusHandler;import org.apache.solr.handler.component.ShardHandlerFactory;-import org.apache.solr.handler.designer.SchemaDesignerAPI;+import org.apache.solr.handler.designer.SchemaDesigner;import org.apache.solr.jersey.InjectionFactories;import org.apache.solr.jersey.JerseyAppHandlerCache;import org.apache.solr.logging.LogWatcher;@@ -877,7 +877,7 @@ public class CoreContainer {registerV2Api(clusterAPI.commands);if (isZooKeeperAware()) {- registerV2Api(new SchemaDesignerAPI(this));+ registerV2Api(SchemaDesigner.class);} // else Schema Designer not available in standalone (non-cloud) mode/*diff --git a/solr/core/src/java/org/apache/solr/handler/designer/DefaultSampleDocumentsLoader.java b/solr/core/src/java/org/apache/solr/handler/designer/DefaultSampleDocumentsLoader.javaindex fa796fc0709..819a20bf589 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/DefaultSampleDocumentsLoader.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/DefaultSampleDocumentsLoader.java@@ -102,7 +102,7 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {+ MAX_STREAM_SIZE+ " bytes is the max upload size for sample documents.");}- // use a byte stream for the parsers in case they need to re-parse using a different strategy+ // use a byte stream for the parsers in case they need to reparse using a different strategy// e.g. JSON vs. JSON lines or different CSV strategies ...ContentStreamBase.ByteArrayStream byteStream =new ContentStreamBase.ByteArrayStream(uploadedBytes, fileSource, contentType);@@ -153,7 +153,6 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {.loadDocs(stream);}- @SuppressWarnings("unchecked")protected List<SolrInputDocument> loadJsonLines(ContentStreamBase.ByteArrayStream stream, final int maxDocsToLoad) throws IOException {List<Map<String, Object>> docs = new ArrayList<>();@@ -161,13 +160,7 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {BufferedReader br = new BufferedReader(r);String line;while ((line = br.readLine()) != null) {- line = line.trim();- if (!line.isEmpty() && line.startsWith("{") && line.endsWith("}")) {- Object jsonLine = ObjectBuilder.getVal(new JSONParser(line));- if (jsonLine instanceof Map) {- docs.add((Map<String, Object>) jsonLine);- }- }+ parseStringToJson(docs, line);if (maxDocsToLoad > 0 && docs.size() == maxDocsToLoad) {break;}@@ -177,6 +170,19 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {return docs.stream().map(JsonLoader::buildDoc).collect(Collectors.toList());}+ private void parseStringToJson(List<Map<String, Object>> docs, String line) throws IOException {+ line = line.trim();+ if (line.startsWith("{") && line.endsWith("}")) {+ Object jsonLine = ObjectBuilder.getVal(new JSONParser(line));+ if (jsonLine instanceof Map<?, ?> rawMap) {+ // JSON object keys are always Strings; the cast is safe+ @SuppressWarnings("unchecked")+ Map<String, Object> typedMap = (Map<String, Object>) rawMap;+ docs.add(typedMap);+ }+ }+ }+@SuppressWarnings("unchecked")protected List<SolrInputDocument> loadJsonDocs(ContentStreamBase.ByteArrayStream stream, final int maxDocsToLoad) throws IOException {@@ -205,7 +211,7 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {if (lines.length > 1) {for (String line : lines) {line = line.trim();- if (!line.isEmpty() && line.startsWith("{") && line.endsWith("}")) {+ if (line.startsWith("{") && line.endsWith("}")) {isJsonLines = true;break;}@@ -295,17 +301,10 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader {}}- @SuppressWarnings("unchecked")protected List<Map<String, Object>> loadJsonLines(String[] lines) throws IOException {List<Map<String, Object>> docs = new ArrayList<>(lines.length);for (String line : lines) {- line = line.trim();- if (!line.isEmpty() && line.startsWith("{") && line.endsWith("}")) {- Object jsonLine = ObjectBuilder.getVal(new JSONParser(line));- if (jsonLine instanceof Map) {- docs.add((Map<String, Object>) jsonLine);- }- }+ parseStringToJson(docs, line);}return docs;}diff --git a/solr/core/src/java/org/apache/solr/handler/designer/DefaultSchemaSuggester.java b/solr/core/src/java/org/apache/solr/handler/designer/DefaultSchemaSuggester.javaindex 543b7a77af1..963cae66283 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/DefaultSchemaSuggester.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/DefaultSchemaSuggester.java@@ -170,8 +170,7 @@ public class DefaultSchemaSuggester implements SchemaSuggester {throw new IllegalStateException("FieldType '" + fieldTypeName + "' not found in the schema!");}- Map<String, String> fieldProps =- guessFieldProps(fieldName, fieldType, sampleValues, isMV, schema);+ Map<String, String> fieldProps = guessFieldProps(fieldName, fieldType, isMV, schema);SchemaField schemaField = schema.newField(fieldName, fieldTypeName, fieldProps);return Optional.of(schemaField);}@@ -179,9 +178,9 @@ public class DefaultSchemaSuggester implements SchemaSuggester {@Overridepublic ManagedIndexSchema adaptExistingFieldToData(SchemaField schemaField, List<Object> sampleValues, ManagedIndexSchema schema) {- // Promote a single-valued to multi-valued if needed+ // Promote a single-valued to multivalued if neededif (!schemaField.multiValued() && isMultiValued(sampleValues)) {- // this existing field needs to be promoted to multi-valued+ // this existing field needs to be promoted to multivaluedSimpleOrderedMap<Object> fieldProps = schemaField.getNamedPropertyValues(false);fieldProps.add("multiValued", true);fieldProps.remove("name");@@ -210,7 +209,7 @@ public class DefaultSchemaSuggester implements SchemaSuggester {Collection<Object> fieldValues = doc.getFieldValues(f);if (fieldValues != null && !fieldValues.isEmpty()) {if (fieldValues.size() == 1) {- // flatten so every field doesn't end up multi-valued+ // flatten so every field doesn't end up multivaluedvalues.add(fieldValues.iterator().next());} else {// truly multi-valued@@ -395,11 +394,7 @@ public class DefaultSchemaSuggester implements SchemaSuggester {}protected Map<String, String> guessFieldProps(- String fieldName,- FieldType fieldType,- List<Object> sampleValues,- boolean isMV,- IndexSchema schema) {+ String fieldName, FieldType fieldType, boolean isMV, IndexSchema schema) {Map<String, String> props = new HashMap<>();props.put("indexed", "true");diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SampleDocuments.java b/solr/core/src/java/org/apache/solr/handler/designer/SampleDocuments.javaindex b98c5995db2..6037db51524 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SampleDocuments.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SampleDocuments.java@@ -56,7 +56,7 @@ public class SampleDocuments {return id != null&& !ids.contains(id); // doc has ID, and it's not already in the set})- .collect(Collectors.toList());+ .toList();parsed.addAll(toAdd);if (maxDocsToLoad > 0 && parsed.size() > maxDocsToLoad) {parsed = parsed.subList(0, maxDocsToLoad);diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesigner.javasimilarity index 63%rename from solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.javarename to solr/core/src/java/org/apache/solr/handler/designer/SchemaDesigner.javaindex bced3660ef7..265de686a7f 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesigner.java@@ -17,14 +17,12 @@package org.apache.solr.handler.designer;-import static org.apache.solr.client.solrj.SolrRequest.METHOD.GET;-import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;-import static org.apache.solr.client.solrj.SolrRequest.METHOD.PUT;import static org.apache.solr.common.params.CommonParams.JSON_MIME;import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM;import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_READ_PERM;+import jakarta.inject.Inject;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;@@ -46,10 +44,25 @@ import java.util.Map;import java.util.Objects;import java.util.Optional;import java.util.Set;-import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;-import org.apache.solr.api.EndPoint;+import org.apache.solr.api.JerseyResource;+import org.apache.solr.client.api.endpoint.SchemaDesignerApi;+import org.apache.solr.client.api.model.FlexibleSolrJerseyResponse;+import org.apache.solr.client.api.model.ListCollectionsResponse;+import org.apache.solr.client.api.model.SchemaDesignerAddRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerAddResponse;+import org.apache.solr.client.api.model.SchemaDesignerAnalyzeResponse;+import org.apache.solr.client.api.model.SchemaDesignerConfigsResponse;+import org.apache.solr.client.api.model.SchemaDesignerFileContentsResponse;+import org.apache.solr.client.api.model.SchemaDesignerInfoResponse;+import org.apache.solr.client.api.model.SchemaDesignerPublishResponse;+import org.apache.solr.client.api.model.SchemaDesignerResponse;+import org.apache.solr.client.api.model.SchemaDesignerSchemaDiffResponse;+import org.apache.solr.client.api.model.SchemaDesignerSettingsResponse;+import org.apache.solr.client.api.model.SchemaDesignerUpdateRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerUpdateResponse;+import org.apache.solr.client.api.model.SolrJerseyResponse;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CloudSolrClient;import org.apache.solr.client.solrj.request.CollectionAdminRequest;@@ -57,6 +70,7 @@ import org.apache.solr.client.solrj.request.SolrQuery;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.cloud.ZkConfigSetService;import org.apache.solr.cloud.ZkSolrResourceLoader;+import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.SolrInputField;@@ -66,16 +80,14 @@ import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.cloud.ZkMaintenanceUtils;import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.common.util.ContentStream;-import org.apache.solr.common.util.ContentStreamBase;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.StrUtils;import org.apache.solr.core.CoreContainer;import org.apache.solr.core.SolrConfig;import org.apache.solr.core.SolrResourceLoader;+import org.apache.solr.jersey.PermissionName;import org.apache.solr.request.SolrQueryRequest;-import org.apache.solr.response.RawResponseWriter;-import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.schema.ManagedIndexSchema;import org.apache.solr.schema.SchemaField;import org.apache.solr.util.RTimer;@@ -86,7 +98,8 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** All V2 APIs have a prefix of /api/schema-designer/ */-public class SchemaDesignerAPI implements SchemaDesignerConstants {+public class SchemaDesigner extends JerseyResource+ implements SchemaDesignerApi, SchemaDesignerConstants {private static final Set<String> excludeConfigSetNames = Set.of(DEFAULT_CONFIGSET_NAME);@@ -97,22 +110,27 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {private final SampleDocumentsLoader sampleDocLoader;private final SchemaDesignerSettingsDAO settingsDAO;private final SchemaDesignerConfigSetHelper configSetHelper;- private final Map<String, Integer> indexedVersion = new ConcurrentHashMap<>();- public SchemaDesignerAPI(CoreContainer coreContainer) {+ private final SolrQueryRequest solrQueryRequest;++ @Inject+ public SchemaDesigner(CoreContainer coreContainer, SolrQueryRequest solrQueryRequest) {this(coreContainer,- SchemaDesignerAPI.newSchemaSuggester(),- SchemaDesignerAPI.newSampleDocumentsLoader());+ SchemaDesigner.newSchemaSuggester(),+ SchemaDesigner.newSampleDocumentsLoader(),+ solrQueryRequest);}- SchemaDesignerAPI(+ SchemaDesigner(CoreContainer coreContainer,SchemaSuggester schemaSuggester,- SampleDocumentsLoader sampleDocLoader) {+ SampleDocumentsLoader sampleDocLoader,+ SolrQueryRequest solrQueryRequest) {this.coreContainer = coreContainer;this.schemaSuggester = schemaSuggester;this.sampleDocLoader = sampleDocLoader;+ this.solrQueryRequest = solrQueryRequest;this.configSetHelper =new SchemaDesignerConfigSetHelper(this.coreContainer, this.schemaSuggester);this.settingsDAO = new SchemaDesignerSettingsDAO(coreContainer, configSetHelper);@@ -146,14 +164,56 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return DESIGNER_PREFIX + configSet;}- @EndPoint(method = GET, path = "/schema-designer/info", permission = CONFIG_READ_PERM)- public void getInfo(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);+ // Last-indexed schema version per temp collection, cached in ZK so it survives across+ // per-request SchemaDesigner instances.- Map<String, Object> responseMap = new HashMap<>();- responseMap.put(CONFIG_SET_PARAM, configSet);+ private Integer getIndexedVersion(String mutableId) throws IOException {+ String path = getConfigSetZkPath(mutableId, INDEXED_VERSION_ZNODE);+ try {+ byte[] data = zkStateReader().getZkClient().getData(path, null, null);+ return data != null && data.length > 0+ ? Integer.valueOf(new String(data, StandardCharsets.UTF_8))+ : null;+ } catch (KeeperException.NoNodeException nne) {+ return null;+ } catch (KeeperException | InterruptedException e) {+ throw new IOException(+ "Failed to read data from ZK at path: " + path, SolrZkClient.checkInterrupted(e));+ }+ }++ private void putIndexedVersion(String mutableId, int version) throws IOException {+ String path = getConfigSetZkPath(mutableId, INDEXED_VERSION_ZNODE);+ byte[] data = String.valueOf(version).getBytes(StandardCharsets.UTF_8);+ try {+ zkStateReader().getZkClient().makePath(path, data, false);+ } catch (KeeperException | InterruptedException e) {+ throw new IOException(+ "Failed to save data in ZK at path: " + path, SolrZkClient.checkInterrupted(e));+ }+ }++ private void removeIndexedVersion(String mutableId) throws IOException {+ String path = getConfigSetZkPath(mutableId, INDEXED_VERSION_ZNODE);+ try {+ zkStateReader().getZkClient().delete(path, -1);+ } catch (KeeperException.NoNodeException nne) {+ // already gone, nothing to do+ } catch (KeeperException | InterruptedException e) {+ throw new IOException(+ "Failed to delete data from ZK at path: " + path, SolrZkClient.checkInterrupted(e));+ }+ }++ @Override+ @PermissionName(CONFIG_READ_PERM)+ public SchemaDesignerInfoResponse getInfo(String configSet) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);++ final var response = instantiateJerseyResponse(SchemaDesignerInfoResponse.class);+ response.configSet = configSet;boolean exists = configExists(configSet);- responseMap.put("published", exists);+ response.published = exists;// mutable config may not exist yet as this is just an info check to gather some basic info the// UI needs@@ -164,72 +224,64 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {SolrConfig srcConfig = exists ? configSetHelper.loadSolrConfig(configSet) : null;SolrConfig solrConfig =configExists(mutableId) ? configSetHelper.loadSolrConfig(mutableId) : srcConfig;- addSettingsToResponse(settingsDAO.getSettings(solrConfig), responseMap);+ addSettingsToResponse(settingsDAO.getSettings(solrConfig), response);- responseMap.put(SCHEMA_VERSION_PARAM, configSetHelper.getCurrentSchemaVersion(mutableId));- responseMap.put(- "collections", exists ? configSetHelper.listCollectionsForConfig(configSet) : List.of());+ response.schemaVersion = configSetHelper.getCurrentSchemaVersion(mutableId);+ response.collections = exists ? configSetHelper.listCollectionsForConfig(configSet) : List.of();// don't fail if loading sample docs failstry {- responseMap.put("numDocs", configSetHelper.retrieveSampleDocs(configSet).size());+ response.numDocs = configSetHelper.retrieveSampleDocs(configSet).size();} catch (Exception exc) {log.warn("Failed to load sample docs from blob store for {}", configSet, exc);}- rsp.getValues().addAll(responseMap);+ return response;}- @EndPoint(method = POST, path = "/schema-designer/prep", permission = CONFIG_EDIT_PERM)- public void prepNewSchema(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerResponse prepNewSchema(String configSet, String copyFrom) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);validateNewConfigSetName(configSet);- final String copyFrom = req.getParams().get(COPY_FROM_PARAM, DEFAULT_CONFIGSET_NAME);+ final String effectiveCopyFrom = copyFrom != null ? copyFrom : DEFAULT_CONFIGSET_NAME;- SchemaDesignerSettings settings = getMutableSchemaForConfigSet(configSet, -1, copyFrom);+ SchemaDesignerSettings settings =+ getMutableSchemaForConfigSet(configSet, -1, effectiveCopyFrom);ManagedIndexSchema schema = settings.getSchema();String mutableId = getMutableId(configSet);// make sure the temp collection for this analysis existsif (!zkStateReader().getClusterState().hasCollection(mutableId)) {- indexedVersion.remove(mutableId);+ removeIndexedVersion(mutableId);configSetHelper.createCollection(mutableId, mutableId);}settingsDAO.persistIfChanged(mutableId, settings);- rsp.getValues().addAll(buildResponse(configSet, schema, settings, null));+ return buildSchemaDesignerResponse(+ SchemaDesignerResponse.class, configSet, schema, settings, null);}- @EndPoint(method = PUT, path = "/schema-designer/cleanup", permission = CONFIG_EDIT_PERM)- public void cleanupTemp(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- cleanupTemp(getRequiredParam(CONFIG_SET_PARAM, req));+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SolrJerseyResponse cleanupTempSchema(String configSet) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ doCleanupTemp(configSet);+ return instantiateJerseyResponse(SolrJerseyResponse.class);}- @EndPoint(method = GET, path = "/schema-designer/file", permission = CONFIG_READ_PERM)- public void getFileContents(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String file = getRequiredParam("file", req);- String filePath = getConfigSetZkPath(getMutableId(configSet), file);- byte[] data;- try {- data = zkStateReader().getZkClient().getData(filePath, null, null);- } catch (KeeperException | InterruptedException e) {- throw new IOException("Error reading file: " + filePath, SolrZkClient.checkInterrupted(e));+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerFileContentsResponse updateFileContents(+ String configSet, String file, InputStream fileContents) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ requireNotEmpty("file", file);+ if (fileContents == null) {+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST, "Request body with file contents is required!");}- String stringData =- data != null && data.length > 0 ? new String(data, StandardCharsets.UTF_8) : "";- rsp.getValues().addAll(Map.of(file, stringData));- }-- @EndPoint(method = POST, path = "/schema-designer/file", permission = CONFIG_EDIT_PERM)- public void updateFileContents(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String file = getRequiredParam("file", req);String mutableId = getMutableId(configSet);String zkPath = getConfigSetZkPath(mutableId, file);@@ -240,7 +292,7 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}byte[] data;- try (InputStream in = extractSingleContentStream(req, true).getStream()) {+ try (InputStream in = fileContents) {data = in.readAllBytes();}Exception updateFileError = null;@@ -261,11 +313,10 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {// solrconfig.xml update failed, but haven't impacted the configSet yet, so just return the// error directlyThrowable causedBy = SolrException.getRootCause(updateFileError);- Map<String, Object> response = new HashMap<>();- response.put("updateFileError", causedBy.getMessage());- response.put(file, new String(data, StandardCharsets.UTF_8));- rsp.getValues().addAll(response);- return;+ final var errorResponse = instantiateJerseyResponse(SchemaDesignerFileContentsResponse.class);+ errorResponse.updateFileError = causedBy.getMessage();+ errorResponse.fileContent = new String(data, StandardCharsets.UTF_8);+ return errorResponse;}// apply the update and reload the temp collection / re-index sample docs@@ -295,10 +346,12 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}}- Map<String, Object> response = buildResponse(configSet, schema, null, docs);+ SchemaDesignerFileContentsResponse response =+ buildSchemaDesignerResponse(+ SchemaDesignerFileContentsResponse.class, configSet, schema, null, docs);if (analysisErrorHolder[0] != null) {- response.put(ANALYSIS_ERROR, analysisErrorHolder[0]);+ response.analysisError = analysisErrorHolder[0];}addErrorToResponse(@@ -308,15 +361,16 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {response,"Failed to re-index sample documents after update to the " + file + " file");- rsp.getValues().addAll(response);+ return response;}- @EndPoint(method = GET, path = "/schema-designer/sample", permission = CONFIG_READ_PERM)- public void getSampleValue(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String fieldName = getRequiredParam(FIELD_PARAM, req);- final String idField = getRequiredParam(UNIQUE_KEY_FIELD_PARAM, req);- String docId = req.getParams().get(DOC_ID_PARAM);+ @Override+ @PermissionName(CONFIG_READ_PERM)+ public FlexibleSolrJerseyResponse getSampleValue(+ String configSet, String fieldName, String idField, String docId) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ requireNotEmpty(FIELD_PARAM, fieldName);+ requireNotEmpty(UNIQUE_KEY_FIELD_PARAM, idField);final List<SolrInputDocument> docs = configSetHelper.retrieveSampleDocs(configSet);String textValue = null;@@ -347,25 +401,29 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {if (textValue != null) {var analysis = configSetHelper.analyzeField(configSet, fieldName, textValue);- rsp.getValues().addAll(Map.of(idField, docId, fieldName, textValue, "analysis", analysis));+ return buildFlexibleResponse(+ Map.of(idField, docId, fieldName, textValue, "analysis", analysis));}+ return instantiateJerseyResponse(FlexibleSolrJerseyResponse.class);}- @EndPoint(- method = GET,- path = "/schema-designer/collectionsForConfig",- permission = CONFIG_READ_PERM)- public void listCollectionsForConfig(SolrQueryRequest req, SolrQueryResponse rsp) {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- rsp.getValues()- .addAll(Map.of("collections", configSetHelper.listCollectionsForConfig(configSet)));+ @Override+ @PermissionName(CONFIG_READ_PERM)+ public ListCollectionsResponse listCollectionsForConfig(String configSet) {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ final var response = instantiateJerseyResponse(ListCollectionsResponse.class);+ response.collections = configSetHelper.listCollectionsForConfig(configSet);+ return response;}// CONFIG_EDIT_PERM is required here since this endpoint is used by the UI to determine if the// user has access to the Schema Designer UI- @EndPoint(method = GET, path = "/schema-designer/configs", permission = CONFIG_EDIT_PERM)- public void listConfigs(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- rsp.getValues().addAll(Map.of("configSets", listEnabledConfigs()));+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerConfigsResponse listDesignerConfigs() throws Exception {+ final var response = instantiateJerseyResponse(SchemaDesignerConfigsResponse.class);+ response.configSets = listEnabledConfigs();+ return response;}protected Map<String, Integer> listEnabledConfigs() throws IOException {@@ -384,69 +442,77 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return configs;}- @EndPoint(method = GET, path = "/schema-designer/download/*", permission = CONFIG_READ_PERM)- public void downloadConfig(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- String mutableId = getMutableId(configSet);-- // find the configSet to download- SolrZkClient zkClient = zkStateReader().getZkClient();- String configId = mutableId;- try {- if (!zkClient.exists(getConfigSetZkPath(mutableId, null))) {- if (zkClient.exists(getConfigSetZkPath(configSet, null))) {- configId = configSet;- } else {- throw new SolrException(- SolrException.ErrorCode.NOT_FOUND, "ConfigSet " + configSet + " not found!");- }- }- } catch (KeeperException | InterruptedException e) {- throw new IOException("Error reading config from ZK", SolrZkClient.checkInterrupted(e));+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerAddResponse addSchemaObject(+ String configSet, Integer schemaVersion, SchemaDesignerAddRequestBody requestBody)+ throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ requireSchemaVersion(schemaVersion);+ final String mutableId = checkMutable(configSet, schemaVersion);++ String action;+ Map<String, Object> attrs;+ if (requestBody == null) {+ action = null;+ attrs = null;+ } else if (requestBody.addField != null) {+ action = "add-field";+ attrs = requestBody.addField;+ } else if (requestBody.addDynamicField != null) {+ action = "add-dynamic-field";+ attrs = requestBody.addDynamicField;+ } else if (requestBody.addCopyField != null) {+ action = "add-copy-field";+ attrs = requestBody.addCopyField;+ } else if (requestBody.addFieldType != null) {+ action = "add-field-type";+ attrs = requestBody.addFieldType;+ } else {+ action = null;+ attrs = null;+ }+ if (action == null) {+ throw new SolrException(+ SolrException.ErrorCode.BAD_REQUEST,+ "Request body must contain exactly one of: add-field, add-dynamic-field, add-copy-field, add-field-type");}- ContentStreamBase content =- new ContentStreamBase.ByteArrayStream(- configSetHelper.downloadAndZipConfigSet(configId),- configSet + ".zip",- "application/zip");- rsp.add(RawResponseWriter.CONTENT, content);- }-- @EndPoint(method = POST, path = "/schema-designer/add", permission = CONFIG_EDIT_PERM)- public void addSchemaObject(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String mutableId = checkMutable(configSet, req);-- Map<String, Object> addJson = readJsonFromRequest(req);+ Map<String, Object> addJson = Map.of(action, attrs);log.info("Adding new schema object from JSON: {}", addJson);String objectName = configSetHelper.addSchemaObject(configSet, addJson);- String action = addJson.keySet().iterator().next();ManagedIndexSchema schema = loadLatestSchema(mutableId);- Map<String, Object> response =- buildResponse(configSet, schema, null, configSetHelper.retrieveSampleDocs(configSet));- response.put(action, objectName);- rsp.getValues().addAll(response);+ SchemaDesignerAddResponse response =+ buildSchemaDesignerResponse(+ SchemaDesignerAddResponse.class,+ configSet,+ schema,+ null,+ configSetHelper.retrieveSampleDocs(configSet));+ setSchemaObjectField(response, action, objectName);+ return response;}- @EndPoint(method = PUT, path = "/schema-designer/update", permission = CONFIG_EDIT_PERM)- public void updateSchemaObject(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String mutableId = checkMutable(configSet, req);+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerUpdateResponse updateSchemaObject(+ String configSet, Integer schemaVersion, SchemaDesignerUpdateRequestBody requestBody)+ throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ requireSchemaVersion(schemaVersion);+ final String mutableId = checkMutable(configSet, schemaVersion);- // Updated field definition is in the request body as JSON- Map<String, Object> updateField = readJsonFromRequest(req);- String name = (String) updateField.get("name");- if (StrUtils.isNullOrEmpty(name)) {+ if (requestBody == null || StrUtils.isNullOrEmpty(requestBody.name)) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"Invalid update request! JSON payload is missing the required name property: "- + updateField);+ + requestBody);}+ String name = requestBody.name;+ Map<String, Object> updateField = new HashMap<>(requestBody.getAdditionalProperties());+ updateField.put("name", name);log.info("Updating schema object: configSet={}, mutableId={}, name={}, JSON={}",configSet,@@ -486,29 +552,51 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}}- Map<String, Object> response = buildResponse(configSet, schema, settings, docs);- response.put("updateType", updateType);+ SchemaDesignerUpdateResponse response =+ buildSchemaDesignerResponse(+ SchemaDesignerUpdateResponse.class, configSet, schema, settings, docs);+ response.updateType = updateType;if (FIELD_PARAM.equals(updateType)) {- response.put(updateType, fieldToMap(schema.getField(name), schema));+ response.field = fieldToMap(schema.getField(name), schema);} else if ("type".equals(updateType)) {- response.put(updateType, schema.getFieldTypeByName(name).getNamedPropertyValues(true));+ response.type = schema.getFieldTypeByName(name).getNamedPropertyValues(true);}if (analysisErrorHolder[0] != null) {- response.put(ANALYSIS_ERROR, analysisErrorHolder[0]);+ response.analysisError = analysisErrorHolder[0];}addErrorToResponse(mutableId, solrExc, errorsDuringIndexing, response, updateError);- response.put("rebuild", needsRebuild);- rsp.getValues().addAll(response);+ response.rebuild = needsRebuild;+ return response;}- @EndPoint(method = PUT, path = "/schema-designer/publish", permission = CONFIG_EDIT_PERM)- public void publish(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);- final String mutableId = checkMutable(configSet, req);+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerPublishResponse publish(+ String configSet,+ Integer schemaVersion,+ String newCollection,+ Boolean reloadCollections,+ Integer numShards,+ Integer replicationFactor,+ Boolean indexToCollection,+ Boolean cleanupTempParam,+ Boolean disableDesigner)+ throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);+ requireSchemaVersion(schemaVersion);+ final String mutableId = checkMutable(configSet, schemaVersion);++ // @DefaultValue on the interface only fires through JAX-RS injection; direct Java callers+ // bypass it, so we resolve here to guard against NPE on unboxing below.+ final boolean doReloadCollections = Objects.requireNonNullElse(reloadCollections, false);+ final int shards = Objects.requireNonNullElse(numShards, 1);+ final int replicas = Objects.requireNonNullElse(replicationFactor, 1);+ final boolean doIndexToCollection = Objects.requireNonNullElse(indexToCollection, false);+ final boolean doCleanupTemp = Objects.requireNonNullElse(cleanupTempParam, true);+ final boolean doDisableDesigner = Objects.requireNonNullElse(disableDesigner, false);// verify the configSet we're going to apply changes to hasn't been changed since being loaded// for@@ -531,7 +619,6 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}}- String newCollection = req.getParams().get(NEW_COLLECTION_PARAM);if (StrUtils.isNotNullOrEmpty(newCollection)&& zkStateReader().getClusterState().hasCollection(newCollection)) {throw new SolrException(@@ -552,8 +639,7 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {copyConfig(mutableId, configSet);}- boolean reloadCollections = req.getParams().getBool(RELOAD_COLLECTIONS_PARAM, false);- if (reloadCollections) {+ if (doReloadCollections) {log.debug("Reloading collections after update to configSet: {}", configSet);List<String> collectionsForConfig = configSetHelper.listCollectionsForConfig(configSet);CloudSolrClient csc = cloudClient();@@ -565,10 +651,8 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {// create new collectionMap<Object, Throwable> errorsDuringIndexing = null;if (StrUtils.isNotNullOrEmpty(newCollection)) {- int numShards = req.getParams().getInt("numShards", 1);- int rf = req.getParams().getInt("replicationFactor", 1);- configSetHelper.createCollection(newCollection, configSet, numShards, rf);- if (req.getParams().getBool(INDEX_TO_COLLECTION_PARAM, false)) {+ configSetHelper.createCollection(newCollection, configSet, shards, replicas);+ if (doIndexToCollection) {List<SolrInputDocument> docs = configSetHelper.retrieveSampleDocs(configSet);if (!docs.isEmpty()) {ManagedIndexSchema schema = loadLatestSchema(mutableId);@@ -578,35 +662,44 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}}- if (req.getParams().getBool(CLEANUP_TEMP_PARAM, true)) {+ if (doCleanupTemp) {try {- cleanupTemp(configSet);+ doCleanupTemp(configSet);} catch (IOException | SolrServerException | SolrException exc) {final String excStr = exc.toString();log.warn("Failed to clean-up temp collection {} due to: {}", mutableId, excStr);}}- settings.setDisabled(req.getParams().getBool(DISABLE_DESIGNER_PARAM, false));+ settings.setDisabled(doDisableDesigner);settingsDAO.persistIfChanged(configSet, settings);- Map<String, Object> response = new HashMap<>();- response.put(CONFIG_SET_PARAM, configSet);- response.put(SCHEMA_VERSION_PARAM, configSetHelper.getCurrentSchemaVersion(configSet));+ final var response = instantiateJerseyResponse(SchemaDesignerPublishResponse.class);+ response.configSet = configSet;+ response.schemaVersion = configSetHelper.getCurrentSchemaVersion(configSet);if (StrUtils.isNotNullOrEmpty(newCollection)) {- response.put(NEW_COLLECTION_PARAM, newCollection);+ response.newCollection = newCollection;}- addErrorToResponse(newCollection, null, errorsDuringIndexing, response, null);+ addErrorToResponse(newCollection, null, errorsDuringIndexing, response);- rsp.getValues().addAll(response);+ return response;}- @EndPoint(method = POST, path = "/schema-designer/analyze", permission = CONFIG_EDIT_PERM)- public void analyze(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final int schemaVersion = req.getParams().getInt(SCHEMA_VERSION_PARAM, -1);- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);+ @Override+ @PermissionName(CONFIG_EDIT_PERM)+ public SchemaDesignerAnalyzeResponse analyze(+ String configSet,+ Integer schemaVersion,+ String copyFrom,+ String uniqueKeyField,+ List<String> languages,+ Boolean enableDynamicFields,+ Boolean enableFieldGuessing,+ Boolean enableNestedDocs)+ throws Exception {+ final int schemaVersionInt = schemaVersion != null ? schemaVersion : -1;+ requireNotEmpty(CONFIG_SET_PARAM, configSet);// don't let the user edit the _default configSet with the designer (for now)if (DEFAULT_CONFIGSET_NAME.equals(configSet)) {@@ -620,27 +713,28 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {// Get the sample documents to analyze, preferring those in the request but falling back to// previously stored- SampleDocuments sampleDocuments = loadSampleDocuments(req, configSet);+ SampleDocuments sampleDocuments = loadSampleDocuments(configSet);- // Get a mutable "temp" schema either from the specified copy source or configSet if it already- // exists.- String copyFrom =+ // Get a mutable "temp" schema either from the specified copy source or configSet if it+ // already exists. Must stay unconditional, not just a fallback for copyFrom == null: below,+ // copyFrom is also relied on for syncing language files, and must match what the schema was+ // actually copied from.+ copyFrom =configExists(configSet)? configSet- : req.getParams().get(COPY_FROM_PARAM, DEFAULT_CONFIGSET_NAME);+ : (copyFrom != null ? copyFrom : DEFAULT_CONFIGSET_NAME);String mutableId = getMutableId(configSet);// holds additional settings needed by the designer to maintain stateSchemaDesignerSettings settings =- getMutableSchemaForConfigSet(configSet, schemaVersion, copyFrom);+ getMutableSchemaForConfigSet(configSet, schemaVersionInt, copyFrom);ManagedIndexSchema schema = settings.getSchema();- String uniqueKeyFieldParam = req.getParams().get(UNIQUE_KEY_FIELD_PARAM);- if (StrUtils.isNotNullOrEmpty(uniqueKeyFieldParam)) {- String uniqueKeyField =+ if (StrUtils.isNotNullOrEmpty(uniqueKeyField)) {+ String existingKeyField =schema.getUniqueKeyField() != null ? schema.getUniqueKeyField().getName() : null;- if (!uniqueKeyFieldParam.equals(uniqueKeyField)) {+ if (!uniqueKeyField.equals(existingKeyField)) {// The Schema API doesn't support changing the ID field so would have to use XML directlythrow new SolrException(SolrException.ErrorCode.BAD_REQUEST,@@ -649,13 +743,12 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}boolean langsUpdated = false;- String[] languages = req.getParams().getParams(LANGUAGES_PARAM);List<String> langs;if (languages != null) {langs =- languages.length == 0 || (languages.length == 1 && "*".equals(languages[0]))+ languages.isEmpty() || (languages.size() == 1 && "*".equals(languages.getFirst()))? List.of()- : Arrays.asList(languages);+ : languages;if (!langs.equals(settings.getLanguages())) {settings.setLanguages(langs);langsUpdated = true;@@ -666,7 +759,6 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}boolean dynamicUpdated = false;- Boolean enableDynamicFields = req.getParams().getBool(ENABLE_DYNAMIC_FIELDS_PARAM);if (enableDynamicFields != null && enableDynamicFields != settings.dynamicFieldsEnabled()) {settings.setDynamicFieldsEnabled(enableDynamicFields);dynamicUpdated = true;@@ -697,7 +789,6 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {// persist the updated schemaschema.persistManagedSchema(false);- Boolean enableFieldGuessing = req.getParams().getBool(ENABLE_FIELD_GUESSING_PARAM);if (enableFieldGuessing != null && enableFieldGuessing != settings.fieldGuessingEnabled()) {settings.setFieldGuessingEnabled(enableFieldGuessing);}@@ -705,14 +796,13 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {// make sure the temp collection for this analysis existsif (!zkStateReader().getClusterState().hasCollection(mutableId)) {configSetHelper.createCollection(mutableId, mutableId);- indexedVersion.remove(mutableId);+ removeIndexedVersion(mutableId);} else {// already created in the prep step ... reload it to pull in the updated schemaCollectionAdminRequest.reloadCollection(mutableId).process(cloudClient());}// nested docs- Boolean enableNestedDocs = req.getParams().getBool(ENABLE_NESTED_DOCS_PARAM);if (enableNestedDocs != null && enableNestedDocs != settings.nestedDocsEnabled()) {settings.setNestedDocsEnabled(enableNestedDocs);configSetHelper.toggleNestedDocsFields(schema, enableNestedDocs);@@ -732,20 +822,25 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {CollectionAdminRequest.reloadCollection(mutableId).process(cloudClient());}- Map<String, Object> response =- buildResponse(configSet, loadLatestSchema(mutableId), settings, docs);- response.put("sampleSource", sampleDocuments.getSource());+ SchemaDesignerAnalyzeResponse response =+ buildSchemaDesignerResponse(+ SchemaDesignerAnalyzeResponse.class,+ configSet,+ loadLatestSchema(mutableId),+ settings,+ docs);+ response.sampleSource = sampleDocuments.getSource();if (analysisErrorHolder[0] != null) {- response.put(ANALYSIS_ERROR, analysisErrorHolder[0]);+ response.analysisError = analysisErrorHolder[0];}addErrorToResponse(mutableId, null, errorsDuringIndexing, response, null);- rsp.getValues().addAll(response);+ return response;}- @EndPoint(method = GET, path = "/schema-designer/query", permission = CONFIG_READ_PERM)- public void query(SolrQueryRequest req, SolrQueryResponse rsp)- throws IOException, SolrServerException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);+ @Override+ @PermissionName(CONFIG_READ_PERM)+ public FlexibleSolrJerseyResponse query(String configSet) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);String mutableId = getMutableId(configSet);if (!configExists(mutableId)) {throw new SolrException(@@ -757,13 +852,13 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}if (!zkStateReader().getClusterState().hasCollection(mutableId)) {- indexedVersion.remove(mutableId);+ removeIndexedVersion(mutableId);configSetHelper.createCollection(mutableId, mutableId);}// only re-index if current state of test collection is not up-to-dateint currentVersion = configSetHelper.getCurrentSchemaVersion(mutableId);- Integer version = indexedVersion.get(mutableId);+ Integer version = getIndexedVersion(mutableId);Map<Object, Throwable> errorsDuringIndexing = null;if (version == null || version != currentVersion) {log.debug(@@ -772,57 +867,83 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {version,currentVersion);List<SolrInputDocument> docs = configSetHelper.retrieveSampleDocs(configSet);- ManagedIndexSchema schema = loadLatestSchema(mutableId);- errorsDuringIndexing =- indexSampleDocsWithRebuildOnAnalysisError(- schema.getUniqueKeyField().getName(), docs, mutableId, true, null);- // the version changes when you index (due to field guessing URP)- currentVersion = configSetHelper.getCurrentSchemaVersion(mutableId);- indexedVersion.put(mutableId, currentVersion);+ if (!docs.isEmpty()) {+ ManagedIndexSchema schema = loadLatestSchema(mutableId);+ errorsDuringIndexing =+ indexSampleDocsWithRebuildOnAnalysisError(+ schema.getUniqueKeyField().getName(), docs, mutableId, true, null);+ // the version changes when you index (due to field guessing URP)+ currentVersion = configSetHelper.getCurrentSchemaVersion(mutableId);+ }+ putIndexedVersion(mutableId, currentVersion);}if (errorsDuringIndexing != null) {- Map<String, Object> response = new HashMap<>();- rsp.setException(+ // Re-indexing failed, so the temp collection may have partial or no data — running the query+ // would return misleading results. Return the indexing errors inline instead of proceeding.+ // FlexibleSolrJerseyResponse is used (rather than throwing) because the UI reads error fields+ // from the response body rather than relying on HTTP status codes.+ Map<String, Object> errorResponse = new HashMap<>();+ addErrorToResponse(+ mutableId,new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "Failed to re-index sample documents after schema updated."));- response.put(ERROR_DETAILS, errorsDuringIndexing);- rsp.getValues().addAll(response);- return;+ "Failed to re-index sample documents after schema updated."),+ errorsDuringIndexing,+ errorResponse,+ "Failed to re-index sample documents after schema updated.");+ return buildFlexibleResponse(errorResponse);}// execute the user's query against the temp collection- QueryResponse qr = cloudClient().query(mutableId, req.getParams());- rsp.getValues().addAll(qr.getResponse());+ QueryResponse qr = cloudClient().query(mutableId, solrQueryRequest.getParams());+ Map<String, Object> responseMap = new HashMap<>();+ qr.getResponse()+ .forEach(+ (name, val) -> {+ if ("response".equals(name) && val instanceof SolrDocumentList) {+ // SolrDocumentList extends ArrayList, so Jackson would serialize it as a plain+ // array, losing numFound/start metadata that the UI expects at data.response.docs+ SolrDocumentList docList = (SolrDocumentList) val;+ Map<String, Object> responseObj = new HashMap<>();+ responseObj.put("numFound", docList.getNumFound());+ responseObj.put("start", docList.getStart());+ responseObj.put("docs", new ArrayList<>(docList));+ responseMap.put(name, responseObj);+ } else {+ responseMap.put(name, val);+ }+ });+ return buildFlexibleResponse(responseMap);}/*** Return the diff of designer schema with the source schema (either previously published or the* copyFrom).*/- @EndPoint(method = GET, path = "/schema-designer/diff", permission = CONFIG_READ_PERM)- public void getSchemaDiff(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {- final String configSet = getRequiredParam(CONFIG_SET_PARAM, req);+ @Override+ @PermissionName(CONFIG_READ_PERM)+ public SchemaDesignerSchemaDiffResponse getSchemaDiff(String configSet) throws Exception {+ requireNotEmpty(CONFIG_SET_PARAM, configSet);SchemaDesignerSettings settings = getMutableSchemaForConfigSet(configSet, -1, null);// diff the published if found, else use the original source schemaString sourceSchema = configExists(configSet) ? configSet : settings.getCopyFrom();- Map<String, Object> response = new HashMap<>();- response.put(- "diff", ManagedSchemaDiff.diff(loadLatestSchema(sourceSchema), settings.getSchema()));- response.put("diff-source", sourceSchema);+ final var response = instantiateJerseyResponse(SchemaDesignerSchemaDiffResponse.class);+ response.diff = ManagedSchemaDiff.diff(loadLatestSchema(sourceSchema), settings.getSchema());+ response.diffSource = sourceSchema;addSettingsToResponse(settings, response);- rsp.getValues().addAll(response);+ return response;}- protected SampleDocuments loadSampleDocuments(SolrQueryRequest req, String configSet)- throws IOException {+ protected SampleDocuments loadSampleDocuments(String configSet) throws IOException {List<SolrInputDocument> docs = null;- ContentStream stream = extractSingleContentStream(req, false);+ ContentStream stream = extractSingleContentStream(false);SampleDocuments sampleDocs = null;if (stream != null && stream.getContentType() != null) {- sampleDocs = sampleDocLoader.parseDocsFromStream(req.getParams(), stream, MAX_SAMPLE_DOCS);+ sampleDocs =+ sampleDocLoader.parseDocsFromStream(+ solrQueryRequest.getParams(), stream, MAX_SAMPLE_DOCS);docs = sampleDocs.parsed;if (!docs.isEmpty()) {// user posted in some docs, if there are already docs stored in the blob store, then add@@ -883,7 +1004,7 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return schema;}- protected SchemaDesignerSettings getMutableSchemaForConfigSet(+ SchemaDesignerSettings getMutableSchemaForConfigSet(final String configSet, final int schemaVersion, String copyFrom) throws IOException {// The designer works with mutable config sets stored in a "temp" znode in ZK instead of the// "live" configSet@@ -963,8 +1084,8 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return configSetHelper.loadLatestSchema(configSet);}- protected ContentStream extractSingleContentStream(final SolrQueryRequest req, boolean required) {- Iterable<ContentStream> streams = req.getContentStreams();+ protected ContentStream extractSingleContentStream(boolean required) {+ Iterable<ContentStream> streams = solrQueryRequest.getContentStreams();Iterator<ContentStream> iter = streams != null ? streams.iterator() : null;ContentStream stream = iter != null && iter.hasNext() ? iter.next() : null;if (required && stream == null)@@ -1104,7 +1225,8 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return numFound;}- protected Map<String, Object> buildResponse(+ <T extends SchemaDesignerResponse> T buildSchemaDesignerResponse(+ Class<T> responseClass,String configSet,final ManagedIndexSchema schema,SchemaDesignerSettings settings,@@ -1112,52 +1234,46 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {throws IOException {String mutableId = getMutableId(configSet);int currentVersion = configSetHelper.getCurrentSchemaVersion(mutableId);- indexedVersion.put(mutableId, currentVersion);+ putIndexedVersion(mutableId, currentVersion);- // response is a map of data structures to support the schema designer- Map<String, Object> response = new HashMap<>();+ final T response = instantiateJerseyResponse(responseClass);DocCollection coll = zkStateReader().getCollection(mutableId);Collection<Slice> activeSlices = coll.getActiveSlices();if (!activeSlices.isEmpty()) {- String coreName = activeSlices.stream().findAny().orElseThrow().getLeader().getCoreName();- response.put("core", coreName);+ response.core = activeSlices.stream().findAny().orElseThrow().getLeader().getCoreName();}- response.put(UNIQUE_KEY_FIELD_PARAM, schema.getUniqueKeyField().getName());-- response.put(CONFIG_SET_PARAM, configSet);+ response.uniqueKeyField = schema.getUniqueKeyField().getName();+ response.configSet = configSet;// important: pass the designer the current schema zk version for MVCC- response.put(SCHEMA_VERSION_PARAM, currentVersion);- response.put(TEMP_COLLECTION_PARAM, mutableId);- response.put("collectionsForConfig", configSetHelper.listCollectionsForConfig(configSet));+ response.schemaVersion = currentVersion;+ response.tempCollection = mutableId;+ response.collectionsForConfig = configSetHelper.listCollectionsForConfig(configSet);// Guess at a schema for each field found in the sample docs// Collect all fields across all docs with mapping to values- response.put(- "fields",+ response.fields =schema.getFields().values().stream().map(f -> fieldToMap(f, schema)).sorted(Comparator.comparing(map -> ((String) map.get("name"))))- .collect(Collectors.toList()));+ .collect(Collectors.toList());if (settings == null) {settings = settingsDAO.getSettings(mutableId);}addSettingsToResponse(settings, response);- response.put(- "dynamicFields",+ response.dynamicFields =Arrays.stream(schema.getDynamicFieldPrototypes()).map(e -> e.getNamedPropertyValues(true)).sorted(Comparator.comparing(map -> ((String) map.get("name"))))- .collect(Collectors.toList()));+ .collect(Collectors.toList());- response.put(- "fieldTypes",+ response.fieldTypes =schema.getFieldTypes().values().stream().map(fieldType -> fieldType.getNamedPropertyValues(true)).sorted(Comparator.comparing(map -> ((String) map.get("name"))))- .collect(Collectors.toList()));+ .collect(Collectors.toList());// filesSolrZkClient zkClient = zkStateReader().getZkClient();@@ -1181,28 +1297,74 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {stripPrefix.remove(schema.getResourceName());stripPrefix.remove("lang");stripPrefix.remove(CONFIGOVERLAY_JSON); // treat this file as private+ stripPrefix.remove(INDEXED_VERSION_ZNODE); // treat this file as privateList<String> sortedFiles = new ArrayList<>(stripPrefix);Collections.sort(sortedFiles);- response.put("files", sortedFiles);+ response.files = sortedFiles;// info about the sample docsif (docs != null) {final String uniqueKeyField = schema.getUniqueKeyField().getName();- response.put(- "docIds",+ response.docIds =docs.stream().map(d -> (String) d.getFieldValue(uniqueKeyField)).filter(Objects::nonNull).limit(100)- .collect(Collectors.toList()));+ .collect(Collectors.toList());}- response.put("numDocs", docs != null ? docs.size() : -1);+ response.numDocs = docs != null ? docs.size() : -1;return response;}+ /**+ * Sets the response's name field for the schema object that was just added, based on the action+ * key from the Schema API request body. The four valid actions are pre-validated by {@link+ * SchemaDesignerConfigSetHelper#addSchemaObject}, so reaching {@code default} indicates a+ * programmer error (e.g. a new action added upstream without a corresponding case here).+ */+ private static void setSchemaObjectField(+ SchemaDesignerAddResponse response, String action, String value) {+ switch (action) {+ case "add-field" -> response.field = value;+ case "add-dynamic-field" -> response.dynamicField = value;+ case "add-field-type" -> response.fieldType = value;+ case "add-copy-field" -> {+ // Copy fields have no single "name" to surface on the response — the JS UI only checks+ // for an error and refreshes; nothing to set.+ }+ default ->+ throw new IllegalStateException(+ "Unhandled schema-designer action '"+ + action+ + "'; addSchemaObject should have rejected this upstream.");+ }+ }++ /**+ * Merges sample-document indexing errors into a response so the endpoint can return them to the+ * UI instead of throwing. Sample docs are indexed into a temp collection to drive field-type+ * inference; when that indexing fails, callers continue to build a normal response and use this+ * method to attach the error details.+ *+ * <p>Two error sources are accepted (either or both may be present):+ *+ * <ul>+ * <li>{@code solrExc} — a top-level exception (e.g. the whole indexing call failed). May be+ * {@code null}.+ * <li>{@code errorsDuringIndexing} — per-document failures. Keys are the failing document's+ * unique-id field VALUE (typed as {@code Object} because the schema's id field type is+ * unknown — typically {@code String}, but could be e.g. {@code Long}); values are the root+ * cause for that doc. May be {@code null} or empty.+ * </ul>+ *+ * <p>If both are absent, the response is left untouched. Otherwise, populates {@code updateError}+ * (message), {@code updateErrorCode} (HTTP-style code; defaults to 400), and {@code errorDetails}+ * (the per-doc map). The {@code updateError} parameter, when non-null, overrides {@code+ * solrExc.getMessage()} as the user-facing message.+ */protected void addErrorToResponse(String collection,SolrException solrExc,@@ -1230,6 +1392,75 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}}+ /**+ * Overload that writes into the typed {@link SchemaDesignerResponse}. See {@link+ * #addErrorToResponse(String, SolrException, Map, Map, String)} for full semantics.+ */+ protected void addErrorToResponse(+ String collection,+ SolrException solrExc,+ Map<Object, Throwable> errorsDuringIndexing,+ SchemaDesignerResponse response,+ String updateError) {++ if (solrExc == null && (errorsDuringIndexing == null || errorsDuringIndexing.isEmpty())) {+ return; // no errors+ }++ if (updateError != null) {+ response.updateError = updateError;+ }++ if (solrExc != null) {+ response.updateErrorCode = solrExc.code();+ if (response.updateError == null) {+ response.updateError = solrExc.getMessage();+ }+ }++ if (response.updateError == null) {+ response.updateError = "Index sample documents into " + collection + " failed!";+ }+ if (response.updateErrorCode == null) {+ response.updateErrorCode = 400;+ }+ if (errorsDuringIndexing != null) {+ response.errorDetails = errorsDuringIndexing;+ }+ }++ /**+ * Overload that writes into the typed {@link SchemaDesignerPublishResponse}. See {@link+ * #addErrorToResponse(String, SolrException, Map, Map, String)} for full semantics. Note this+ * variant has no {@code updateError} override parameter — publish callers always derive the+ * message from {@code solrExc} or the default.+ */+ protected void addErrorToResponse(+ String collection,+ SolrException solrExc,+ Map<Object, Throwable> errorsDuringIndexing,+ SchemaDesignerPublishResponse response) {++ if (solrExc == null && (errorsDuringIndexing == null || errorsDuringIndexing.isEmpty())) {+ return; // no errors+ }++ if (solrExc != null) {+ response.updateErrorCode = solrExc.code();+ response.updateError = solrExc.getMessage();+ }++ if (response.updateError == null) {+ response.updateError = "Index sample documents into " + collection + " failed!";+ }+ if (response.updateErrorCode == null) {+ response.updateErrorCode = 400;+ }+ if (errorsDuringIndexing != null) {+ response.errorDetails = errorsDuringIndexing;+ }+ }+protected SimpleOrderedMap<Object> fieldToMap(SchemaField f, ManagedIndexSchema schema) {SimpleOrderedMap<Object> map = f.getNamedPropertyValues(true);@@ -1244,8 +1475,8 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}@SuppressWarnings("unchecked")- protected Map<String, Object> readJsonFromRequest(SolrQueryRequest req) throws IOException {- ContentStream stream = extractSingleContentStream(req, true);+ protected Map<String, Object> readJsonFromRequest() throws IOException {+ ContentStream stream = extractSingleContentStream(true);String contentType = stream.getContentType();if (StrUtils.isNullOrEmpty(contentType)|| !contentType.toLowerCase(Locale.ROOT).contains(JSON_MIME)) {@@ -1258,22 +1489,18 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {return (Map<String, Object>) json;}- protected void addSettingsToResponse(- SchemaDesignerSettings settings, final Map<String, Object> response) {- response.put(LANGUAGES_PARAM, settings.getLanguages());- response.put(ENABLE_FIELD_GUESSING_PARAM, settings.fieldGuessingEnabled());- response.put(ENABLE_DYNAMIC_FIELDS_PARAM, settings.dynamicFieldsEnabled());- response.put(ENABLE_NESTED_DOCS_PARAM, settings.nestedDocsEnabled());- response.put(DISABLED, settings.isDisabled());- Optional<Integer> publishedVersion = settings.getPublishedVersion();- publishedVersion.ifPresent(version -> response.put(PUBLISHED_VERSION, version));- String copyFrom = settings.getCopyFrom();- if (copyFrom != null) {- response.put(COPY_FROM_PARAM, copyFrom);- }+ void addSettingsToResponse(+ SchemaDesignerSettings settings, final SchemaDesignerSettingsResponse response) {+ response.languages = settings.getLanguages();+ response.enableFieldGuessing = settings.fieldGuessingEnabled();+ response.enableDynamicFields = settings.dynamicFieldsEnabled();+ response.enableNestedDocs = settings.nestedDocsEnabled();+ response.disabled = settings.isDisabled();+ settings.getPublishedVersion().ifPresent(v -> response.publishedVersion = v);+ response.copyFrom = settings.getCopyFrom();}- protected String checkMutable(String configSet, SolrQueryRequest req) throws IOException {+ protected String checkMutable(String configSet, int clientSchemaVersion) throws IOException {// an apply just copies over the temp config to the "live" locationString mutableId = getMutableId(configSet);if (!configExists(mutableId)) {@@ -1288,41 +1515,41 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {final int schemaVersionInZk = configSetHelper.getCurrentSchemaVersion(mutableId);if (schemaVersionInZk != -1) {// check the versions agree- configSetHelper.checkSchemaVersion(- mutableId, requireSchemaVersionFromClient(req), schemaVersionInZk);+ configSetHelper.checkSchemaVersion(mutableId, clientSchemaVersion, schemaVersionInZk);} // else the stored is -1, can't really enforce herereturn mutableId;}- protected int requireSchemaVersionFromClient(SolrQueryRequest req) {- final int schemaVersion = req.getParams().getInt(SCHEMA_VERSION_PARAM, -1);- if (schemaVersion == -1) {+ protected void requireSchemaVersion(Integer schemaVersion) {+ ensureRequiredParameterProvided(SCHEMA_VERSION_PARAM, schemaVersion);+ if (schemaVersion < 0) {throw new SolrException(- SolrException.ErrorCode.BAD_REQUEST,- SCHEMA_VERSION_PARAM + " is a required parameter for the " + req.getPath() + " endpoint");+ SolrException.ErrorCode.BAD_REQUEST, SCHEMA_VERSION_PARAM + " is a required parameter!");}- return schemaVersion;}- protected String getRequiredParam(final String param, final SolrQueryRequest req) {- final String paramValue = req.getParams().get(param);- if (StrUtils.isNullOrEmpty(paramValue)) {+ protected void requireNotEmpty(final String param, final String value) {+ ensureRequiredParameterProvided(param, value);+ if (value.isEmpty()) {throw new SolrException(- SolrException.ErrorCode.BAD_REQUEST,- param + " is a required parameter for the " + req.getPath() + " endpoint!");+ SolrException.ErrorCode.BAD_REQUEST, param + " is a required parameter!");}- return paramValue;}- protected void cleanupTemp(String configSet) throws IOException, SolrServerException {+ protected void doCleanupTemp(String configSet) throws IOException, SolrServerException {String mutableId = getMutableId(configSet);- indexedVersion.remove(mutableId);CollectionAdminRequest.deleteCollection(mutableId).process(cloudClient());configSetHelper.deleteStoredSampleDocs(configSet);deleteConfig(mutableId);}+ protected FlexibleSolrJerseyResponse buildFlexibleResponse(Map<String, Object> responseMap) {+ final var response = instantiateJerseyResponse(FlexibleSolrJerseyResponse.class);+ responseMap.forEach(response::setUnknownProperty);+ return response;+ }+private boolean configExists(String configSet) throws IOException {return coreContainer.getConfigSetService().checkConfigExists(configSet);}diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.javaindex 1af273cc9d0..72d8c3bb99b 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java@@ -20,24 +20,18 @@ package org.apache.solr.handler.designer;import static org.apache.solr.common.params.CommonParams.VERSION_FIELD;import static org.apache.solr.common.util.Utils.toJavabin;import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;-import static org.apache.solr.handler.designer.SchemaDesignerAPI.getConfigSetZkPath;-import static org.apache.solr.handler.designer.SchemaDesignerAPI.getMutableId;+import static org.apache.solr.handler.designer.SchemaDesigner.getConfigSetZkPath;+import static org.apache.solr.handler.designer.SchemaDesigner.getMutableId;import static org.apache.solr.schema.IndexSchema.NEST_PATH_FIELD_NAME;import static org.apache.solr.schema.IndexSchema.ROOT_FIELD_NAME;import static org.apache.solr.schema.ManagedIndexSchemaFactory.DEFAULT_MANAGED_SCHEMA_RESOURCE_NAME;-import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.lang.invoke.MethodHandles;import java.nio.ByteBuffer;import java.nio.charset.StandardCharsets;-import java.nio.file.FileVisitResult;-import java.nio.file.Files;-import java.nio.file.Path;-import java.nio.file.SimpleFileVisitor;-import java.nio.file.attribute.BasicFileAttributes;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;@@ -46,16 +40,11 @@ import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Objects;-import java.util.Optional;import java.util.Set;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;import java.util.concurrent.atomic.AtomicReference;import java.util.stream.Collectors;-import java.util.zip.ZipEntry;-import java.util.zip.ZipOutputStream;-import org.apache.commons.io.FilenameUtils;-import org.apache.commons.io.file.PathUtils;import org.apache.lucene.util.IOSupplier;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrResponse;@@ -73,7 +62,6 @@ import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.cloud.DocCollection;-import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.cloud.ZkMaintenanceUtils;import org.apache.solr.common.cloud.ZkStateReader;@@ -382,7 +370,7 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {}}- // detect if they're trying to copy multi-valued fields into a single-valued field+ // detect if they're trying to copy multivalued fields into a single-valued fieldObject multiValued = diff.get(MULTIVALUED);if (multiValued == null) {// mv not overridden explicitly, but we need the actual value, which will come from the new@@ -403,7 +391,7 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {name,src);multiValued = Boolean.TRUE;- diff.put(MULTIVALUED, multiValued);+ diff.put(MULTIVALUED, true);break;}}@@ -414,8 +402,8 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {validateMultiValuedChange(configSet, schemaField, Boolean.FALSE);}- // switch from single-valued to multi-valued requires a full rebuild- // See SOLR-12185 ... if we're switching from single to multi-valued, then it's a big operation+ // switch from single-valued to multivalued requires a full rebuild+ // See SOLR-12185 ... if we're switching from single to multivalued, then it's a big operationif (fieldHasMultiValuedChange(multiValued, schemaField)) {needsRebuild = true;log.warn(@@ -535,29 +523,6 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {}}- private String getBaseUrl(final String collection) {- String baseUrl = null;- try {- Set<String> liveNodes = zkStateReader().getClusterState().getLiveNodes();- DocCollection docColl = zkStateReader().getCollection(collection);- if (docColl != null && !liveNodes.isEmpty()) {- Optional<Replica> maybeActive =- docColl.getReplicas().stream().filter(r -> r.isActive(liveNodes)).findAny();- if (maybeActive.isPresent()) {- baseUrl = maybeActive.get().getBaseUrl();- }- }- } catch (Exception exc) {- log.warn("Failed to lookup base URL for collection {}", collection, exc);- }-- if (baseUrl == null) {- baseUrl = zkStateReader().getBaseUrlForNodeName(cc.getZkController().getNodeName());- }-- return baseUrl;- }-protected String getManagedSchemaZkPath(final String configSet) {return getConfigSetZkPath(configSet, DEFAULT_MANAGED_SCHEMA_RESOURCE_NAME);}@@ -706,7 +671,7 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {continue; // cannot copy to self}- // make sure the field exists and is multi-valued if this field is+ // make sure the field exists and is multivalued if this field isSchemaField toAddField = schema.getFieldOrNull(toAdd);if (toAddField != null) {if (!field.multiValued() || toAddField.multiValued()) {@@ -816,8 +781,8 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {final Set<String> toRemove =types.values().stream().filter(this::isTextType)- .filter(t -> !languages.contains(t.getTypeName().substring(TEXT_PREFIX_LEN))).map(FieldType::getTypeName)+ .filter(typeName -> !languages.contains(typeName.substring(TEXT_PREFIX_LEN))).filter(t -> !usedTypes.contains(t)) // not explicitly used by a field.collect(Collectors.toSet());@@ -960,9 +925,9 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {List<SchemaField> addDynFields =Arrays.stream(copyFromSchema.getDynamicFields())- .filter(df -> langFieldTypeNames.contains(df.getPrototype().getType().getTypeName()))- .filter(df -> !existingDynFields.contains(df.getPrototype().getName())).map(IndexSchema.DynamicField::getPrototype)+ .filter(prototype -> langFieldTypeNames.contains(prototype.getType().getTypeName()))+ .filter(prototype -> !existingDynFields.contains(prototype.getName())).collect(Collectors.toList());if (!addDynFields.isEmpty()) {schema = schema.addDynamicFields(addDynFields, null, false);@@ -1034,8 +999,8 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {.collect(Collectors.toSet());List<SchemaField> toAdd =Arrays.stream(dynamicFields)- .filter(df -> !existingDFNames.contains(df.getPrototype().getName())).map(IndexSchema.DynamicField::getPrototype)+ .filter(prototype -> !existingDFNames.contains(prototype.getName())).collect(Collectors.toList());// only restore language specific dynamic fields that match our langSet@@ -1099,52 +1064,6 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {return cc.getConfigSetService().listConfigs();}- byte[] downloadAndZipConfigSet(String configId) throws IOException {- ByteArrayOutputStream baos = new ByteArrayOutputStream();- Path tmpDirectory =- Files.createTempDirectory("schema-designer-" + FilenameUtils.getName(configId));- try {- cc.getConfigSetService().downloadConfig(configId, tmpDirectory);- try (ZipOutputStream zipOut = new ZipOutputStream(baos)) {- Files.walkFileTree(- tmpDirectory,- new SimpleFileVisitor<>() {- @Override- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)- throws IOException {- if (Files.isHidden(dir)) {- return FileVisitResult.SKIP_SUBTREE;- }-- String dirName = tmpDirectory.relativize(dir).toString();- if (!dirName.endsWith("/")) {- dirName += "/";- }- zipOut.putNextEntry(new ZipEntry(dirName));- zipOut.closeEntry();- return FileVisitResult.CONTINUE;- }-- @Override- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)- throws IOException {- if (!Files.isHidden(file)) {- try (InputStream fis = Files.newInputStream(file)) {- ZipEntry zipEntry = new ZipEntry(tmpDirectory.relativize(file).toString());- zipOut.putNextEntry(zipEntry);- fis.transferTo(zipOut);- }- }- return FileVisitResult.CONTINUE;- }- });- }- } finally {- PathUtils.deleteDirectory(tmpDirectory);- }- return baos.toByteArray();- }-protected ZkSolrResourceLoader zkLoaderForConfigSet(final String configSet) {SolrResourceLoader loader = cc.getResourceLoader();return new ZkSolrResourceLoader(diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConstants.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConstants.javaindex 0ad93d90d27..e3cd103e3aa 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConstants.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConstants.java@@ -21,18 +21,13 @@ public interface SchemaDesignerConstants {String CONFIG_SET_PARAM = "configSet";String COPY_FROM_PARAM = "copyFrom";String SCHEMA_VERSION_PARAM = "schemaVersion";- String RELOAD_COLLECTIONS_PARAM = "reloadCollections";- String INDEX_TO_COLLECTION_PARAM = "indexToCollection";String NEW_COLLECTION_PARAM = "newCollection";- String CLEANUP_TEMP_PARAM = "cleanupTemp";String ENABLE_DYNAMIC_FIELDS_PARAM = "enableDynamicFields";String ENABLE_FIELD_GUESSING_PARAM = "enableFieldGuessing";String ENABLE_NESTED_DOCS_PARAM = "enableNestedDocs";String TEMP_COLLECTION_PARAM = "tempCollection";String PUBLISHED_VERSION = "publishedVersion";- String DISABLE_DESIGNER_PARAM = "disableDesigner";String DISABLED = "disabled";- String DOC_ID_PARAM = "docId";String FIELD_PARAM = "field";String UNIQUE_KEY_FIELD_PARAM = "uniqueKeyField";String AUTO_CREATE_FIELDS = "update.autoCreateFields";@@ -40,6 +35,8 @@ public interface SchemaDesignerConstants {String DESIGNER_KEY = "_designer.";String LANGUAGES_PARAM = "languages";String CONFIGOVERLAY_JSON = "configoverlay.json";+ // Separate from configoverlay.json so writes here don't trigger a collection reload.+ String INDEXED_VERSION_ZNODE = "_designer_indexed_version";String UPDATE_ERROR = "updateError";String ANALYSIS_ERROR = "analysisError";String ERROR_DETAILS = "errorDetails";diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerSettingsDAO.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerSettingsDAO.javaindex 9a09e8e5da9..939a8900497 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerSettingsDAO.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerSettingsDAO.java@@ -17,7 +17,7 @@package org.apache.solr.handler.designer;-import static org.apache.solr.handler.designer.SchemaDesignerAPI.getConfigSetZkPath;+import static org.apache.solr.handler.designer.SchemaDesigner.getConfigSetZkPath;import java.io.IOException;import java.lang.invoke.MethodHandles;diff --git a/solr/core/src/java/org/apache/solr/handler/designer/package-info.java b/solr/core/src/java/org/apache/solr/handler/designer/package-info.javaindex 17e3b7af276..c8516c1c327 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/package-info.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/package-info.java@@ -20,5 +20,5 @@* limitations under the License.*/-/** The {@link org.apache.solr.handler.designer.SchemaDesignerAPI} and supporting classes. */+/** The {@link org.apache.solr.handler.designer.SchemaDesigner} and supporting classes. */package org.apache.solr.handler.designer;diff --git a/solr/core/src/test/org/apache/solr/handler/configsets/DownloadConfigSetAPITest.java b/solr/core/src/test/org/apache/solr/handler/configsets/DownloadConfigSetAPITest.javaindex 10325b278ee..31bfd1e97d8 100644--- a/solr/core/src/test/org/apache/solr/handler/configsets/DownloadConfigSetAPITest.java+++ b/solr/core/src/test/org/apache/solr/handler/configsets/DownloadConfigSetAPITest.java@@ -89,6 +89,7 @@ public class DownloadConfigSetAPITest extends SolrTestCase {try (final Response response = api.downloadConfigSet("myconfig")) {assertEquals(200, response.getStatus());assertEquals("application/zip", response.getMediaType().toString());+ assertNull(response.getHeaderString("Content-Disposition"));}}}diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesigner.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesigner.javanew file mode 100644index 00000000000..d1677f75235--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesigner.java@@ -0,0 +1,941 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.handler.designer;++import static org.apache.solr.common.params.CommonParams.JSON_MIME;+import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;+import static org.apache.solr.handler.designer.SchemaDesigner.getMutableId;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++import java.io.ByteArrayInputStream;+import java.io.IOException;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.Arrays;+import java.util.Collection;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.concurrent.atomic.AtomicInteger;+import java.util.stream.Stream;+import org.apache.solr.client.api.model.FlexibleSolrJerseyResponse;+import org.apache.solr.client.api.model.ListCollectionsResponse;+import org.apache.solr.client.api.model.SchemaDesignerAddRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerAddResponse;+import org.apache.solr.client.api.model.SchemaDesignerFileContentsResponse;+import org.apache.solr.client.api.model.SchemaDesignerInfoResponse;+import org.apache.solr.client.api.model.SchemaDesignerResponse;+import org.apache.solr.client.api.model.SchemaDesignerSchemaDiffResponse;+import org.apache.solr.client.api.model.SchemaDesignerUpdateRequestBody;+import org.apache.solr.client.api.model.SchemaDesignerUpdateResponse;+import org.apache.solr.client.solrj.SolrServerException;+import org.apache.solr.client.solrj.request.SolrQuery;+import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.cloud.SolrCloudTestCase;+import org.apache.solr.common.SolrException;+import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.common.cloud.SolrZkClient;+import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.common.util.ContentStream;+import org.apache.solr.common.util.ContentStreamBase;+import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.core.CoreContainer;+import org.apache.solr.handler.TestSampleDocumentsLoader;+import org.apache.solr.jersey.SolrJacksonMapper;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.schema.ManagedIndexSchema;+import org.apache.solr.schema.SchemaField;+import org.apache.solr.util.ExternalPaths;+import org.apache.zookeeper.KeeperException;+import org.junit.AfterClass;+import org.junit.Before;+import org.junit.BeforeClass;+import org.junit.Test;++public class TestSchemaDesigner extends SolrCloudTestCase implements SchemaDesignerConstants {++ private CoreContainer cc;+ private SchemaDesigner schemaDesigner;+ private SolrQueryRequest mockReq;++ @BeforeClass+ public static void createCluster() throws Exception {+ System.setProperty("managed.schema.mutable", "true");+ configureCluster(1)+ .addConfig(DEFAULT_CONFIGSET_NAME, ExternalPaths.DEFAULT_CONFIGSET)+ .configure();+ }++ @AfterClass+ public static void tearDownCluster() throws Exception {+ if (cluster != null && cluster.getSolrClient() != null) {+ cluster.deleteAllCollections();+ cluster.deleteAllConfigSets();+ }+ }++ @Before+ public void setupTest() {+ assumeWorkingMockito();+ assertNotNull(cluster);+ cc = cluster.getJettySolrRunner(0).getCoreContainer();+ assertNotNull(cc);+ mockReq = mock(SolrQueryRequest.class);+ schemaDesigner =+ new SchemaDesigner(+ cc,+ SchemaDesigner.newSchemaSuggester(),+ SchemaDesigner.newSampleDocumentsLoader(),+ mockReq);+ }++ // indexedVersion lives in ZK now, so invalidate it by deleting the znode directly.+ private void invalidateIndexedVersionCache(String mutableId) throws Exception {+ SolrZkClient zkClient = cc.getZkController().getZkClient();+ String path = SchemaDesigner.getConfigSetZkPath(mutableId, INDEXED_VERSION_ZNODE);+ try {+ zkClient.delete(path, -1);+ } catch (KeeperException.NoNodeException ignored) {+ // already gone, nothing to do+ }+ }++ public void testTSV() throws Exception {+ String configSet = "testTSV";++ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ reqParams.set(LANGUAGES_PARAM, "en");+ reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, false);+ when(mockReq.getParams()).thenReturn(reqParams);++ String tsv = "id\tcol1\tcol2\n1\tfoo\tbar\n2\tbaz\tbah\n";++ // POST some sample TSV docs+ ContentStream stream = new ContentStreamBase.StringStream(tsv, "text/csv");+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));++ // POST /schema-designer/analyze+ SchemaDesignerResponse response =+ schemaDesigner.analyze(configSet, null, null, null, List.of("en"), false, null, null);+ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ assertEquals(Integer.valueOf(2), response.numDocs);++ reqParams.clear();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getContentStreams()).thenReturn(null);+ schemaDesigner.cleanupTempSchema(configSet);++ String mutableId = getMutableId(configSet);+ assertFalse(cc.getZkController().getClusterState().hasCollection(mutableId));+ SolrZkClient zkClient = cc.getZkController().getZkClient();+ assertFalse(zkClient.exists("/configs/" + mutableId));+ }++ @Test+ @SuppressWarnings("unchecked")+ public void testAddTechproductsProgressively() throws Exception {+ Path docsDir = ExternalPaths.SOURCE_HOME.resolve("example/exampledocs");+ assertTrue(docsDir + " not found!", Files.isDirectory(docsDir));+ List<Path> toAdd;+ try (Stream<Path> files = Files.list(docsDir)) {+ toAdd =+ files+ .filter(+ (dir) -> {+ String name = dir.getFileName().toString();+ return name.endsWith(".xml")+ || name.endsWith(".json")+ || name.endsWith(".csv")+ || name.endsWith(".jsonl");+ })+ .toList();+ assertNotNull("No test data files found in " + docsDir, toAdd);+ }++ String configSet = "techproducts";++ // GET /schema-designer/{configSet}+ SchemaDesignerInfoResponse infoResponse = schemaDesigner.getInfo(configSet);+ // response should just be the default values+ Map<String, Object> expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, true,+ ENABLE_FIELD_GUESSING_PARAM, true,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, List.of());+ assertDesignerSettings(expSettings, infoResponse);+ int schemaVersion = infoResponse.schemaVersion;+ assertEquals(schemaVersion, -1); // shouldn't exist yet++ // Use the prep endpoint to prepare the new schema+ SchemaDesignerResponse response = schemaDesigner.prepNewSchema(configSet, null);+ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ schemaVersion = response.schemaVersion;++ for (Path next : toAdd) {+ // Analyze some sample documents to refine the schema+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);++ // POST some sample JSON docs+ ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(next);+ stream.setContentType(+ TestSampleDocumentsLoader.guessContentTypeFromFilename(next.getFileName().toString()));+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));++ // POST /schema-designer/analyze+ response =+ schemaDesigner.analyze(+ configSet, schemaVersion, null, null, List.of("en"), false, null, null);++ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ assertNotNull(response.fields);+ assertNotNull(response.fieldTypes);+ assertNotNull(response.docIds);++ // capture the schema version for MVCC+ schemaVersion = response.schemaVersion;+ }++ // get info (from the temp)+ // GET /schema-designer/{configSet}+ infoResponse = schemaDesigner.getInfo(configSet);+ expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, false,+ ENABLE_FIELD_GUESSING_PARAM, true,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, List.of("en"),+ COPY_FROM_PARAM, "_default");+ assertDesignerSettings(expSettings, infoResponse);++ // query to see how the schema decisions impact retrieval / ranking+ ModifiableSolrParams queryParams = new ModifiableSolrParams();+ queryParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));+ queryParams.set(CONFIG_SET_PARAM, configSet);+ queryParams.set(CommonParams.Q, "*:*");+ when(mockReq.getParams()).thenReturn(queryParams);+ when(mockReq.getContentStreams()).thenReturn(null);++ // GET /schema-designer/query+ FlexibleSolrJerseyResponse queryResp = schemaDesigner.query(configSet);+ assertNotNull(queryResp.unknownProperties().get("responseHeader"));+ @SuppressWarnings("unchecked")+ Map<String, Object> queryResponse =+ (Map<String, Object>) queryResp.unknownProperties().get("response");+ assertNotNull("response object must be a map with numFound/docs", queryResponse);+ assertEquals(47L, queryResponse.get("numFound"));+ @SuppressWarnings("unchecked")+ List<Object> queryDocs = (List<Object>) queryResponse.get("docs");+ assertNotNull("response.docs must be a list", queryDocs);+ assertFalse("response.docs must be non-empty", queryDocs.isEmpty());++ // publish schema to a config set that can be used by real collections+ String collection = "techproducts";+ schemaDesigner.publish(configSet, schemaVersion, collection, true, 1, 1, true, true, true);+ assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));++ // listCollectionsForConfig+ ListCollectionsResponse collectionsResp = schemaDesigner.listCollectionsForConfig(configSet);+ List<String> collections = collectionsResp.collections;+ assertNotNull(collections);+ assertTrue(collections.contains(collection));++ // now try to create another temp, which should fail since designer is disabled for this+ // configSet now+ try {+ schemaDesigner.prepNewSchema(configSet, null);+ fail("Prep should fail for locked schema " + configSet);+ } catch (SolrException solrExc) {+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrExc.code());+ }+ }++ @Test+ @SuppressWarnings("unchecked")+ public void testSuggestFilmsXml() throws Exception {+ String configSet = "films";++ Path filmsDir = ExternalPaths.SOURCE_HOME.resolve("example/films");+ assertTrue(filmsDir + " not found!", Files.isDirectory(filmsDir));+ Path filmsXml = filmsDir.resolve("films.xml");+ assertTrue("example/films/films.xml not found", Files.isRegularFile(filmsXml));++ // POST some sample XML docs+ ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(filmsXml);+ stream.setContentType("application/xml");+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));++ // POST /schema-designer/analyze+ SchemaDesignerResponse response =+ schemaDesigner.analyze(configSet, null, null, null, null, true, null, null);++ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ assertNotNull(response.fields);+ assertNotNull(response.fieldTypes);+ List<String> docIds = response.docIds;+ assertNotNull(docIds);+ assertEquals(100, docIds.size()); // designer limits the doc ids to top 100++ String idField = response.uniqueKeyField;+ assertNotNull(idField);+ }++ @Test+ @SuppressWarnings("unchecked")+ public void testBasicUserWorkflow() throws Exception {+ String configSet = "testJson";++ // Use the prep endpoint to prepare the new schema+ SchemaDesignerResponse response = schemaDesigner.prepNewSchema(configSet, null);+ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);++ Map<String, Object> expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, true,+ ENABLE_FIELD_GUESSING_PARAM, true,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, List.of(),+ COPY_FROM_PARAM, "_default");+ assertDesignerSettings(expSettings, response);++ // Analyze some sample documents to refine the schema+ Path booksJson = ExternalPaths.SOURCE_HOME.resolve("example/exampledocs/books.json");+ ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(booksJson);+ stream.setContentType(JSON_MIME);+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));++ // POST /schema-designer/analyze+ response = schemaDesigner.analyze(configSet, null, null, null, null, null, null, null);++ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ assertNotNull(response.fields);+ assertNotNull(response.fieldTypes);+ assertNotNull(response.docIds);+ String idField = response.uniqueKeyField;+ assertNotNull(idField);+ assertDesignerSettings(expSettings, response);++ // capture the schema version for MVCC+ int schemaVersion = response.schemaVersion;++ // load the contents of a file+ Collection<String> files = response.files;+ assertTrue(files != null && !files.isEmpty());++ String file = null;+ for (String f : files) {+ if ("solrconfig.xml".equals(f)) {+ file = f;+ break;+ }+ }+ assertNotNull("solrconfig.xml not found in files!", file);+ byte[] solrconfigBytes =+ cc.getConfigSetService().downloadFileFromConfig(getMutableId(configSet), file);+ assertNotNull(solrconfigBytes);+ String solrconfigXml = new String(solrconfigBytes, StandardCharsets.UTF_8);++ // Update solrconfig.xml+ SchemaDesignerFileContentsResponse fileContentsResponse =+ schemaDesigner.updateFileContents(+ configSet,+ file,+ new ByteArrayInputStream(solrconfigXml.getBytes(StandardCharsets.UTF_8)));+ schemaVersion = fileContentsResponse.schemaVersion;++ // this should fail b/c the updated solrconfig.xml is invalid+ fileContentsResponse =+ schemaDesigner.updateFileContents(+ configSet,+ file,+ new ByteArrayInputStream("<config/>".getBytes(StandardCharsets.UTF_8)));+ assertNotNull(fileContentsResponse.updateFileError);++ // remove dynamic fields and change the language to "en" only+ when(mockReq.getContentStreams()).thenReturn(null);+ // POST /schema-designer/analyze+ response =+ schemaDesigner.analyze(+ configSet, schemaVersion, null, null, List.of("en"), false, false, null);++ expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, false,+ ENABLE_FIELD_GUESSING_PARAM, false,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, List.of("en"),+ COPY_FROM_PARAM, "_default");+ assertDesignerSettings(expSettings, response);++ List<String> filesInResp = response.files;+ assertEquals(5, filesInResp.size());+ assertTrue(filesInResp.contains("lang/stopwords_en.txt"));++ schemaVersion = response.schemaVersion;++ // add the dynamic fields back and change the languages too+ response =+ schemaDesigner.analyze(+ configSet, schemaVersion, null, null, Arrays.asList("en", "fr"), true, false, null);++ expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, true,+ ENABLE_FIELD_GUESSING_PARAM, false,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, Arrays.asList("en", "fr"),+ COPY_FROM_PARAM, "_default");+ assertDesignerSettings(expSettings, response);++ filesInResp = response.files;+ assertEquals(7, filesInResp.size());+ assertTrue(filesInResp.contains("lang/stopwords_fr.txt"));++ schemaVersion = response.schemaVersion;++ // add back all the default languages (using "*" wildcard -> empty list)+ response =+ schemaDesigner.analyze(+ configSet, schemaVersion, null, null, List.of("*"), false, null, null);++ expSettings =+ Map.of(+ ENABLE_DYNAMIC_FIELDS_PARAM, false,+ ENABLE_FIELD_GUESSING_PARAM, false,+ ENABLE_NESTED_DOCS_PARAM, false,+ LANGUAGES_PARAM, List.of(),+ COPY_FROM_PARAM, "_default");+ assertDesignerSettings(expSettings, response);++ filesInResp = response.files;+ assertEquals(43, filesInResp.size());+ assertTrue(filesInResp.contains("lang/stopwords_fr.txt"));+ assertTrue(filesInResp.contains("lang/stopwords_en.txt"));+ assertTrue(filesInResp.contains("lang/stopwords_it.txt"));++ schemaVersion = response.schemaVersion;++ // Get the value of a sample document+ String docId = "978-0641723445";+ String fieldName = "series_t";++ // GET /schema-designer/sample+ FlexibleSolrJerseyResponse sampleResp =+ schemaDesigner.getSampleValue(configSet, fieldName, idField, docId);+ assertNotNull(sampleResp.unknownProperties().get(idField));+ assertNotNull(sampleResp.unknownProperties().get(fieldName));+ assertNotNull(sampleResp.unknownProperties().get("analysis"));++ // at this point the user would refine the schema by+ // editing suggestions for fields and adding/removing fields / field types as needed++ // add a new field+ // POST /schema-designer/{configSet}+ SchemaDesignerAddResponse addResponse =+ schemaDesigner.addSchemaObject(+ configSet, schemaVersion, loadAddBody("schema-designer/add-new-field.json"));+ assertNotNull(addResponse.field);+ schemaVersion = addResponse.schemaVersion;+ assertNotNull(addResponse.fields);++ // update an existing field+ // switch a single-valued field to a multivalued field, which triggers a full rebuild of the+ // "temp" collection+ // PUT /schema-designer/{configSet}+ SchemaDesignerUpdateResponse updateResponse =+ schemaDesigner.updateSchemaObject(+ configSet, schemaVersion, loadUpdateBody("schema-designer/update-author-field.json"));+ assertNotNull(updateResponse.field);+ schemaVersion = updateResponse.schemaVersion;++ // add a new type+ // POST /schema-designer/{configSet}+ addResponse =+ schemaDesigner.addSchemaObject(+ configSet, schemaVersion, loadAddBody("schema-designer/add-new-type.json"));+ final String expectedTypeName = "test_txt";+ assertEquals(expectedTypeName, addResponse.fieldType);+ schemaVersion = addResponse.schemaVersion;+ assertNotNull(addResponse.fieldTypes);+ @SuppressWarnings("unchecked")+ List<Map<String, Object>> fieldTypes = addResponse.fieldTypes;+ Optional<Map<String, Object>> expected =+ fieldTypes.stream().filter(m -> expectedTypeName.equals(m.get("name"))).findFirst();+ assertTrue(+ "New field type '" + expectedTypeName + "' not found in add type response!",+ expected.isPresent());++ // POST /schema-designer/update+ updateResponse =+ schemaDesigner.updateSchemaObject(+ configSet, schemaVersion, loadUpdateBody("schema-designer/update-type.json"));+ schemaVersion = updateResponse.schemaVersion;++ // query to see how the schema decisions impact retrieval / ranking+ ModifiableSolrParams queryParams = new ModifiableSolrParams();+ queryParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));+ queryParams.set(CONFIG_SET_PARAM, configSet);+ queryParams.set(CommonParams.Q, "*:*");+ when(mockReq.getParams()).thenReturn(queryParams);+ when(mockReq.getContentStreams()).thenReturn(null);++ // GET /schema-designer/query+ FlexibleSolrJerseyResponse queryResp2 = schemaDesigner.query(configSet);+ assertNotNull(queryResp2.unknownProperties().get("responseHeader"));+ @SuppressWarnings("unchecked")+ Map<String, Object> queryResponse2 =+ (Map<String, Object>) queryResp2.unknownProperties().get("response");+ assertNotNull("response object must be a map with numFound/docs", queryResponse2);+ @SuppressWarnings("unchecked")+ List<Object> queryDocs2 = (List<Object>) queryResponse2.get("docs");+ assertEquals(4, queryDocs2.size());++ // publish schema to a config set that can be used by real collections+ String collection = "test123";+ schemaDesigner.publish(configSet, schemaVersion, collection, true, 1, 1, true, true, false);++ assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));++ // listCollectionsForConfig+ ListCollectionsResponse collectionsResp2 = schemaDesigner.listCollectionsForConfig(configSet);+ List<String> collections = collectionsResp2.collections;+ assertNotNull(collections);+ assertTrue(collections.contains(collection));++ // verify temp designer objects were cleaned up during the publish operation ...+ String mutableId = getMutableId(configSet);+ assertFalse(cc.getZkController().getClusterState().hasCollection(mutableId));+ SolrZkClient zkClient = cc.getZkController().getZkClient();+ assertFalse(zkClient.exists("/configs/" + mutableId));++ SolrQuery query = new SolrQuery("*:*");+ query.setRows(0);+ QueryResponse qr = cluster.getSolrClient().query(collection, query);+ // this proves the docs were stored in the filestore too+ assertEquals(4, qr.getResults().getNumFound());+ }++ @SuppressWarnings("unchecked")+ public void testFieldUpdates() throws Exception {+ String configSet = "fieldUpdates";++ // Use the prep endpoint to prepare the new schema+ SchemaDesignerResponse response = schemaDesigner.prepNewSchema(configSet, null);+ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ int schemaVersion = response.schemaVersion;++ // add our test field that we'll test various updates to+ // POST /schema-designer/{configSet}+ SchemaDesignerAddResponse addResponse =+ schemaDesigner.addSchemaObject(+ configSet, schemaVersion, loadAddBody("schema-designer/add-new-field.json"));+ assertNotNull(addResponse.field);++ final String fieldName = "keywords";++ Optional<Map<String, Object>> maybeField =+ addResponse.fields.stream().filter(m -> fieldName.equals(m.get("name"))).findFirst();+ assertTrue(maybeField.isPresent());+ Map<String, Object> field = maybeField.get();+ assertEquals(Boolean.FALSE, field.get("indexed"));+ assertEquals(Boolean.FALSE, field.get("required"));+ assertEquals(Boolean.TRUE, field.get("stored"));+ assertEquals(Boolean.TRUE, field.get("docValues"));+ assertEquals(Boolean.TRUE, field.get("useDocValuesAsStored"));+ assertEquals(Boolean.FALSE, field.get("multiValued"));+ assertEquals("string", field.get("type"));++ String mutableId = getMutableId(configSet);+ SchemaDesignerConfigSetHelper configSetHelper =+ new SchemaDesignerConfigSetHelper(cc, SchemaDesigner.newSchemaSuggester());+ ManagedIndexSchema schema = schemaDesigner.loadLatestSchema(mutableId);++ // make it required+ Map<String, Object> updateField =+ Map.of("name", fieldName, "type", field.get("type"), "required", true);+ configSetHelper.updateField(configSet, updateField, schema);++ schema = schemaDesigner.loadLatestSchema(mutableId);+ SchemaField schemaField = schema.getField(fieldName);+ assertTrue(schemaField.isRequired());++ updateField =+ Map.of("name", fieldName, "type", field.get("type"), "required", false, "stored", false);+ configSetHelper.updateField(configSet, updateField, schema);+ schema = schemaDesigner.loadLatestSchema(mutableId);+ schemaField = schema.getField(fieldName);+ assertFalse(schemaField.isRequired());+ assertFalse(schemaField.stored());++ updateField =+ Map.of(+ "name",+ fieldName,+ "type",+ field.get("type"),+ "required",+ false,+ "stored",+ false,+ "multiValued",+ true);+ configSetHelper.updateField(configSet, updateField, schema);+ schema = schemaDesigner.loadLatestSchema(mutableId);+ schemaField = schema.getField(fieldName);+ assertFalse(schemaField.isRequired());+ assertFalse(schemaField.stored());+ assertTrue(schemaField.multiValued());++ updateField = Map.of("name", fieldName, "type", "strings", "copyDest", "_text_");+ configSetHelper.updateField(configSet, updateField, schema);+ schema = schemaDesigner.loadLatestSchema(mutableId);+ schemaField = schema.getField(fieldName);+ assertTrue(schemaField.multiValued());+ assertEquals("strings", schemaField.getType().getTypeName());+ assertFalse(schemaField.isRequired());+ assertTrue(schemaField.stored());+ List<String> srcFields = schema.getCopySources("_text_");+ assertEquals(List.of(fieldName), srcFields);+ }++ @SuppressWarnings({"unchecked"})+ public void testSchemaDiffEndpoint() throws Exception {+ String configSet = "testDiff";++ // Use the prep endpoint to prepare the new schema+ SchemaDesignerResponse response = schemaDesigner.prepNewSchema(configSet, null);+ assertNotNull(response.configSet);+ assertNotNull(response.schemaVersion);+ int schemaVersion = response.schemaVersion;++ // publish schema to a config set that can be used by real collections+ String collection = "diff456";+ schemaDesigner.publish(configSet, schemaVersion, collection, true, 1, 1, true, true, false);++ assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));++ // Load the schema designer for the existing config set and make some changes to it+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ when(mockReq.getContentStreams()).thenReturn(null);+ response = schemaDesigner.analyze(configSet, null, null, null, null, true, false, null);++ // Update id field to not use docValues+ @SuppressWarnings("unchecked")+ List<SimpleOrderedMap<Object>> fields =+ (List<SimpleOrderedMap<Object>>) (List<?>) response.fields;+ SimpleOrderedMap<Object> idFieldMap =+ fields.stream().filter(field -> field.get("name").equals("id")).findFirst().get();+ idFieldMap.remove("copyDest"); // Don't include copyDest as it is not a property of SchemaField+ SimpleOrderedMap<Object> idFieldMapUpdated = idFieldMap.clone();+ idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("docValues", 0), Boolean.FALSE);+ idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("useDocValuesAsStored", 0), Boolean.FALSE);+ idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("uninvertible", 0), Boolean.TRUE);+ idFieldMapUpdated.setVal(+ idFieldMapUpdated.indexOf("omitTermFreqAndPositions", 0), Boolean.FALSE);++ Map<String, Object> mapParams = new SimpleOrderedMap<>(idFieldMapUpdated.toSolrParams());+ mapParams.put("termVectors", Boolean.FALSE);+ schemaVersion = response.schemaVersion;++ SchemaDesignerUpdateRequestBody idFieldUpdate =+ SolrJacksonMapper.getObjectMapper()+ .convertValue(mapParams, SchemaDesignerUpdateRequestBody.class);+ response = schemaDesigner.updateSchemaObject(configSet, schemaVersion, idFieldUpdate);++ // Add a new field+ schemaVersion = response.schemaVersion;+ // POST /schema-designer/{configSet}+ SchemaDesignerAddResponse addResponse =+ schemaDesigner.addSchemaObject(+ configSet, schemaVersion, loadAddBody("schema-designer/add-new-field.json"));+ assertNotNull(addResponse.field);++ // Add a new field type+ schemaVersion = addResponse.schemaVersion;+ // POST /schema-designer/{configSet}+ addResponse =+ schemaDesigner.addSchemaObject(+ configSet, schemaVersion, loadAddBody("schema-designer/add-new-type.json"));+ assertNotNull(addResponse.fieldType);++ // Let's do a diff now+ SchemaDesignerSchemaDiffResponse diffResp = schemaDesigner.getSchemaDiff(configSet);++ Map<String, Object> diff = diffResp.diff;++ // field asserts+ assertNotNull(diff.get("fields"));+ Map<String, Object> fieldsDiff = (Map<String, Object>) diff.get("fields");+ assertNotNull(fieldsDiff.get("updated"));+ Map<String, Object> mapDiff = (Map<String, Object>) fieldsDiff.get("updated");+ assertEquals(+ Arrays.asList(+ Map.of(+ "omitTermFreqAndPositions",+ true,+ "useDocValuesAsStored",+ true,+ "docValues",+ true,+ "uninvertible",+ false),+ Map.of(+ "omitTermFreqAndPositions",+ false,+ "useDocValuesAsStored",+ false,+ "docValues",+ false,+ "uninvertible",+ true)),+ mapDiff.get("id"));+ assertNotNull(fieldsDiff.get("added"));+ Map<String, Object> fieldsAdded = (Map<String, Object>) fieldsDiff.get("added");+ assertNotNull(fieldsAdded.get("keywords"));++ // field type asserts+ assertNotNull(diff.get("fieldTypes"));+ Map<String, Object> fieldTypesDiff = (Map<String, Object>) diff.get("fieldTypes");+ assertNotNull(fieldTypesDiff.get("added"));+ Map<String, Object> fieldTypesAdded = (Map<String, Object>) fieldTypesDiff.get("added");+ assertNotNull(fieldTypesAdded.get("test_txt"));+ }++ @Test+ @SuppressWarnings("unchecked")+ public void testQueryReturnsErrorDetailsOnIndexingFailure() throws Exception {+ String configSet = "queryIndexErrTest";++ // Prep the schema and analyze sample docs so the temp collection and stored docs exist+ schemaDesigner.prepNewSchema(configSet, null);+ ContentStreamBase.StringStream stream =+ new ContentStreamBase.StringStream("[{\"id\":\"doc1\",\"title\":\"test doc\"}]", JSON_MIME);+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));+ schemaDesigner.analyze(configSet, null, null, null, null, null, null, null);++ // analyze() already populated the cache; invalidate it so query() below is forced to re-index.+ invalidateIndexedVersionCache(getMutableId(configSet));++ // Build an API instance that simulates indexing errors.+ Map<Object, Throwable> fakeErrors = new HashMap<>();+ fakeErrors.put("doc1", new RuntimeException("simulated indexing failure"));+ SchemaDesigner apiWithErrors =+ new SchemaDesigner(+ cc,+ SchemaDesigner.newSchemaSuggester(),+ SchemaDesigner.newSampleDocumentsLoader(),+ mockReq) {+ @Override+ protected Map<Object, Throwable> indexSampleDocsWithRebuildOnAnalysisError(+ String idField,+ List<SolrInputDocument> docs,+ String collectionName,+ boolean asBatch,+ String[] analysisErrorHolder)+ throws IOException, SolrServerException {+ return fakeErrors;+ }+ };++ when(mockReq.getContentStreams()).thenReturn(null);+ FlexibleSolrJerseyResponse response = apiWithErrors.query(configSet);++ Map<String, Object> props = response.unknownProperties();+ assertNotNull("updateError must be present in error response", props.get(UPDATE_ERROR));+ assertEquals(400, props.get("updateErrorCode"));+ Map<Object, Throwable> details = (Map<Object, Throwable>) props.get(ERROR_DETAILS);+ assertNotNull("errorDetails must be present in error response", details);+ assertTrue("errorDetails must contain the failing doc id", details.containsKey("doc1"));+ }++ @Test+ public void testAnalyzeIgnoresClientCopyFromWhenConfigSetAlreadyExists() throws Exception {+ String configSet = "copyFromExistingTest";++ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ ContentStreamBase.StringStream stream =+ new ContentStreamBase.StringStream("[{\"id\":\"doc1\",\"title\":\"test doc\"}]", JSON_MIME);+ when(mockReq.getContentStreams()).thenReturn(List.of(stream));++ // configSet doesn't exist yet, so copyFrom falls back to _default; then publish it so+ // configExists(configSet) becomes true (disableDesigner=false so it can be reopened below).+ SchemaDesignerResponse response =+ schemaDesigner.analyze(configSet, null, null, null, List.of("en"), null, null, null);+ when(mockReq.getContentStreams()).thenReturn(null);+ schemaDesigner.publish(+ configSet, response.schemaVersion, null, false, 1, 1, false, true, false);++ // Reopen with a language change (forces syncLanguageSpecificObjectsAndFiles) and a bogus+ // copyFrom; copyFrom must be ignored since configSet already exists.+ SchemaDesignerResponse reopened =+ schemaDesigner.analyze(+ configSet, null, "does-not-exist-config", null, List.of("en", "fr"), null, null, null);+ assertNotNull(reopened.configSet);+ }++ @Test+ public void testIndexedVersionCachePersistsAcrossSchemaDesignerInstances() throws Exception {+ String configSet = "indexedVersionCacheTest";++ schemaDesigner.prepNewSchema(configSet, null);+ ModifiableSolrParams reqParams = new ModifiableSolrParams();+ reqParams.set(CONFIG_SET_PARAM, configSet);+ when(mockReq.getParams()).thenReturn(reqParams);+ when(mockReq.getContentStreams())+ .thenReturn(+ List.of(+ new ContentStreamBase.StringStream(+ "[{\"id\":\"doc1\",\"title\":\"test doc\"}]", JSON_MIME)));+ schemaDesigner.analyze(configSet, null, null, null, null, null, null, null);+ when(mockReq.getContentStreams()).thenReturn(null);++ // analyze() already indexes the sample docs itself (for field-guessing) and marks the cache+ // up-to-date via buildSchemaDesignerResponse, so invalidate it here to simulate "the schema+ // changed since the last index" and force the next query() to need a genuine rebuild.+ String mutableId = getMutableId(configSet);+ invalidateIndexedVersionCache(mutableId);++ // SchemaDesigner is instantiated fresh per JAX-RS request in production (see the @Inject+ // constructor), so simulate two separate requests querying the same configSet, each with its+ // own new instance, and count how many times the sample-doc rebuild actually runs.+ AtomicInteger rebuildCount = new AtomicInteger();+ for (int i = 0; i < 2; i++) {+ SchemaDesigner perRequestInstance =+ new SchemaDesigner(+ cc,+ SchemaDesigner.newSchemaSuggester(),+ SchemaDesigner.newSampleDocumentsLoader(),+ mockReq) {+ @Override+ protected Map<Object, Throwable> indexSampleDocsWithRebuildOnAnalysisError(+ String idField,+ List<SolrInputDocument> docs,+ String collectionName,+ boolean asBatch,+ String[] analysisErrorHolder)+ throws IOException, SolrServerException {+ rebuildCount.incrementAndGet();+ return super.indexSampleDocsWithRebuildOnAnalysisError(+ idField, docs, collectionName, asBatch, analysisErrorHolder);+ }+ };+ perRequestInstance.query(configSet);+ }++ // Only the first (post-invalidation) query should need to rebuild; the second, from a fresh+ // instance, should hit the shared cache.+ assertEquals(+ "a fresh SchemaDesigner instance querying an unchanged schema should hit the shared "+ + "indexedVersion cache and skip re-indexing, not rebuild again",+ 1,+ rebuildCount.get());+ }++ @Test+ public void testRequireSchemaVersionRejectsNegativeValues() throws Exception {+ String configSet = "schemaVersionValidation";+ schemaDesigner.prepNewSchema(configSet, null);++ // null schemaVersion must be rejected+ SolrException nullEx =+ expectThrows(+ SolrException.class, () -> schemaDesigner.addSchemaObject(configSet, null, null));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, nullEx.code());++ // negative schemaVersion must be rejected (was previously bypassing validation)+ SolrException negEx =+ expectThrows(+ SolrException.class, () -> schemaDesigner.addSchemaObject(configSet, -1, null));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, negEx.code());++ // same contract must hold for updateSchemaObject+ SolrException updateNegEx =+ expectThrows(+ SolrException.class, () -> schemaDesigner.updateSchemaObject(configSet, -1, null));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, updateNegEx.code());+ }++ protected void assertDesignerSettings(Map<String, Object> expected, Map<String, Object> actual) {+ for (String expKey : expected.keySet()) {+ Object expValue = expected.get(expKey);+ assertEquals(+ "Value for designer setting '" + expKey + "' not match expected!",+ expValue,+ actual.get(expKey));+ }+ }++ protected void assertDesignerSettings(+ Map<String, Object> expected, SchemaDesignerResponse response) {+ Map<String, Object> actual = new HashMap<>();+ actual.put(LANGUAGES_PARAM, response.languages);+ actual.put(ENABLE_FIELD_GUESSING_PARAM, response.enableFieldGuessing);+ actual.put(ENABLE_DYNAMIC_FIELDS_PARAM, response.enableDynamicFields);+ actual.put(ENABLE_NESTED_DOCS_PARAM, response.enableNestedDocs);+ actual.put(COPY_FROM_PARAM, response.copyFrom);+ assertDesignerSettings(expected, actual);+ }++ protected void assertDesignerSettings(+ Map<String, Object> expected, SchemaDesignerInfoResponse response) {+ Map<String, Object> actual = new HashMap<>();+ actual.put(LANGUAGES_PARAM, response.languages);+ actual.put(ENABLE_FIELD_GUESSING_PARAM, response.enableFieldGuessing);+ actual.put(ENABLE_DYNAMIC_FIELDS_PARAM, response.enableDynamicFields);+ actual.put(ENABLE_NESTED_DOCS_PARAM, response.enableNestedDocs);+ actual.put(COPY_FROM_PARAM, response.copyFrom);+ assertDesignerSettings(expected, actual);+ }++ private SchemaDesignerAddRequestBody loadAddBody(String fixturePath) throws IOException {+ return SolrJacksonMapper.getObjectMapper()+ .readValue(getFile(fixturePath).toFile(), SchemaDesignerAddRequestBody.class);+ }++ private SchemaDesignerUpdateRequestBody loadUpdateBody(String fixturePath) throws IOException {+ return SolrJacksonMapper.getObjectMapper()+ .readValue(getFile(fixturePath).toFile(), SchemaDesignerUpdateRequestBody.class);+ }+}diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerAPI.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerAPI.javadeleted file mode 100644index 6eb157e914c..00000000000--- a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerAPI.java+++ /dev/null@@ -1,935 +0,0 @@-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--package org.apache.solr.handler.designer;--import static org.apache.solr.common.params.CommonParams.JSON_MIME;-import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;-import static org.apache.solr.handler.designer.SchemaDesignerAPI.getMutableId;-import static org.apache.solr.response.RawResponseWriter.CONTENT;-import static org.mockito.Mockito.mock;-import static org.mockito.Mockito.when;--import java.nio.file.Files;-import java.nio.file.Path;-import java.util.Arrays;-import java.util.Collection;-import java.util.List;-import java.util.Map;-import java.util.Optional;-import java.util.stream.Stream;-import org.apache.solr.client.solrj.request.SolrQuery;-import org.apache.solr.client.solrj.response.QueryResponse;-import org.apache.solr.cloud.SolrCloudTestCase;-import org.apache.solr.common.SolrDocumentList;-import org.apache.solr.common.SolrException;-import org.apache.solr.common.cloud.SolrZkClient;-import org.apache.solr.common.params.CommonParams;-import org.apache.solr.common.params.ModifiableSolrParams;-import org.apache.solr.common.params.SolrParams;-import org.apache.solr.common.util.ContentStream;-import org.apache.solr.common.util.ContentStreamBase;-import org.apache.solr.common.util.NamedList;-import org.apache.solr.common.util.SimpleOrderedMap;-import org.apache.solr.core.CoreContainer;-import org.apache.solr.handler.TestSampleDocumentsLoader;-import org.apache.solr.request.SolrQueryRequest;-import org.apache.solr.response.SolrQueryResponse;-import org.apache.solr.schema.ManagedIndexSchema;-import org.apache.solr.schema.SchemaField;-import org.apache.solr.util.ExternalPaths;-import org.junit.AfterClass;-import org.junit.Before;-import org.junit.BeforeClass;-import org.junit.Test;-import org.noggit.JSONUtil;--public class TestSchemaDesignerAPI extends SolrCloudTestCase implements SchemaDesignerConstants {-- private CoreContainer cc;- private SchemaDesignerAPI schemaDesignerAPI;-- @BeforeClass- public static void createCluster() throws Exception {- System.setProperty("managed.schema.mutable", "true");- configureCluster(1)- .addConfig(DEFAULT_CONFIGSET_NAME, ExternalPaths.DEFAULT_CONFIGSET)- .configure();- }-- @AfterClass- public static void tearDownCluster() throws Exception {- if (cluster != null && cluster.getSolrClient() != null) {- cluster.deleteAllCollections();- cluster.deleteAllConfigSets();- }- }-- @Before- public void setupTest() {- assumeWorkingMockito();- assertNotNull(cluster);- cc = cluster.getJettySolrRunner(0).getCoreContainer();- assertNotNull(cc);- schemaDesignerAPI = new SchemaDesignerAPI(cc);- }-- public void testTSV() throws Exception {- String configSet = "testTSV";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- // GET /schema-designer/info- SolrQueryResponse rsp = new SolrQueryResponse();- SolrQueryRequest req = mock(SolrQueryRequest.class);-- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(LANGUAGES_PARAM, "en");- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, false);- when(req.getParams()).thenReturn(reqParams);-- String tsv = "id\tcol1\tcol2\n1\tfoo\tbar\n2\tbaz\tbah\n";-- // POST some sample TSV docs- ContentStream stream = new ContentStreamBase.StringStream(tsv, "text/csv");- when(req.getContentStreams()).thenReturn(List.of(stream));-- // POST /schema-designer/analyze- schemaDesignerAPI.analyze(req, rsp);- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- assertEquals(2, rsp.getValues().get("numDocs"));-- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- rsp = new SolrQueryResponse();- schemaDesignerAPI.cleanupTemp(req, rsp);-- String mutableId = getMutableId(configSet);- assertFalse(cc.getZkController().getClusterState().hasCollection(mutableId));- SolrZkClient zkClient = cc.getZkController().getZkClient();- assertFalse(zkClient.exists("/configs/" + mutableId));- }-- @Test- @SuppressWarnings("unchecked")- public void testAddTechproductsProgressively() throws Exception {- Path docsDir = ExternalPaths.SOURCE_HOME.resolve("example/exampledocs");- assertTrue(docsDir + " not found!", Files.isDirectory(docsDir));- List<Path> toAdd;- try (Stream<Path> files = Files.list(docsDir)) {- toAdd =- files- .filter(- (dir) -> {- String name = dir.getFileName().toString();- return name.endsWith(".xml")- || name.endsWith(".json")- || name.endsWith(".csv")- || name.endsWith(".jsonl");- })- .toList();- assertNotNull("No test data files found in " + docsDir, toAdd);- }-- String configSet = "techproducts";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- // GET /schema-designer/info- SolrQueryResponse rsp = new SolrQueryResponse();- SolrQueryRequest req = mock(SolrQueryRequest.class);- reqParams.set(CONFIG_SET_PARAM, configSet);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.getInfo(req, rsp);- // response should just be the default values- Map<String, Object> expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, true,- ENABLE_FIELD_GUESSING_PARAM, true,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, List.of());- assertDesignerSettings(expSettings, rsp.getValues());- SolrParams rspData = rsp.getValues().toSolrParams();- int schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);- assertEquals(schemaVersion, -1); // shouldn't exist yet-- // Use the prep endpoint to prepare the new schema- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- rsp = new SolrQueryResponse();- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.prepNewSchema(req, rsp);- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- for (Path next : toAdd) {- // Analyze some sample documents to refine the schema- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(LANGUAGES_PARAM, "en");- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, false);- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);-- // POST some sample JSON docs- ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(next);- stream.setContentType(- TestSampleDocumentsLoader.guessContentTypeFromFilename(next.getFileName().toString()));- when(req.getContentStreams()).thenReturn(List.of(stream));-- rsp = new SolrQueryResponse();-- // POST /schema-designer/analyze- schemaDesignerAPI.analyze(req, rsp);-- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- assertNotNull(rsp.getValues().get("fields"));- assertNotNull(rsp.getValues().get("fieldTypes"));- assertNotNull(rsp.getValues().get("docIds"));-- // capture the schema version for MVCC- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);- }-- // get info (from the temp)- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();-- // GET /schema-designer/info- schemaDesignerAPI.getInfo(req, rsp);- expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, false,- ENABLE_FIELD_GUESSING_PARAM, true,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, List.of("en"),- COPY_FROM_PARAM, "_default");- assertDesignerSettings(expSettings, rsp.getValues());-- // query to see how the schema decisions impact retrieval / ranking- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(CommonParams.Q, "*:*");- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();-- // GET /schema-designer/query- schemaDesignerAPI.query(req, rsp);- assertNotNull(rsp.getResponseHeader());- SolrDocumentList results = (SolrDocumentList) rsp.getResponse();- assertEquals(47, results.getNumFound());-- // publish schema to a config set that can be used by real collections- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- String collection = "techproducts";- reqParams.set(NEW_COLLECTION_PARAM, collection);- reqParams.set(INDEX_TO_COLLECTION_PARAM, true);- reqParams.set(RELOAD_COLLECTIONS_PARAM, true);- reqParams.set(CLEANUP_TEMP_PARAM, true);- reqParams.set(DISABLE_DESIGNER_PARAM, true);-- rsp = new SolrQueryResponse();- schemaDesignerAPI.publish(req, rsp);- assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));-- // listCollectionsForConfig- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- rsp = new SolrQueryResponse();- schemaDesignerAPI.listCollectionsForConfig(req, rsp);- List<String> collections = (List<String>) rsp.getValues().get("collections");- assertNotNull(collections);- assertTrue(collections.contains(collection));-- // now try to create another temp, which should fail since designer is disabled for this- // configSet now- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- rsp = new SolrQueryResponse();- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- try {- schemaDesignerAPI.prepNewSchema(req, rsp);- fail("Prep should fail for locked schema " + configSet);- } catch (SolrException solrExc) {- assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrExc.code());- }- }-- @Test- @SuppressWarnings("unchecked")- public void testSuggestFilmsXml() throws Exception {- String configSet = "films";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- Path filmsDir = ExternalPaths.SOURCE_HOME.resolve("example/films");- assertTrue(filmsDir + " not found!", Files.isDirectory(filmsDir));- Path filmsXml = filmsDir.resolve("films.xml");- assertTrue("example/films/films.xml not found", Files.isRegularFile(filmsXml));-- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, "true");-- SolrQueryRequest req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);-- // POST some sample XML docs- ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(filmsXml);- stream.setContentType("application/xml");- when(req.getContentStreams()).thenReturn(List.of(stream));-- SolrQueryResponse rsp = new SolrQueryResponse();-- // POST /schema-designer/analyze- schemaDesignerAPI.analyze(req, rsp);-- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- assertNotNull(rsp.getValues().get("fields"));- assertNotNull(rsp.getValues().get("fieldTypes"));- List<String> docIds = (List<String>) rsp.getValues().get("docIds");- assertNotNull(docIds);- assertEquals(100, docIds.size()); // designer limits the doc ids to top 100-- String idField = rsp.getValues()._getStr(UNIQUE_KEY_FIELD_PARAM);- assertNotNull(idField);- }-- @Test- @SuppressWarnings("unchecked")- public void testBasicUserWorkflow() throws Exception {- String configSet = "testJson";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- // Use the prep endpoint to prepare the new schema- reqParams.set(CONFIG_SET_PARAM, configSet);- SolrQueryResponse rsp = new SolrQueryResponse();- SolrQueryRequest req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.prepNewSchema(req, rsp);- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));-- Map<String, Object> expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, true,- ENABLE_FIELD_GUESSING_PARAM, true,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, List.of(),- COPY_FROM_PARAM, "_default");- assertDesignerSettings(expSettings, rsp.getValues());-- // Analyze some sample documents to refine the schema- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);-- // POST some sample JSON docs- Path booksJson = ExternalPaths.SOURCE_HOME.resolve("example/exampledocs/books.json");- ContentStreamBase.FileStream stream = new ContentStreamBase.FileStream(booksJson);- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));-- rsp = new SolrQueryResponse();-- // POST /schema-designer/analyze- schemaDesignerAPI.analyze(req, rsp);-- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- assertNotNull(rsp.getValues().get("fields"));- assertNotNull(rsp.getValues().get("fieldTypes"));- assertNotNull(rsp.getValues().get("docIds"));- String idField = rsp.getValues()._getStr(UNIQUE_KEY_FIELD_PARAM);- assertNotNull(idField);- assertDesignerSettings(expSettings, rsp.getValues());-- // capture the schema version for MVCC- SolrParams rspData = rsp.getValues().toSolrParams();- reqParams.clear();- int schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // load the contents of a file- Collection<String> files = (Collection<String>) rsp.getValues().get("files");- assertTrue(files != null && !files.isEmpty());-- reqParams.set(CONFIG_SET_PARAM, configSet);- String file = null;- for (String f : files) {- if ("solrconfig.xml".equals(f)) {- file = f;- break;- }- }- assertNotNull("solrconfig.xml not found in files!", file);- reqParams.set("file", file);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();- schemaDesignerAPI.getFileContents(req, rsp);- String solrconfigXml = (String) rsp.getValues().get(file);- assertNotNull(solrconfigXml);- reqParams.clear();-- // Update solrconfig.xml- rsp = new SolrQueryResponse();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set("file", file);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- when(req.getContentStreams())- .thenReturn(List.of(new ContentStreamBase.StringStream(solrconfigXml, "application/xml")));-- schemaDesignerAPI.updateFileContents(req, rsp);- rspData = rsp.getValues().toSolrParams();- reqParams.clear();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // update solrconfig.xml with some invalid XML mess- rsp = new SolrQueryResponse();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set("file", file);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- when(req.getContentStreams())- .thenReturn(List.of(new ContentStreamBase.StringStream("<config/>", "application/xml")));-- // this should fail b/c the updated solrconfig.xml is invalid- schemaDesignerAPI.updateFileContents(req, rsp);- rspData = rsp.getValues().toSolrParams();- reqParams.clear();- assertNotNull(rspData.get("updateFileError"));-- // remove dynamic fields and change the language to "en" only- rsp = new SolrQueryResponse();- // POST /schema-designer/analyze- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(LANGUAGES_PARAM, "en");- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, false);- reqParams.set(ENABLE_FIELD_GUESSING_PARAM, false);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.analyze(req, rsp);-- expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, false,- ENABLE_FIELD_GUESSING_PARAM, false,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, List.of("en"),- COPY_FROM_PARAM, "_default");- assertDesignerSettings(expSettings, rsp.getValues());-- List<String> filesInResp = (List<String>) rsp.getValues().get("files");- assertEquals(5, filesInResp.size());- assertTrue(filesInResp.contains("lang/stopwords_en.txt"));-- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- reqParams.clear();-- // add the dynamic fields back and change the languages too- rsp = new SolrQueryResponse();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.add(LANGUAGES_PARAM, "en");- reqParams.add(LANGUAGES_PARAM, "fr");- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, true);- reqParams.set(ENABLE_FIELD_GUESSING_PARAM, false);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.analyze(req, rsp);-- expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, true,- ENABLE_FIELD_GUESSING_PARAM, false,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, Arrays.asList("en", "fr"),- COPY_FROM_PARAM, "_default");- assertDesignerSettings(expSettings, rsp.getValues());-- filesInResp = (List<String>) rsp.getValues().get("files");- assertEquals(7, filesInResp.size());- assertTrue(filesInResp.contains("lang/stopwords_fr.txt"));-- rspData = rsp.getValues().toSolrParams();- reqParams.clear();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // add back all the default languages- rsp = new SolrQueryResponse();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.add(LANGUAGES_PARAM, "*");- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, false);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.analyze(req, rsp);-- expSettings =- Map.of(- ENABLE_DYNAMIC_FIELDS_PARAM, false,- ENABLE_FIELD_GUESSING_PARAM, false,- ENABLE_NESTED_DOCS_PARAM, false,- LANGUAGES_PARAM, List.of(),- COPY_FROM_PARAM, "_default");- assertDesignerSettings(expSettings, rsp.getValues());-- filesInResp = (List<String>) rsp.getValues().get("files");- assertEquals(43, filesInResp.size());- assertTrue(filesInResp.contains("lang/stopwords_fr.txt"));- assertTrue(filesInResp.contains("lang/stopwords_en.txt"));- assertTrue(filesInResp.contains("lang/stopwords_it.txt"));-- rspData = rsp.getValues().toSolrParams();- reqParams.clear();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // Get the value of a sample document- String docId = "978-0641723445";- String fieldName = "series_t";- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(DOC_ID_PARAM, docId);- reqParams.set(FIELD_PARAM, fieldName);- reqParams.set(UNIQUE_KEY_FIELD_PARAM, idField);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();-- // GET /schema-designer/sample- schemaDesignerAPI.getSampleValue(req, rsp);- rspData = rsp.getValues().toSolrParams();- assertNotNull(rspData.get(idField));- assertNotNull(rspData.get(fieldName));- assertNotNull(rspData.get("analysis"));-- reqParams.clear();-- // at this point the user would refine the schema by- // editing suggestions for fields and adding/removing fields / field types as needed-- // add a new field- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- stream = new ContentStreamBase.FileStream(getFile("schema-designer/add-new-field.json"));- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));- rsp = new SolrQueryResponse();-- // POST /schema-designer/add- schemaDesignerAPI.addSchemaObject(req, rsp);- assertNotNull(rsp.getValues().get("add-field"));- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);- assertNotNull(rsp.getValues().get("fields"));-- // update an existing field- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- // switch a single-valued field to a multi-valued field, which triggers a full rebuild of the- // "temp" collection- stream = new ContentStreamBase.FileStream(getFile("schema-designer/update-author-field.json"));- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));-- rsp = new SolrQueryResponse();-- // PUT /schema-designer/update- schemaDesignerAPI.updateSchemaObject(req, rsp);- assertNotNull(rsp.getValues().get("field"));- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // add a new type- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- stream = new ContentStreamBase.FileStream(getFile("schema-designer/add-new-type.json"));- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));- rsp = new SolrQueryResponse();-- // POST /schema-designer/add- schemaDesignerAPI.addSchemaObject(req, rsp);- final String expectedTypeName = "test_txt";- assertEquals(expectedTypeName, rsp.getValues().get("add-field-type"));- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);- assertNotNull(rsp.getValues().get("fieldTypes"));- List<SimpleOrderedMap<Object>> fieldTypes =- (List<SimpleOrderedMap<Object>>) rsp.getValues().get("fieldTypes");- Optional<SimpleOrderedMap<Object>> expected =- fieldTypes.stream().filter(m -> expectedTypeName.equals(m.get("name"))).findFirst();- assertTrue(- "New field type '" + expectedTypeName + "' not found in add type response!",- expected.isPresent());-- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- stream = new ContentStreamBase.FileStream(getFile("schema-designer/update-type.json"));- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));- rsp = new SolrQueryResponse();-- // POST /schema-designer/update- schemaDesignerAPI.updateSchemaObject(req, rsp);- rspData = rsp.getValues().toSolrParams();- schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // query to see how the schema decisions impact retrieval / ranking- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(CommonParams.Q, "*:*");- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();-- // GET /schema-designer/query- schemaDesignerAPI.query(req, rsp);- assertNotNull(rsp.getResponseHeader());- SolrDocumentList results = (SolrDocumentList) rsp.getResponse();- assertEquals(4, results.size());-- // Download ZIP- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- rsp = new SolrQueryResponse();- schemaDesignerAPI.downloadConfig(req, rsp);- assertNotNull(rsp.getValues().get(CONTENT));-- // publish schema to a config set that can be used by real collections- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);-- String collection = "test123";- reqParams.set(NEW_COLLECTION_PARAM, collection);- reqParams.set(INDEX_TO_COLLECTION_PARAM, true);- reqParams.set(RELOAD_COLLECTIONS_PARAM, true);- reqParams.set(CLEANUP_TEMP_PARAM, true);-- rsp = new SolrQueryResponse();- schemaDesignerAPI.publish(req, rsp);-- assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));-- // listCollectionsForConfig- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- rsp = new SolrQueryResponse();- schemaDesignerAPI.listCollectionsForConfig(req, rsp);- List<String> collections = (List<String>) rsp.getValues().get("collections");- assertNotNull(collections);- assertTrue(collections.contains(collection));-- // verify temp designer objects were cleaned up during the publish operation ...- String mutableId = getMutableId(configSet);- assertFalse(cc.getZkController().getClusterState().hasCollection(mutableId));- SolrZkClient zkClient = cc.getZkController().getZkClient();- assertFalse(zkClient.exists("/configs/" + mutableId));-- SolrQuery query = new SolrQuery("*:*");- query.setRows(0);- QueryResponse qr = cluster.getSolrClient().query(collection, query);- // this proves the docs were stored in the filestore too- assertEquals(4, qr.getResults().getNumFound());- }-- @SuppressWarnings("unchecked")- public void testFieldUpdates() throws Exception {- String configSet = "fieldUpdates";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- // Use the prep endpoint to prepare the new schema- reqParams.set(CONFIG_SET_PARAM, configSet);- SolrQueryResponse rsp = new SolrQueryResponse();- SolrQueryRequest req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.prepNewSchema(req, rsp);- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));- SolrParams rspData = rsp.getValues().toSolrParams();- int schemaVersion = rspData.getInt(SCHEMA_VERSION_PARAM);-- // add our test field that we'll test various updates to- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(schemaVersion));- reqParams.set(CONFIG_SET_PARAM, configSet);- req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- ContentStreamBase.FileStream stream =- new ContentStreamBase.FileStream(getFile("schema-designer/add-new-field.json"));- stream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stream));- rsp = new SolrQueryResponse();-- // POST /schema-designer/add- schemaDesignerAPI.addSchemaObject(req, rsp);- assertNotNull(rsp.getValues().get("add-field"));-- final String fieldName = "keywords";-- Optional<SimpleOrderedMap<Object>> maybeField =- ((List<SimpleOrderedMap<Object>>) rsp.getValues().get("fields"))- .stream().filter(m -> fieldName.equals(m.get("name"))).findFirst();- assertTrue(maybeField.isPresent());- SimpleOrderedMap<Object> field = maybeField.get();- assertEquals(Boolean.FALSE, field.get("indexed"));- assertEquals(Boolean.FALSE, field.get("required"));- assertEquals(Boolean.TRUE, field.get("stored"));- assertEquals(Boolean.TRUE, field.get("docValues"));- assertEquals(Boolean.TRUE, field.get("useDocValuesAsStored"));- assertEquals(Boolean.FALSE, field.get("multiValued"));- assertEquals("string", field.get("type"));-- String mutableId = getMutableId(configSet);- SchemaDesignerConfigSetHelper configSetHelper =- new SchemaDesignerConfigSetHelper(cc, SchemaDesignerAPI.newSchemaSuggester());- ManagedIndexSchema schema = schemaDesignerAPI.loadLatestSchema(mutableId);-- // make it required- Map<String, Object> updateField =- Map.of("name", fieldName, "type", field.get("type"), "required", true);- configSetHelper.updateField(configSet, updateField, schema);-- schema = schemaDesignerAPI.loadLatestSchema(mutableId);- SchemaField schemaField = schema.getField(fieldName);- assertTrue(schemaField.isRequired());-- updateField =- Map.of("name", fieldName, "type", field.get("type"), "required", false, "stored", false);- configSetHelper.updateField(configSet, updateField, schema);- schema = schemaDesignerAPI.loadLatestSchema(mutableId);- schemaField = schema.getField(fieldName);- assertFalse(schemaField.isRequired());- assertFalse(schemaField.stored());-- updateField =- Map.of(- "name",- fieldName,- "type",- field.get("type"),- "required",- false,- "stored",- false,- "multiValued",- true);- configSetHelper.updateField(configSet, updateField, schema);- schema = schemaDesignerAPI.loadLatestSchema(mutableId);- schemaField = schema.getField(fieldName);- assertFalse(schemaField.isRequired());- assertFalse(schemaField.stored());- assertTrue(schemaField.multiValued());-- updateField = Map.of("name", fieldName, "type", "strings", "copyDest", "_text_");- configSetHelper.updateField(configSet, updateField, schema);- schema = schemaDesignerAPI.loadLatestSchema(mutableId);- schemaField = schema.getField(fieldName);- assertTrue(schemaField.multiValued());- assertEquals("strings", schemaField.getType().getTypeName());- assertFalse(schemaField.isRequired());- assertTrue(schemaField.stored());- List<String> srcFields = schema.getCopySources("_text_");- assertEquals(List.of(fieldName), srcFields);- }-- @SuppressWarnings({"unchecked"})- public void testSchemaDiffEndpoint() throws Exception {- String configSet = "testDiff";-- ModifiableSolrParams reqParams = new ModifiableSolrParams();-- // Use the prep endpoint to prepare the new schema- reqParams.set(CONFIG_SET_PARAM, configSet);- SolrQueryResponse rsp = new SolrQueryResponse();- SolrQueryRequest req = mock(SolrQueryRequest.class);- when(req.getParams()).thenReturn(reqParams);- schemaDesignerAPI.prepNewSchema(req, rsp);- assertNotNull(rsp.getValues().get(CONFIG_SET_PARAM));- assertNotNull(rsp.getValues().get(SCHEMA_VERSION_PARAM));-- // publish schema to a config set that can be used by real collections- reqParams.clear();- reqParams.set(SCHEMA_VERSION_PARAM, String.valueOf(rsp.getValues().get(SCHEMA_VERSION_PARAM)));- reqParams.set(CONFIG_SET_PARAM, configSet);-- String collection = "diff456";- reqParams.set(NEW_COLLECTION_PARAM, collection);- reqParams.set(INDEX_TO_COLLECTION_PARAM, true);- reqParams.set(RELOAD_COLLECTIONS_PARAM, true);- reqParams.set(CLEANUP_TEMP_PARAM, true);-- rsp = new SolrQueryResponse();- schemaDesignerAPI.publish(req, rsp);-- assertNotNull(cc.getZkController().zkStateReader.getCollection(collection));-- // Load the schema designer for the existing config set and make some changes to it- reqParams.clear();- reqParams.set(CONFIG_SET_PARAM, configSet);- reqParams.set(ENABLE_DYNAMIC_FIELDS_PARAM, "true");- reqParams.set(ENABLE_FIELD_GUESSING_PARAM, "false");- rsp = new SolrQueryResponse();- schemaDesignerAPI.analyze(req, rsp);-- // Update id field to not use docValues- List<SimpleOrderedMap<Object>> fields =- (List<SimpleOrderedMap<Object>>) rsp.getValues().get("fields");- SimpleOrderedMap<Object> idFieldMap =- fields.stream().filter(field -> field.get("name").equals("id")).findFirst().get();- idFieldMap.remove("copyDest"); // Don't include copyDest as it is not a property of SchemaField- SimpleOrderedMap<Object> idFieldMapUpdated = idFieldMap.clone();- idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("docValues", 0), Boolean.FALSE);- idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("useDocValuesAsStored", 0), Boolean.FALSE);- idFieldMapUpdated.setVal(idFieldMapUpdated.indexOf("uninvertible", 0), Boolean.TRUE);- idFieldMapUpdated.setVal(- idFieldMapUpdated.indexOf("omitTermFreqAndPositions", 0), Boolean.FALSE);-- SolrParams solrParams = idFieldMapUpdated.toSolrParams();- Map<String, Object> mapParams = new SimpleOrderedMap<>(solrParams);- mapParams.put("termVectors", Boolean.FALSE);- reqParams.set(- SCHEMA_VERSION_PARAM, rsp.getValues().toSolrParams().getInt(SCHEMA_VERSION_PARAM));-- ContentStreamBase.StringStream stringStream =- new ContentStreamBase.StringStream(JSONUtil.toJSON(mapParams), JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(stringStream));-- rsp = new SolrQueryResponse();- schemaDesignerAPI.updateSchemaObject(req, rsp);-- // Add a new field- Integer schemaVersion = rsp.getValues().toSolrParams().getInt(SCHEMA_VERSION_PARAM);- reqParams.set(SCHEMA_VERSION_PARAM, schemaVersion);- ContentStreamBase.FileStream fileStream =- new ContentStreamBase.FileStream(getFile("schema-designer/add-new-field.json"));- fileStream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(fileStream));- rsp = new SolrQueryResponse();- // POST /schema-designer/add- schemaDesignerAPI.addSchemaObject(req, rsp);- assertNotNull(rsp.getValues().get("add-field"));-- // Add a new field type- schemaVersion = rsp.getValues().toSolrParams().getInt(SCHEMA_VERSION_PARAM);- reqParams.set(SCHEMA_VERSION_PARAM, schemaVersion);- fileStream = new ContentStreamBase.FileStream(getFile("schema-designer/add-new-type.json"));- fileStream.setContentType(JSON_MIME);- when(req.getContentStreams()).thenReturn(List.of(fileStream));- rsp = new SolrQueryResponse();- // POST /schema-designer/add- schemaDesignerAPI.addSchemaObject(req, rsp);- assertNotNull(rsp.getValues().get("add-field-type"));-- // Let's do a diff now- rsp = new SolrQueryResponse();- schemaDesignerAPI.getSchemaDiff(req, rsp);-- Map<String, Object> diff = (Map<String, Object>) rsp.getValues().get("diff");-- // field asserts- assertNotNull(diff.get("fields"));- Map<String, Object> fieldsDiff = (Map<String, Object>) diff.get("fields");- assertNotNull(fieldsDiff.get("updated"));- Map<String, Object> mapDiff = (Map<String, Object>) fieldsDiff.get("updated");- assertEquals(- Arrays.asList(- Map.of(- "omitTermFreqAndPositions",- true,- "useDocValuesAsStored",- true,- "docValues",- true,- "uninvertible",- false),- Map.of(- "omitTermFreqAndPositions",- false,- "useDocValuesAsStored",- false,- "docValues",- false,- "uninvertible",- true)),- mapDiff.get("id"));- assertNotNull(fieldsDiff.get("added"));- Map<String, Object> fieldsAdded = (Map<String, Object>) fieldsDiff.get("added");- assertNotNull(fieldsAdded.get("keywords"));-- // field type asserts- assertNotNull(diff.get("fieldTypes"));- Map<String, Object> fieldTypesDiff = (Map<String, Object>) diff.get("fieldTypes");- assertNotNull(fieldTypesDiff.get("added"));- Map<String, Object> fieldTypesAdded = (Map<String, Object>) fieldTypesDiff.get("added");- assertNotNull(fieldTypesAdded.get("test_txt"));- }-- protected void assertDesignerSettings(Map<String, Object> expected, NamedList<?> actual) {- for (String expKey : expected.keySet()) {- Object expValue = expected.get(expKey);- assertEquals(- "Value for designer setting '" + expKey + "' not match expected!",- expValue,- actual.get(expKey));- }- }-}diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.javaindex 82231061bdf..300a78c3581 100644--- a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java+++ b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java@@ -19,7 +19,7 @@ package org.apache.solr.handler.designer;import static org.apache.solr.common.util.Utils.toJavabin;import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;-import static org.apache.solr.handler.designer.SchemaDesignerAPI.getMutableId;+import static org.apache.solr.handler.designer.SchemaDesigner.getMutableId;import static org.apache.solr.schema.IndexSchema.NEST_PATH_FIELD_NAME;import static org.apache.solr.schema.IndexSchema.ROOT_FIELD_NAME;@@ -37,6 +37,7 @@ import org.apache.solr.common.util.Utils;import org.apache.solr.core.CoreContainer;import org.apache.solr.core.SolrConfig;import org.apache.solr.filestore.FileStore;+import org.apache.solr.handler.configsets.DownloadConfigSet;import org.apache.solr.schema.FieldType;import org.apache.solr.schema.ManagedIndexSchema;import org.apache.solr.schema.SchemaField;@@ -74,7 +75,7 @@ public class TestSchemaDesignerConfigSetHelper extends SolrCloudTestCaseassertNotNull(cluster);cc = cluster.getJettySolrRunner(0).getCoreContainer();assertNotNull(cc);- helper = new SchemaDesignerConfigSetHelper(cc, SchemaDesignerAPI.newSchemaSuggester());+ helper = new SchemaDesignerConfigSetHelper(cc, SchemaDesigner.newSchemaSuggester());}@Test@@ -112,13 +113,14 @@ public class TestSchemaDesignerConfigSetHelper extends SolrCloudTestCaseconfigSet, schema, List.of(), true, DEFAULT_CONFIGSET_NAME);assertEquals(2, schema.getSchemaZkVersion());- byte[] zipped = helper.downloadAndZipConfigSet(mutableId);+ byte[] zipped = DownloadConfigSet.zipConfigSet(cc.getConfigSetService(), mutableId);assertTrue(zipped != null && zipped.length > 0);}@Testpublic void testDownloadAndZip() throws IOException {- byte[] zipped = helper.downloadAndZipConfigSet(DEFAULT_CONFIGSET_NAME);+ byte[] zipped =+ DownloadConfigSet.zipConfigSet(cc.getConfigSetService(), DEFAULT_CONFIGSET_NAME);ZipInputStream stream = new ZipInputStream(new ByteArrayInputStream(zipped));boolean foundSolrConfig = false;@@ -176,7 +178,7 @@ public class TestSchemaDesignerConfigSetHelper extends SolrCloudTestCaseassertTrue(cluster.getZkClient()- .exists(SchemaDesignerAPI.getConfigSetZkPath(mutableId, "lang/stopwords_en.txt")));+ .exists(SchemaDesigner.getConfigSetZkPath(mutableId, "lang/stopwords_en.txt")));assertNotNull(schema.getFieldTypeByName("text_fr"));assertNotNull(schema.getFieldOrNull("*_txt_fr"));assertNull(schema.getFieldOrNull("*_txt_ga"));@@ -198,7 +200,7 @@ public class TestSchemaDesignerConfigSetHelper extends SolrCloudTestCaseassertTrue(cluster.getZkClient()- .exists(SchemaDesignerAPI.getConfigSetZkPath(mutableId, "lang/stopwords_en.txt")));+ .exists(SchemaDesigner.getConfigSetZkPath(mutableId, "lang/stopwords_en.txt")));assertNotNull(schema.getFieldTypeByName("text_fr"));assertNotNull(schema.getFieldOrNull("*_txt_fr"));assertNull(schema.getFieldOrNull("*_txt_ga"));diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerSolrJ.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerSolrJ.javanew file mode 100644index 00000000000..d755b6964d9--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerSolrJ.java@@ -0,0 +1,159 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.handler.designer;++import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;++import java.io.ByteArrayInputStream;+import java.nio.charset.StandardCharsets;+import java.util.Map;+import org.apache.solr.client.api.model.SchemaDesignerAddResponse;+import org.apache.solr.client.api.model.SchemaDesignerFileContentsResponse;+import org.apache.solr.client.api.model.SchemaDesignerInfoResponse;+import org.apache.solr.client.api.model.SchemaDesignerResponse;+import org.apache.solr.client.api.model.SchemaDesignerUpdateResponse;+import org.apache.solr.client.solrj.request.SchemaDesignerApi;+import org.apache.solr.cloud.SolrCloudTestCase;+import org.apache.solr.util.ExternalPaths;+import org.junit.AfterClass;+import org.junit.BeforeClass;+import org.junit.Test;++/**+ * Smoke tests that exercise the Schema Designer V2 API through the generated SolrJ client. Unlike+ * {@link TestSchemaDesigner}, which calls the handler in-process and bypasses both the JAX-RS layer+ * and JSON (de)serialization, these tests round-trip every typed request body and response over+ * HTTP. They guard against regressions in the {@code @JsonProperty} / {@code @Schema} /+ * {@code @JsonAnySetter} annotations and the OpenAPI-generated SolrJ wiring. This is required as+ * the primary interaction mechanism to Schema Designer APIs is via the schema-designer.js JSON+ * calls, not SolrJ.+ */+public class TestSchemaDesignerSolrJ extends SolrCloudTestCase {++ @BeforeClass+ public static void createCluster() throws Exception {+ System.setProperty("managed.schema.mutable", "true");+ configureCluster(1)+ .addConfig(DEFAULT_CONFIGSET_NAME, ExternalPaths.DEFAULT_CONFIGSET)+ .configure();+ }++ @AfterClass+ public static void cleanup() throws Exception {+ if (cluster != null && cluster.getSolrClient() != null) {+ cluster.deleteAllCollections();+ cluster.deleteAllConfigSets();+ }+ }++ /**+ * Walks through prep → add (each of the four wrapper keys) → update → info, verifying both the+ * SolrJ request wiring and the typed-response deserialization.+ */+ @Test+ public void testTypedBodyRoundTrip() throws Exception {+ final String configSet = "solrjSmoke";++ // POST /schema-designer/{configSet}/prep — no body, baseline that SolrJ wiring works+ SchemaDesignerResponse prep =+ new SchemaDesignerApi.PrepNewSchema(configSet).process(cluster.getSolrClient());+ assertEquals(configSet, prep.configSet);+ int schemaVersion = prep.schemaVersion;++ // POST /{configSet} — addField — verifies the dual-annotation pair: @Schema(name="addField")+ // generates a camelCase SolrJ setter, while @JsonProperty("add-field") serializes it to+ // kebab-case on the wire. Both must be correct for the round-trip to succeed.+ var addField = new SchemaDesignerApi.AddSchemaObject(configSet);+ addField.setSchemaVersion(schemaVersion);+ addField.setAddField(Map.of("name", "keywords", "type", "string", "stored", true));+ SchemaDesignerAddResponse addFieldResp = addField.process(cluster.getSolrClient());+ assertEquals("keywords", addFieldResp.field);+ schemaVersion = addFieldResp.schemaVersion;++ // POST /{configSet} — addFieldType — covers a different wrapper key+ var addType = new SchemaDesignerApi.AddSchemaObject(configSet);+ addType.setSchemaVersion(schemaVersion);+ addType.setAddFieldType(+ Map.of(+ "name",+ "smoke_txt",+ "class",+ "solr.TextField",+ "analyzer",+ Map.of("tokenizer", Map.of("class", "solr.StandardTokenizerFactory"))));+ SchemaDesignerAddResponse addTypeResp = addType.process(cluster.getSolrClient());+ assertEquals("smoke_txt", addTypeResp.fieldType);+ schemaVersion = addTypeResp.schemaVersion;++ // POST /{configSet} — addDynamicField+ var addDyn = new SchemaDesignerApi.AddSchemaObject(configSet);+ addDyn.setSchemaVersion(schemaVersion);+ addDyn.setAddDynamicField(Map.of("name", "*_smoke", "type", "string"));+ SchemaDesignerAddResponse addDynResp = addDyn.process(cluster.getSolrClient());+ assertEquals("*_smoke", addDynResp.dynamicField);+ schemaVersion = addDynResp.schemaVersion;++ // POST /{configSet} — addCopyField — verifies the explicit no-op response branch in+ // setSchemaObjectField (no field/type/dynamicField/fieldType is populated)+ var addCopy = new SchemaDesignerApi.AddSchemaObject(configSet);+ addCopy.setSchemaVersion(schemaVersion);+ addCopy.setAddCopyField(Map.of("source", "keywords", "dest", "_text_"));+ SchemaDesignerAddResponse addCopyResp = addCopy.process(cluster.getSolrClient());+ assertNull(addCopyResp.field);+ assertNull(addCopyResp.fieldType);+ assertNull(addCopyResp.dynamicField);+ schemaVersion = addCopyResp.schemaVersion;++ // PUT /{configSet} — exercises the @JsonAnyGetter/@JsonAnySetter capture for arbitrary attrs+ var update = new SchemaDesignerApi.UpdateSchemaObject(configSet);+ update.setSchemaVersion(schemaVersion);+ update.setName("keywords");+ update.setAdditionalProperties(Map.of("type", "string", "stored", true, "multiValued", true));+ SchemaDesignerUpdateResponse updateResp = update.process(cluster.getSolrClient());+ assertNotNull(updateResp.field);+ assertEquals("field", updateResp.updateType);++ // GET /{configSet} — round-trips a typed response that extends SchemaDesignerSettingsResponse+ SchemaDesignerInfoResponse info =+ new SchemaDesignerApi.GetInfo(configSet).process(cluster.getSolrClient());+ assertEquals(configSet, info.configSet);+ }++ /**+ * Exercises the {@code InputStream} body binding on {@code updateFileContents} by sending an+ * invalid {@code solrconfig.xml} and verifying the server returns the typed error fields rather+ * than throwing — this also confirms the bytes actually reached the server.+ */+ @Test+ public void testUpdateFileContentsBodyBinding() throws Exception {+ final String configSet = "solrjFileSmoke";++ new SchemaDesignerApi.PrepNewSchema(configSet).process(cluster.getSolrClient());++ byte[] invalidXml = "<config/>".getBytes(StandardCharsets.UTF_8);+ var req =+ new SchemaDesignerApi.UpdateFileContents(configSet, new ByteArrayInputStream(invalidXml));+ req.setFile("solrconfig.xml");+ SchemaDesignerFileContentsResponse resp = req.process(cluster.getSolrClient());++ assertNotNull(+ "server should report a validation error for the invalid solrconfig.xml",+ resp.updateFileError);+ assertEquals("<config/>", resp.fileContent);+ }+}diff --git a/solr/webapp/web/js/angular/app.js b/solr/webapp/web/js/angular/app.jsindex e4be0017384..1ff0cb824f2 100644--- a/solr/webapp/web/js/angular/app.js+++ b/solr/webapp/web/js/angular/app.js@@ -330,6 +330,15 @@ solrAdminApp.config([onSelect: '&'},link: function(scope, element, attrs) {+ // Bind once; previously this was inside the $watch which stacked listeners+ // on every data change and could fire synchronously during a digest,+ // triggering $rootScope:inprog.+ element.on("select_node.jstree", function (event, data) {+ scope.$applyAsync(function() {+ scope.onSelect({url: data.node.a_attr.href, data: data});+ });+ });+scope.$watch("data", function(newValue, oldValue) {if (newValue && !jQuery.isEmptyObject(newValue)) {var treeConfig = {@@ -339,7 +348,7 @@ solrAdminApp.config([}};- var tree = $(element).jstree(treeConfig);+ $(element).jstree(treeConfig);// This is done to ensure that the data can be refreshed if it is updated behind the scenes.// Putting the data in the treeConfig makes it stack and doesn't update.@@ -347,13 +356,6 @@ solrAdminApp.config([$(element).jstree(true).refresh();$(element).jstree('open_node','li:first');- if (tree) {- element.bind("select_node.jstree", function (event, data) {- scope.$apply(function() {- scope.onSelect({url: data.node.a_attr.href, data: data});- });- });- }}}, true);}diff --git a/solr/webapp/web/js/angular/controllers/schema-designer.js b/solr/webapp/web/js/angular/controllers/schema-designer.jsindex 07013446a27..7ec4282bb05 100644--- a/solr/webapp/web/js/angular/controllers/schema-designer.js+++ b/solr/webapp/web/js/angular/controllers/schema-designer.js@@ -15,7 +15,7 @@limitations under the License.*/-solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout, $cookies, $window, Constants, SchemaDesigner, Luke) {+solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout, $cookies, $window, Constants, SchemaDesigner, ConfigSetFiles, Luke) {$scope.resetMenu("schema-designer", Constants.IS_ROOT_PAGE);$scope.schemas = [];@@ -239,7 +239,7 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,}$scope.resetSchema();- var params = {path: "info", configSet: $scope.currentSchema};+ var params = {configSet: $scope.currentSchema};SchemaDesigner.get(params, function (data) {$scope.currentSchema = data.configSet;$("#select-schema").trigger("chosen:updated");@@ -582,7 +582,7 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,$scope.addErrors.push($scope.adding + " name is required!");}- if ($scope.newField.name.indexOf(" ") != -1) {+ if ($scope.newField.name && $scope.newField.name.indexOf(" ") != -1) {$scope.addErrors.push($scope.adding + " name should not have whitespace");}@@ -595,7 +595,8 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,}// TODO: is this the correct logic for detecting dynamic? Probably good enough for the designer- var isDynamic = $scope.newField.name.startsWith("*") || $scope.newField.name.endsWith("*");+ var isDynamic = !!$scope.newField.name &&+ ($scope.newField.name.startsWith("*") || $scope.newField.name.endsWith("*"));if (isDynamic) {if ($scope.luke && $scope.luke.dynamic_fields[$scope.newField.name]) {$scope.addErrors.push("dynamic field '" + $scope.newField.name + "' already exists!");@@ -656,7 +657,6 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,delete $scope.addErrors; // no errors!SchemaDesigner.post({- path: "add",configSet: $scope.currentSchema,schemaVersion: $scope.schemaVersion}, addData, function (data) {@@ -673,9 +673,9 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,$scope.added = false;var nodeId = "/";if ("field" === $scope.adding) {- nodeId = "field/" + data[command];+ nodeId = "field/" + ("add-dynamic-field" === command ? data.dynamicField : data.field);} else if ("type" === $scope.adding) {- nodeId = "type/" + data[command];+ nodeId = "type/" + data.fieldType;}$scope.onSchemaUpdated(data.configSet, data, nodeId);}, 500);@@ -791,7 +791,6 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,delete $scope.addCopyFieldErrors;var data = {"add-copy-field": $scope.copyField};SchemaDesigner.post({- path: "add",configSet: $scope.currentSchema,schemaVersion: $scope.schemaVersion}, data, function (data) {@@ -887,10 +886,10 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,$scope.updateWorking = true;$scope.updateStatusMessage = "Updating file ...";- SchemaDesigner.post(params, $scope.fileNodeText, function (data) {+ SchemaDesigner.put(params, $scope.fileNodeText, function (data) {if (data.updateFileError) {- if (data[$scope.selectedFile]) {- $scope.fileNodeText = data[$scope.selectedFile];+ if (data.fileContent) {+ $scope.fileNodeText = data.fileContent;}$scope.updateFileError = data.updateFileError;} else {@@ -904,9 +903,9 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,$scope.onSelectFileNode = function (id, doSelectOnTree) {$scope.selectedFile = id.startsWith("files/") ? id.substring("files/".length) : id;- var params = {path: "file", file: $scope.selectedFile, configSet: $scope.currentSchema};- SchemaDesigner.get(params, function (data) {- $scope.fileNodeText = data[$scope.selectedFile];+ var mutableId = "._designer_" + $scope.currentSchema;+ ConfigSetFiles.get({configSet: mutableId, filePath: $scope.selectedFile}, function (data) {+ $scope.fileNodeText = data.content;$scope.isLeafNode = false;if (doSelectOnTree) {delete $scope.selectedNode;@@ -1389,7 +1388,6 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,$scope.updateStatusMessage = "Updating " + $scope.selectedType + " ...";SchemaDesigner.put({- path: "update",configSet: $scope.currentSchema,schemaVersion: $scope.schemaVersion}, putData, function (data) {@@ -1522,11 +1520,13 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,};$scope.downloadConfig = function () {- // have to use an AJAX request so we can supply the Authorization header+ // Use the generic configsets download endpoint on the mutable draft+ var mutableId = "._designer_" + $scope.currentSchema;+ var downloadUrl = "/api/configsets/" + encodeURIComponent(mutableId) + "/files";if (sessionStorage.getItem("auth.header")) {var fileName = $scope.currentSchema+"_configset.zip";var xhr = new XMLHttpRequest();- xhr.open("GET", "/api/schema-designer/download/"+fileName+"?wt=raw&configSet="+$scope.currentSchema, true);+ xhr.open("GET", downloadUrl, true);xhr.setRequestHeader('Authorization', sessionStorage.getItem("auth.header"));xhr.responseType = 'blob';xhr.addEventListener('load',function() {@@ -1543,7 +1543,7 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,})xhr.send();} else {- location.href = "/api/schema-designer/download/"+$scope.currentSchema+"_configset.zip?wt=raw&configSet=" + $scope.currentSchema;+ location.href = downloadUrl;}};@@ -1834,6 +1834,11 @@ solrAdminApp.controller('SchemaDesignerController', function ($scope, $timeout,}SchemaDesigner.get(params, function (data) {+ if (data.updateError != null) {+ $scope.onError(data.updateError, data.updateErrorCode, data.errorDetails);+ return;+ }+$("#sort").trigger("chosen:updated");$("#ff").trigger("chosen:updated");$("#hl").trigger("chosen:updated");diff --git a/solr/webapp/web/js/angular/services.js b/solr/webapp/web/js/angular/services.jsindex 98e7e37d9ba..47c5ad2fa6e 100644--- a/solr/webapp/web/js/angular/services.js+++ b/solr/webapp/web/js/angular/services.js@@ -73,6 +73,23 @@ solrAdminServices.factory('System',return $resource('admin/configs', {'wt': 'json', '_': Date.now()}, {"configs": {params: {action: "LIST"}}});}])+.factory('ConfigSetFiles',+ ['$http', function ($http) {+ // Fetches a single file from a configset via V2 /api/configsets/{name}/files/{path}.+ // Each path segment is encoded separately so subdirectory paths like "lang/stopwords.txt"+ // preserve their slashes (encoding them as %2F gets rejected by Jetty).+ // transformResponse is overridden to skip JSON parsing since files are raw text.+ return {+ get: function (params, successFn, errorFn) {+ var url = "/api/configsets/" + encodeURIComponent(params.configSet) ++ "/files/" + params.filePath.split("/").map(encodeURIComponent).join("/");+ $http.get(url, {transformResponse: [function (data) { return data; }]}).then(+ function (response) { if (successFn) successFn({content: response.data}); },+ function (response) { if (errorFn) errorFn(response); }+ );+ }+ };+ }]).factory('Cores',['$resource', function($resource) {return $resource('admin/cores',@@ -271,10 +288,11 @@ solrAdminServices.factory('System',}]).factory('SchemaDesigner',['$resource', function($resource) {- return $resource('/api/schema-designer/:path', {wt: 'json', path: '@path', _:Date.now()}, {+ return $resource('/api/schema-designer/:configSet/:path', {wt: 'json', path: '@path', configSet: '@configSet', filePath: '@filePath', _:Date.now()}, {get: {method: "GET"},post: {method: "POST", timeout: 90000},put: {method: "PUT"},+ delete: {method: "DELETE"},postXml: {headers: {'Content-type': 'text/xml'}, method: "POST", timeout: 90000},postCsv: {headers: {'Content-type': 'application/csv'}, method: "POST", timeout: 90000},upload: {method: "POST", transformRequest: angular.identity, headers: {'Content-Type': undefined}, timeout: 90000}diff --git a/solr/webapp/web/partials/schema-designer.html b/solr/webapp/web/partials/schema-designer.htmlindex 63936d43437..4d7fbd5b4b3 100644--- a/solr/webapp/web/partials/schema-designer.html+++ b/solr/webapp/web/partials/schema-designer.html@@ -476,7 +476,7 @@ limitations under the License.<input type="file" id="upload-file" name="upload-file" file-model="fileUpload"/><a ng-click="showHelp('sampleDocsHelp')"><img class="help-ico" src="img/ico/question-white.png"/></a><div id="sampleDocsHelp" class="help" ng-show="helpId === 'sampleDocsHelp'"><div class="help">- <p>Upload a JSON, CSS, or XML file containing sample documents or simply paste some sample documents into the text area below; the Schema Designer supports a maximum of 5MB and 1,000 documents.+ <p>Upload a JSON, JSONL, CSV, or XML file containing sample documents or simply paste some sample documents into the text area below; the Schema Designer supports a maximum of 5MB and 1,000 documents.<br/><br/>Click on the <b>Analyze Documents</b> button to have Solr determine the schema by looking at the sample values for each field. Sample documents are stored on the server so you can make changes to the schema and Schema Designer will automatically re-index the sample documents to apply the changes.</p></div>