Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-16395: Convert v2 "schema" modification APIs to JAX-RS (#3299)

Jason Gerlowskie0cff13905f3
diff --git a/dev-docs/v2-api-conventions.adoc b/dev-docs/v2-api-conventions.adoc
index 75e2896560b..f27eae76c44 100644
--- a/dev-docs/v2-api-conventions.adoc
+++ b/dev-docs/v2-api-conventions.adoc
@@ -103,3 +103,39 @@ Please use the following conventions when implementing similar endpoints:
2. Interface methods should return a type that implements the JAX-RS `StreamingOutput` interface.
See the `fetchFile()` method in `ReplicationApis.java` for a concrete example.
+
+== Dynamic and Partially-Dynamic Request POJOs
+
+Many Solr APIs have a request body that is fully dynamic or open-ended, with users able to specify any property name and value.
+In these cases, the request-body should be represented in JAX-RS by using a map: typically a `Map<String,Object>`.
+
+Slightly more difficult are cases where the request-body allows dynamic/open-ended fields, but also has some fields that are known statically.
+In these cases, developers may:
+1. Represent the request-body using a POJO class. The "known" fields can be annotated with `@JsonProperty` as in standard POJOs. Dynamic fields can be allowed using Jackson's `@JsonAnyGetter` and `@JsonAnySetter` annotations as below:
++
+```
+ private Map<String, Object> additionalProperties = new HashMap<>();
+
+ @JsonAnyGetter
+ public Map<String, Object> getAdditionalProperties() {
+ return additionalProperties;
+ }
+
+ @JsonAnySetter
+ public void setAdditionalProperty(String field, Object value) {
+ additionalProperties.put(field, value);
+ }
+```
+2. Annotate the request-body parameter with the `ADDTL_FIELDS_PROPERTY` Swagger extension when declaring the request body. This tells our code-generation templates that the request-body takes additional properties the setters should be generated for.
++
+```
+ SolrJerseyResponse addField(
+ @PathParam("fieldName") String fieldName,
+ @RequestBody(
+ extensions = {
+ @Extension(
+ properties = {@ExtensionProperty(name = ADDTL_FIELDS_PROPERTY, value = "true")})
+ })
+ AddFieldOperation requestBody)
+ throws Exception;
+```
diff --git a/solr/api/build.gradle b/solr/api/build.gradle
index 685b561da12..d8128053623 100644
--- a/solr/api/build.gradle
+++ b/solr/api/build.gradle
@@ -70,7 +70,10 @@ dependencies {
testImplementation project(':solr:test-framework')
testImplementation project(':solr:api')
+ testImplementation libs.junit.junit
+ testImplementation libs.hamcrest.hamcrest
testImplementation libs.apache.lucene.testframework
+ testImplementation libs.fasterxml.jackson.core.databind
swaggerBuild libs.swagger3.jaxrs2.jakarta
}
diff --git a/solr/api/gradle.lockfile b/solr/api/gradle.lockfile
index 328923fd0e7..45cf5e95f35 100644
--- a/solr/api/gradle.lockfile
+++ b/solr/api/gradle.lockfile
@@ -6,9 +6,9 @@ com.carrotsearch:hppc:0.10.0=jarValidation,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-annotations:2.16.2=swaggerDeps
com.fasterxml.jackson.core:jackson-annotations:2.18.2=apiHelper,compileClasspath,jarValidation,runtimeClasspath,swaggerBuild,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.16.2=swaggerDeps
-com.fasterxml.jackson.core:jackson-core:2.18.2=jarValidation,swaggerBuild,testRuntimeClasspath
+com.fasterxml.jackson.core:jackson-core:2.18.2=jarValidation,swaggerBuild,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-databind:2.16.2=swaggerDeps
-com.fasterxml.jackson.core:jackson-databind:2.18.2=jarValidation,swaggerBuild,testRuntimeClasspath
+com.fasterxml.jackson.core:jackson-databind:2.18.2=jarValidation,swaggerBuild,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.18.2=jarValidation,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.18.2=jarValidation,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.2=swaggerDeps
diff --git a/solr/api/src/java/org/apache/solr/client/api/endpoint/UpdateSchemaApi.java b/solr/api/src/java/org/apache/solr/client/api/endpoint/UpdateSchemaApi.java
new file mode 100644
index 00000000000..5b5971928f3
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/endpoint/UpdateSchemaApi.java
@@ -0,0 +1,142 @@
+/*
+ * 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.ADDTL_FIELDS_PROPERTY;
+import static org.apache.solr.client.api.util.Constants.INDEX_PATH_PREFIX;
+
+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.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import java.util.List;
+import org.apache.solr.client.api.model.SchemaChange;
+import org.apache.solr.client.api.model.SolrJerseyResponse;
+import org.apache.solr.client.api.model.UpsertDynamicFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldTypeOperation;
+import org.apache.solr.client.api.util.StoreApiParameters;
+
+@Path(INDEX_PATH_PREFIX + "/schema")
+public interface UpdateSchemaApi {
+ @PUT
+ @Path("/fields/{fieldName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Add a standard (i.e. non-dynamic) field with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse addField(
+ @PathParam("fieldName") String fieldName,
+ @RequestBody(
+ extensions = {
+ @Extension(
+ properties = {@ExtensionProperty(name = ADDTL_FIELDS_PROPERTY, value = "true")})
+ })
+ UpsertFieldOperation requestBody)
+ throws Exception;
+
+ @DELETE
+ @Path("/fields/{fieldName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Remove the non-dynamic field with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse deleteField(@PathParam("fieldName") String fieldName) throws Exception;
+
+ @PUT
+ @Path("/dynamicfields/{dynamicFieldName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Create a new dynamic field with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse addDynamicField(
+ @PathParam("dynamicFieldName") String dynamicFieldName,
+ @RequestBody(
+ extensions = {
+ @Extension(
+ properties = {@ExtensionProperty(name = ADDTL_FIELDS_PROPERTY, value = "true")})
+ })
+ UpsertDynamicFieldOperation requestBody)
+ throws Exception;
+
+ @DELETE
+ @Path("/dynamicfields/{dynamicFieldName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Remove the dynamic field with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse deleteDynamicField(@PathParam("dynamicFieldName") String dynamicFieldName)
+ throws Exception;
+
+ @PUT
+ @Path("/fieldtypes/{fieldTypeName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Add a new field-type with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse addFieldType(
+ @PathParam("fieldTypeName") String fieldTypeName,
+ @RequestBody(
+ extensions = {
+ @Extension(
+ properties = {@ExtensionProperty(name = ADDTL_FIELDS_PROPERTY, value = "true")})
+ })
+ UpsertFieldTypeOperation requestBody)
+ throws Exception;
+
+ @DELETE
+ @Path("/fieldtypes/{fieldTypeName}")
+ @StoreApiParameters
+ @Operation(
+ summary = "Remove the field type with the specified name.",
+ tags = {"schema"})
+ SolrJerseyResponse deleteFieldType(@PathParam("fieldTypeName") String fieldTypeName)
+ throws Exception;
+
+ // TODO Gah! Copyfields don't currently have names for us to create/delete by name in an API like
+ // /schema/copyfields/{copyFieldName}...figure out how to address this in a way consistent with
+ // all our other APIs. (In the meantime, the functionality is exposed through the bulk API at
+ // least.
+ // @POST
+ // @Path("/copyfields/{copyFieldName}")
+ // @StoreApiParameters
+ // @Operation(
+ // summary = "Add a new copy-field with the specified name.",
+ // tags = {"schema"})
+ // SolrJerseyResponse addCopyField(@PathParam("copyFieldName") String copyFieldName,
+ // @RequestBody SchemaChangeOperation.AddCopyField requestBody) throws Exception;
+ //
+ // @DELETE
+ // @Path("/copyfields/{copyFieldName}")
+ // @StoreApiParameters
+ // @Operation(summary = "Remove the copy-field with the specified name.", tags = {"schema"})
+ // SolrJerseyResponse deleteCopyField(@PathParam("copyFieldName") String copyFieldName) throws
+ // Exception;
+
+ @POST
+ @Path("/bulk")
+ @StoreApiParameters
+ @Operation(
+ summary = "Perform the specified schema modifications.",
+ tags = {"schema"})
+ SolrJerseyResponse bulkSchemaModification(List<SchemaChange> requestBody) throws Exception;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/AddCopyFieldOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/AddCopyFieldOperation.java
new file mode 100644
index 00000000000..2efad0ea3aa
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/AddCopyFieldOperation.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.JsonAlias;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
+
+public class AddCopyFieldOperation extends SchemaChange {
+ @JsonProperty public String source;
+
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ @JsonAlias("dest")
+ public List<String> destinations;
+
+ @JsonProperty public Integer maxChars;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/DeleteCopyFieldOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/DeleteCopyFieldOperation.java
new file mode 100644
index 00000000000..1d1ed970b46
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/DeleteCopyFieldOperation.java
@@ -0,0 +1,30 @@
+/*
+ * 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.JsonAlias;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
+
+public class DeleteCopyFieldOperation extends SchemaChange {
+ @JsonProperty public String source;
+
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ @JsonAlias("dest")
+ public List<String> destinations;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/DeleteDynamicFieldOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/DeleteDynamicFieldOperation.java
new file mode 100644
index 00000000000..fd4c669f6d2
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/DeleteDynamicFieldOperation.java
@@ -0,0 +1,23 @@
+/*
+ * 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;
+
+public class DeleteDynamicFieldOperation extends SchemaChange {
+ @JsonProperty public String name;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldOperation.java
new file mode 100644
index 00000000000..19aa4f6bf07
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldOperation.java
@@ -0,0 +1,23 @@
+/*
+ * 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;
+
+public class DeleteFieldOperation extends SchemaChange {
+ @JsonProperty public String name;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldTypeOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldTypeOperation.java
new file mode 100644
index 00000000000..10815c2d4f5
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/DeleteFieldTypeOperation.java
@@ -0,0 +1,23 @@
+/*
+ * 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;
+
+public class DeleteFieldTypeOperation extends SchemaChange {
+ @JsonProperty public String name;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/SchemaChange.java b/solr/api/src/java/org/apache/solr/client/api/model/SchemaChange.java
new file mode 100644
index 00000000000..7a9265bd119
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/SchemaChange.java
@@ -0,0 +1,52 @@
+/*
+ * 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 com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.OptBoolean;
+
+/** Modifications that can be made to elements of a schema, either individually or in bulk. */
+@JsonTypeInfo(
+ use = JsonTypeInfo.Id.NAME,
+ property = SchemaChange.OPERATION_TYPE_PROP,
+ requireTypeIdForSubtypes = OptBoolean.FALSE,
+ visible = true)
+@JsonSubTypes({
+ @JsonSubTypes.Type(
+ value = UpsertFieldTypeOperation.class,
+ names = {"add-field-type", "replace-field-type"}),
+ @JsonSubTypes.Type(
+ value = UpsertFieldOperation.class,
+ names = {"add-field", "replace-field"}),
+ @JsonSubTypes.Type(
+ value = UpsertDynamicFieldOperation.class,
+ names = {"add-dynamic-field", "replace-dynamic-field"}),
+ @JsonSubTypes.Type(value = DeleteFieldTypeOperation.class, name = "delete-field-type"),
+ @JsonSubTypes.Type(value = DeleteFieldOperation.class, name = "delete-field"),
+ @JsonSubTypes.Type(value = DeleteDynamicFieldOperation.class, name = "delete-dynamic-field"),
+ @JsonSubTypes.Type(value = AddCopyFieldOperation.class, name = "add-copy-field"),
+ @JsonSubTypes.Type(value = DeleteCopyFieldOperation.class, name = "delete-copy-field"),
+})
+public class SchemaChange {
+
+ public static final String OPERATION_TYPE_PROP = "operationType";
+
+ @JsonProperty(OPERATION_TYPE_PROP)
+ public String operationType;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/UpsertDynamicFieldOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/UpsertDynamicFieldOperation.java
new file mode 100644
index 00000000000..db010fbacca
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/UpsertDynamicFieldOperation.java
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+public class UpsertDynamicFieldOperation extends SchemaChange {
+ @JsonProperty public String name;
+ @JsonProperty public String type;
+
+ // Used for setting index and stored settings, etc.
+ private Map<String, Object> additionalProperties = new HashMap<>();
+
+ @JsonAnyGetter
+ public Map<String, Object> getAdditionalProperties() {
+ return additionalProperties;
+ }
+
+ @JsonAnySetter
+ public void setAdditionalProperty(String field, Object value) {
+ additionalProperties.put(field, value);
+ }
+}
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/V2SchemaAPIMappingTest.java b/solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldOperation.java
similarity index 52%
rename from solr/core/src/test/org/apache/solr/handler/admin/api/V2SchemaAPIMappingTest.java
rename to solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldOperation.java
index 3b1cab9b049..c6a7d1c3c32 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/api/V2SchemaAPIMappingTest.java
+++ b/solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldOperation.java
@@ -14,33 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package org.apache.solr.client.api.model;
-package org.apache.solr.handler.admin.api;
+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;
-import org.apache.solr.handler.SchemaHandler;
-import org.apache.solr.handler.admin.V2ApiMappingTest;
-import org.junit.Test;
+public class UpsertFieldOperation extends SchemaChange {
+ @JsonProperty public String name;
-/** Unit tests for the GET and POST /v2/c/collectionName/schema APIs. */
-public class V2SchemaAPIMappingTest extends V2ApiMappingTest<SchemaHandler> {
+ @JsonProperty public String type;
- @Override
- public void populateApiBag() {
- apiBag.registerObject(new SchemaBulkModifyAPI(getRequestHandler()));
- }
-
- @Override
- public SchemaHandler createUnderlyingRequestHandler() {
- return createMock(SchemaHandler.class);
- }
+ // Used for setting index and stored settings, etc.
+ private Map<String, Object> additionalProperties = new HashMap<>();
- @Override
- public boolean isCoreSpecific() {
- return true;
+ @JsonAnyGetter
+ public Map<String, Object> getAdditionalProperties() {
+ return additionalProperties;
}
- @Test
- public void testSchemaBulkModificationApiMapping() {
- assertAnnotatedApiExistsFor("POST", "/schema");
+ @JsonAnySetter
+ public void setAdditionalProperty(String field, Object value) {
+ additionalProperties.put(field, value);
}
}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldTypeOperation.java b/solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldTypeOperation.java
new file mode 100644
index 00000000000..6a517830b2e
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/UpsertFieldTypeOperation.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.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.HashMap;
+import java.util.Map;
+
+public class UpsertFieldTypeOperation extends SchemaChange {
+ @JsonProperty public String name;
+
+ @JsonProperty("class")
+ public String className;
+
+ // Used for setting analyzers, index and stored settings, etc.
+ private Map<String, Object> additionalProperties = new HashMap<>();
+
+ @JsonAnyGetter
+ public Map<String, Object> getAdditionalProperties() {
+ return additionalProperties;
+ }
+
+ @JsonAnySetter
+ public void setAdditionalProperty(String field, Object value) {
+ additionalProperties.put(field, value);
+ }
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/util/Constants.java b/solr/api/src/java/org/apache/solr/client/api/util/Constants.java
index cff9084b549..3e3c7443c18 100644
--- a/solr/api/src/java/org/apache/solr/client/api/util/Constants.java
+++ b/solr/api/src/java/org/apache/solr/client/api/util/Constants.java
@@ -37,5 +37,14 @@ public class Constants {
public static final String GENERIC_ENTITY_PROPERTY = "genericEntity";
+ // Swagger "extension" property name used to indicate that a request/response POJO has dynamic
+ // properties (typically handled during serialization/deserialization by Jackson's
+ // @JsonAnyGetter and @JsonAnySetter).
+ //
+ // This "extension" is utilized by our SolrJ code generation, but isn't currently handled by
+ // generators for other languages, so it should be avoided where possible in favor of defining
+ // request and response fields statically.
+ public static final String ADDTL_FIELDS_PROPERTY = "hasAdditionalFields";
+
public static final String JAVABIN_CONTENT_TYPE_V2 = "application/vnd.apache.solr.javabin";
}
diff --git a/solr/api/src/test/org/apache/solr/client/api/model/SchemaChangeOperationSerializationTest.java b/solr/api/src/test/org/apache/solr/client/api/model/SchemaChangeOperationSerializationTest.java
new file mode 100644
index 00000000000..48afd83e694
--- /dev/null
+++ b/solr/api/src/test/org/apache/solr/client/api/model/SchemaChangeOperationSerializationTest.java
@@ -0,0 +1,332 @@
+/*
+ * 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 static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.instanceOf;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.Map;
+import org.apache.solr.SolrTestCase;
+import org.junit.Test;
+
+/**
+ * Unit tests ensuring that {@link SchemaChange} deserializes as intended
+ *
+ * <p>Not always necessary for model-type "serde" validation, but useful given the polymorphism at
+ * play
+ */
+@SuppressWarnings("unchecked") // The casts *are* "checked", just not in a way the compiler detects.
+public class SchemaChangeOperationSerializationTest extends SolrTestCase {
+
+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+ @Test
+ public void testAddFieldType() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "add-field-type",
+ "name": "my-new-field-type",
+ "class": "org.apache.my.ClassName",
+ "positionIncrementGap": 100,
+ "analyzer" : {
+ "charFilters":[],
+ "tokenizer":{
+ "name":"whitespace"
+ }
+ }
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertFieldTypeOperation.class));
+ final var parsedSpecific = (UpsertFieldTypeOperation) parsedGeneric;
+ assertEquals("my-new-field-type", parsedSpecific.name);
+ assertEquals("org.apache.my.ClassName", parsedSpecific.className);
+ // Arbitrary properties are put in a map, and can contain nesting
+ assertEquals(100, parsedSpecific.getAdditionalProperties().get("positionIncrementGap"));
+ assertThat(parsedSpecific.getAdditionalProperties().get("analyzer"), instanceOf(Map.class));
+ final var analyzerProperties =
+ (Map<String, Object>) parsedSpecific.getAdditionalProperties().get("analyzer");
+ assertThat(analyzerProperties.keySet(), contains("charFilters", "tokenizer"));
+ assertThat(analyzerProperties.get("tokenizer"), instanceOf(Map.class));
+ final var tokenizerProperties = (Map<String, Object>) analyzerProperties.get("tokenizer");
+ assertEquals("whitespace", tokenizerProperties.get("name"));
+ }
+
+ @Test
+ public void testAddCopyField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "add-copy-field",
+ "source": "source1",
+ "destinations": ["dest1", "dest2"],
+ "maxChars": 123
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(AddCopyFieldOperation.class));
+ final var parsedSpecific = (AddCopyFieldOperation) parsedGeneric;
+ assertEquals("source1", parsedSpecific.source);
+ assertThat(parsedSpecific.destinations, contains("dest1", "dest2"));
+ assertEquals(Integer.valueOf(123), parsedSpecific.maxChars);
+ }
+
+ // "dest" is used instead of "destinations", to support v1 APIs
+ @Test
+ public void testAddCopyFieldAltFieldName() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "add-copy-field",
+ "source": "source1",
+ "dest": ["dest1", "dest2"],
+ "maxChars": 123
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(AddCopyFieldOperation.class));
+ final var parsedSpecific = (AddCopyFieldOperation) parsedGeneric;
+ assertEquals("source1", parsedSpecific.source);
+ assertThat(parsedSpecific.destinations, contains("dest1", "dest2"));
+ assertEquals(Integer.valueOf(123), parsedSpecific.maxChars);
+ }
+
+ @Test
+ public void testAddField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "add-field",
+ "name": "my-new-field",
+ "type": "fieldType",
+ "stored": true
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertFieldOperation.class));
+ final var parsedSpecific = (UpsertFieldOperation) parsedGeneric;
+ assertEquals("my-new-field", parsedSpecific.name);
+ assertEquals("fieldType", parsedSpecific.type);
+
+ // Arbitrary properties are put in a map
+ assertEquals(Boolean.TRUE, parsedSpecific.getAdditionalProperties().get("stored"));
+ }
+
+ @Test
+ public void testAddDynamicField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "add-dynamic-field",
+ "name": "_abc",
+ "type": "fieldType",
+ "stored": true
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertDynamicFieldOperation.class));
+ final var parsedSpecific = (UpsertDynamicFieldOperation) parsedGeneric;
+ assertEquals("_abc", parsedSpecific.name);
+ assertEquals("fieldType", parsedSpecific.type);
+
+ // Arbitrary properties are put in a map
+ assertEquals(Boolean.TRUE, parsedSpecific.getAdditionalProperties().get("stored"));
+ }
+
+ @Test
+ public void testDeleteFieldType() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "delete-field-type",
+ "name": "myFieldTypeName"
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(DeleteFieldTypeOperation.class));
+ final var parsedSpecific = (DeleteFieldTypeOperation) parsedGeneric;
+ assertEquals("myFieldTypeName", parsedSpecific.name);
+ }
+
+ @Test
+ public void testDeleteCopyField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "delete-copy-field",
+ "source": "source1",
+ "destinations": ["dest1", "dest2"]
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(DeleteCopyFieldOperation.class));
+ final var parsedSpecific = (DeleteCopyFieldOperation) parsedGeneric;
+ assertEquals("source1", parsedSpecific.source);
+ assertThat(parsedSpecific.destinations, contains("dest1", "dest2"));
+ }
+
+ // "dest" is used instead of "destinations", to support v1 APIs
+ @Test
+ public void testDeleteCopyFieldAltFieldName() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "delete-copy-field",
+ "source": "source1",
+ "dest": ["dest1", "dest2"]
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(DeleteCopyFieldOperation.class));
+ final var parsedSpecific = (DeleteCopyFieldOperation) parsedGeneric;
+ assertEquals("source1", parsedSpecific.source);
+ assertThat(parsedSpecific.destinations, contains("dest1", "dest2"));
+ }
+
+ @Test
+ public void testDeleteField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "delete-field",
+ "name": "myFieldName"
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(DeleteFieldOperation.class));
+ final var parsedSpecific = (DeleteFieldOperation) parsedGeneric;
+ assertEquals("myFieldName", parsedSpecific.name);
+ }
+
+ @Test
+ public void testDeleteDynamicField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "delete-dynamic-field",
+ "name": "_abc"
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(DeleteDynamicFieldOperation.class));
+ final var parsedSpecific = (DeleteDynamicFieldOperation) parsedGeneric;
+ assertEquals("_abc", parsedSpecific.name);
+ }
+
+ @Test
+ public void testReplaceFieldType() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "replace-field-type",
+ "name": "my-new-field-type",
+ "class": "org.apache.my.ClassName",
+ "positionIncrementGap": 100,
+ "analyzer" : {
+ "charFilters":[],
+ "tokenizer":{
+ "name":"whitespace"
+ }
+ }
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertFieldTypeOperation.class));
+ final var parsedSpecific = (UpsertFieldTypeOperation) parsedGeneric;
+ assertEquals("my-new-field-type", parsedSpecific.name);
+ assertEquals("org.apache.my.ClassName", parsedSpecific.className);
+ // Arbitrary properties are put in a map, and can contain nesting
+ assertEquals(100, parsedSpecific.getAdditionalProperties().get("positionIncrementGap"));
+ assertThat(parsedSpecific.getAdditionalProperties().get("analyzer"), instanceOf(Map.class));
+ final var analyzerProperties =
+ (Map<String, Object>) parsedSpecific.getAdditionalProperties().get("analyzer");
+ assertThat(analyzerProperties.keySet(), contains("charFilters", "tokenizer"));
+ assertThat(analyzerProperties.get("tokenizer"), instanceOf(Map.class));
+ final var tokenizerProperties = (Map<String, Object>) analyzerProperties.get("tokenizer");
+ assertEquals("whitespace", tokenizerProperties.get("name"));
+ }
+
+ @Test
+ public void testReplaceField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "replace-field",
+ "name": "my-new-field",
+ "type": "fieldType",
+ "stored": true
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertFieldOperation.class));
+ final var parsedSpecific = (UpsertFieldOperation) parsedGeneric;
+ assertEquals("my-new-field", parsedSpecific.name);
+ assertEquals("fieldType", parsedSpecific.type);
+
+ // Arbitrary properties are put in a map
+ assertEquals(Boolean.TRUE, parsedSpecific.getAdditionalProperties().get("stored"));
+ }
+
+ @Test
+ public void testReplaceDynamicField() throws Exception {
+ final var inputJson =
+ """
+ {
+ "operationType": "replace-dynamic-field",
+ "name": "_abc",
+ "type": "fieldType",
+ "stored": true
+ }
+ """;
+
+ final var parsedGeneric = OBJECT_MAPPER.readValue(inputJson, SchemaChange.class);
+
+ assertThat(parsedGeneric, instanceOf(UpsertDynamicFieldOperation.class));
+ final var parsedSpecific = (UpsertDynamicFieldOperation) parsedGeneric;
+ assertEquals("_abc", parsedSpecific.name);
+ assertEquals("fieldType", parsedSpecific.type);
+
+ // Arbitrary properties are put in a map
+ assertEquals(Boolean.TRUE, parsedSpecific.getAdditionalProperties().get("stored"));
+ }
+}
diff --git a/solr/core/src/java/org/apache/solr/api/V2HttpCall.java b/solr/core/src/java/org/apache/solr/api/V2HttpCall.java
index 1ecb290fa0e..d14cd59e2ce 100644
--- a/solr/core/src/java/org/apache/solr/api/V2HttpCall.java
+++ b/solr/core/src/java/org/apache/solr/api/V2HttpCall.java
@@ -156,8 +156,9 @@ public class V2HttpCall extends HttpSolrCall {
+ this.collectionsList);
} else {
String collectionName = collectionsList.get(0);
- // Certain HTTP methods are only used for admin APIs, check for those and short-circuit
- if (List.of("delete").contains(req.getMethod().toLowerCase(Locale.ROOT))) {
+ // Short-circuit for coll-deletion, so it can happen without acquiring a 'SolrCore' ref.
+ if (List.of("delete").contains(req.getMethod().toLowerCase(Locale.ROOT))
+ && pathSegments.size() == 2) {
initAdminRequest(path);
return;
}
diff --git a/solr/core/src/java/org/apache/solr/handler/SchemaHandler.java b/solr/core/src/java/org/apache/solr/handler/SchemaHandler.java
index c81fa8eb572..b4362858daa 100644
--- a/solr/core/src/java/org/apache/solr/handler/SchemaHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/SchemaHandler.java
@@ -25,31 +25,33 @@ import static org.apache.solr.schema.IndexSchema.SchemaProps.Handler.FIELD_TYPES
import java.io.IOException;
import java.lang.invoke.MethodHandles;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import org.apache.solr.api.AnnotatedApi;
+import java.util.stream.Collectors;
import org.apache.solr.api.Api;
import org.apache.solr.api.ApiBag;
import org.apache.solr.api.JerseyResource;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.CommandOperation;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.admin.api.GetSchema;
import org.apache.solr.handler.admin.api.GetSchemaField;
-import org.apache.solr.handler.admin.api.SchemaBulkModifyAPI;
+import org.apache.solr.handler.admin.api.UpdateSchema;
import org.apache.solr.handler.api.V2ApiUtils;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.rest.RestManager;
import org.apache.solr.schema.SchemaManager;
+import org.apache.solr.schema.SchemaManagerUtils;
import org.apache.solr.security.AuthorizationContext;
import org.apache.solr.security.PermissionNameProvider;
import org.apache.solr.util.plugin.SolrCoreAware;
@@ -87,10 +89,22 @@ public class SchemaHandler extends RequestHandlerBase
}
try {
- List<Map<String, Object>> errs = new SchemaManager(req).performOperations();
- if (!errs.isEmpty())
+ List<CommandOperation> ops = req.getCommands(false);
+ List<Map<String, Object>> errs = CommandOperation.captureErrors(ops);
+ if (!errs.isEmpty()) {
throw new ApiBag.ExceptionWithErrObject(
SolrException.ErrorCode.BAD_REQUEST, "error processing commands", errs);
+ }
+
+ final var operationList =
+ ops.stream()
+ .map(co -> SchemaManagerUtils.convertToSchemaChangeOperations(co))
+ .collect(Collectors.toList());
+ errs = new SchemaManager(req).performOperations(operationList);
+ if (!errs.isEmpty()) {
+ throw new ApiBag.ExceptionWithErrObject(
+ SolrException.ErrorCode.BAD_REQUEST, "error processing commands", errs);
+ }
} catch (IOException e) {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST, "Error reading input String " + e.getMessage(), e);
@@ -285,16 +299,12 @@ public class SchemaHandler extends RequestHandlerBase
@Override
public Collection<Api> getApis() {
-
- final List<Api> apis = new ArrayList<>();
- apis.addAll(AnnotatedApi.getApis(new SchemaBulkModifyAPI(this)));
-
- return apis;
+ return Collections.EMPTY_LIST;
}
@Override
public Collection<Class<? extends JerseyResource>> getJerseyResources() {
- return List.of(GetSchema.class, GetSchemaField.class);
+ return List.of(GetSchema.class, GetSchemaField.class, UpdateSchema.class);
}
@Override
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/SchemaBulkModifyAPI.java b/solr/core/src/java/org/apache/solr/handler/admin/api/SchemaBulkModifyAPI.java
deleted file mode 100644
index 22b82bf3960..00000000000
--- a/solr/core/src/java/org/apache/solr/handler/admin/api/SchemaBulkModifyAPI.java
+++ /dev/null
@@ -1,57 +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.admin.api;
-
-import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;
-
-import org.apache.solr.api.EndPoint;
-import org.apache.solr.handler.SchemaHandler;
-import org.apache.solr.request.SolrQueryRequest;
-import org.apache.solr.response.SolrQueryResponse;
-import org.apache.solr.security.PermissionNameProvider;
-
-/**
- * V2 APIs for creating, updating, or deleting individual components in a collection's schema.
- *
- * <p>This API (POST /v2/collections/collectionName/schema {...}) is analogous to the commands
- * supported by the v1 POST /collectionName/schema API.
- *
- * <p>Typically v2 "POST" API implementations separate each "command" into a different method. This
- * is not done here because the v1 API code in SchemaHandler expects to consume the request body
- * itself (meaning that nothing _before_ SchemaHandler can consume the request body).
- *
- * <p>As a result the single method below handles all "commands" supported by the POST /schema API:
- * 'add-field', 'delete-field', 'replace-field', 'add-dynamic-field', 'delete-dynamic-field',
- * 'replace-dynamic-field', 'add-field-type', 'delete-field-type', 'replace-field-type',
- * 'add-copy-field', and 'delete-copy-field'.
- */
-public class SchemaBulkModifyAPI {
- private final SchemaHandler schemaHandler;
-
- public SchemaBulkModifyAPI(SchemaHandler schemaHandler) {
- this.schemaHandler = schemaHandler;
- }
-
- @EndPoint(
- path = {"/schema"},
- method = POST,
- permission = PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
- public void bulkModifySchema(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
- schemaHandler.handleRequestBody(req, rsp);
- }
-}
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/UpdateSchema.java b/solr/core/src/java/org/apache/solr/handler/admin/api/UpdateSchema.java
new file mode 100644
index 00000000000..1bb3f51c4c3
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/UpdateSchema.java
@@ -0,0 +1,179 @@
+/*
+ * 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.admin.api;
+
+import jakarta.inject.Inject;
+import java.util.List;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.client.api.endpoint.UpdateSchemaApi;
+import org.apache.solr.client.api.model.DeleteDynamicFieldOperation;
+import org.apache.solr.client.api.model.DeleteFieldOperation;
+import org.apache.solr.client.api.model.DeleteFieldTypeOperation;
+import org.apache.solr.client.api.model.ErrorInfo;
+import org.apache.solr.client.api.model.SchemaChange;
+import org.apache.solr.client.api.model.SolrJerseyResponse;
+import org.apache.solr.client.api.model.UpsertDynamicFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldTypeOperation;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.handler.SolrConfigHandler;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.schema.SchemaManager;
+import org.apache.solr.security.PermissionNameProvider;
+
+public class UpdateSchema extends JerseyResource implements UpdateSchemaApi {
+
+ private final SolrCore solrCore;
+ private final SolrQueryRequest solrQueryRequest;
+
+ @Inject
+ public UpdateSchema(SolrCore solrCore, SolrQueryRequest solrQueryRequest) {
+ this.solrCore = solrCore;
+ this.solrQueryRequest = solrQueryRequest;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse addField(String fieldName, UpsertFieldOperation requestBody)
+ throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredParameterProvided("fieldName", fieldName);
+ ensureRequiredRequestBodyProvided(requestBody);
+ ensureRequiredParameterProvided("type", requestBody.type);
+ requestBody.name = fieldName;
+ requestBody.operationType = "upsert-field";
+
+ runWithSchemaManager(List.of(requestBody), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse deleteField(String fieldName) throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredParameterProvided("fieldName", fieldName);
+
+ // Format operation as a 'SchemaChangeOperation' so it can be processed by SchemaManager
+ final var deleteFieldOp = new DeleteFieldOperation();
+ deleteFieldOp.operationType = "delete-field";
+ deleteFieldOp.name = fieldName;
+
+ runWithSchemaManager(List.of(deleteFieldOp), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse addDynamicField(
+ String dynamicFieldName, UpsertDynamicFieldOperation requestBody) throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredRequestBodyProvided(requestBody);
+ ensureRequiredParameterProvided("dynamicFieldName", dynamicFieldName);
+ ensureRequiredParameterProvided("type", requestBody.type);
+ requestBody.name = dynamicFieldName;
+ requestBody.operationType = "add-dynamic-field";
+
+ runWithSchemaManager(List.of(requestBody), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse deleteDynamicField(String dynamicFieldName) throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredParameterProvided("dynamicFieldName", dynamicFieldName);
+
+ final var deleteDynamicFieldOp = new DeleteDynamicFieldOperation();
+ deleteDynamicFieldOp.name = dynamicFieldName;
+ deleteDynamicFieldOp.operationType = "delete-dynamic-field";
+ runWithSchemaManager(List.of(deleteDynamicFieldOp), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse addFieldType(String fieldTypeName, UpsertFieldTypeOperation requestBody)
+ throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredRequestBodyProvided(requestBody);
+ ensureRequiredParameterProvided("fieldTypeName", fieldTypeName);
+ ensureRequiredParameterProvided("class", requestBody.className);
+ requestBody.operationType = "add-field-type";
+
+ runWithSchemaManager(List.of(requestBody), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse deleteFieldType(String fieldTypeName) throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredParameterProvided("fieldTypeName", fieldTypeName);
+
+ final var deleteFieldTypeOp = new DeleteFieldTypeOperation();
+ deleteFieldTypeOp.name = fieldTypeName;
+ deleteFieldTypeOp.operationType = "delete-field-type";
+
+ runWithSchemaManager(List.of(deleteFieldTypeOp), response);
+
+ return response;
+ }
+
+ @Override
+ @PermissionName(PermissionNameProvider.Name.SCHEMA_EDIT_PERM)
+ public SolrJerseyResponse bulkSchemaModification(List<SchemaChange> requestBody)
+ throws Exception {
+ final var response = instantiateJerseyResponse(SolrJerseyResponse.class);
+ ensureSchemaMutable();
+ ensureRequiredRequestBodyProvided(requestBody);
+
+ runWithSchemaManager(requestBody, response);
+
+ return response;
+ }
+
+ private void ensureSchemaMutable() {
+ if (SolrConfigHandler.getImmutable(solrCore)) {
+ throw new SolrException(
+ SolrException.ErrorCode.BAD_REQUEST,
+ "ConfigSet is immutable; schema modifications not allowed/enabled");
+ }
+ }
+
+ private void runWithSchemaManager(List<SchemaChange> operations, SolrJerseyResponse response)
+ throws Exception {
+ final var schemaManager = new SchemaManager(solrQueryRequest);
+ final var errorDetails = schemaManager.performOperations(operations);
+ if (errorDetails != null && !errorDetails.isEmpty()) {
+ response.error = new ErrorInfo();
+ response.error.details = errorDetails;
+ }
+ }
+}
diff --git a/solr/core/src/java/org/apache/solr/schema/SchemaManager.java b/solr/core/src/java/org/apache/solr/schema/SchemaManager.java
index b30293efb1a..62c7434bb0f 100644
--- a/solr/core/src/java/org/apache/solr/schema/SchemaManager.java
+++ b/solr/core/src/java/org/apache/solr/schema/SchemaManager.java
@@ -19,23 +19,35 @@ package org.apache.solr.schema;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
-import static org.apache.solr.schema.FieldType.CLASS_NAME;
-import static org.apache.solr.schema.IndexSchema.DESTINATION;
+import static org.apache.solr.client.api.model.SchemaChange.OPERATION_TYPE_PROP;
+import static org.apache.solr.common.util.CommandOperation.ERR_MSGS;
import static org.apache.solr.schema.IndexSchema.MAX_CHARS;
import static org.apache.solr.schema.IndexSchema.NAME;
-import static org.apache.solr.schema.IndexSchema.SOURCE;
import static org.apache.solr.schema.IndexSchema.TYPE;
+import static org.apache.solr.schema.SchemaManagerUtils.convertToMap;
+import static org.apache.solr.schema.SchemaManagerUtils.convertToMapExcluding;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.api.model.AddCopyFieldOperation;
+import org.apache.solr.client.api.model.DeleteCopyFieldOperation;
+import org.apache.solr.client.api.model.DeleteDynamicFieldOperation;
+import org.apache.solr.client.api.model.DeleteFieldOperation;
+import org.apache.solr.client.api.model.DeleteFieldTypeOperation;
+import org.apache.solr.client.api.model.SchemaChange;
+import org.apache.solr.client.api.model.UpsertDynamicFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldTypeOperation;
import org.apache.solr.cloud.ZkController;
import org.apache.solr.cloud.ZkSolrResourceLoader;
import org.apache.solr.common.SolrException;
@@ -79,45 +91,46 @@ public class SchemaManager {
}
}
+ // TODO Replace the error-list used here with something more intuitive?
/**
- * Take in a JSON command set and execute them. It tries to capture as many errors as possible
- * instead of failing at the first error it encounters
+ * Take in a set of schema commands and execute them. It tries to capture as many errors as
+ * possible instead of failing at the first error it encounters
*
* @return List of errors. If the List is empty then the operation was successful.
*/
- public List<Map<String, Object>> performOperations() throws Exception {
- List<CommandOperation> ops = req.getCommands(false);
- List<Map<String, Object>> errs = CommandOperation.captureErrors(ops);
- if (!errs.isEmpty()) return errs;
-
+ public List<Map<String, Object>> performOperations(List<SchemaChange> ops) throws Exception {
IndexSchema schema = req.getCore().getLatestSchema();
if (schema instanceof ManagedIndexSchema && schema.isMutable()) {
return doOperations(ops);
} else {
- return singletonList(singletonMap(CommandOperation.ERR_MSGS, "schema is not editable"));
+ return singletonList(singletonMap(ERR_MSGS, "schema is not editable"));
}
}
- private List<Map<String, Object>> doOperations(List<CommandOperation> operations)
+ private List<Map<String, Object>> doOperations(List<SchemaChange> operations)
throws InterruptedException, IOException, KeeperException {
TimeOut timeOut = new TimeOut(updateTimeOut, TimeUnit.SECONDS, TimeSource.NANO_TIME);
SolrCore core = req.getCore();
String errorMsg = "Unable to persist managed schema. ";
- List<Map<String, Object>> errors = Collections.emptyList();
+ List<Map<String, Object>> errors = new ArrayList<>();
int latestVersion = -1;
synchronized (req.getSchema().getSchemaUpdateLock()) {
while (!timeOut.hasTimedOut()) {
managedIndexSchema = getFreshManagedSchema(req.getCore());
- for (CommandOperation op : operations) {
- OpType opType = OpType.get(op.name);
- if (opType != null) {
+ for (SchemaChange op : operations) {
+ OpType opType = OpType.get(op.operationType);
+ try {
opType.perform(op, this);
- } else {
- op.addError("No such operation : " + op.name);
+ } catch (SchemaOperationException soe) {
+ errors.add(
+ Map.of(
+ op.operationType,
+ SchemaManagerUtils.convertToMap(op),
+ ERR_MSGS,
+ singletonList(soe.getMessage())));
}
}
- errors = CommandOperation.captureErrors(operations);
if (!errors.isEmpty()) break;
SolrResourceLoader loader = req.getCore().getResourceLoader();
if (loader instanceof ZkSolrResourceLoader zkLoader) {
@@ -202,257 +215,277 @@ public class SchemaManager {
}
}
+ /**
+ * Represents an error encountered while processing a schema-change operation
+ *
+ * <p>Does not necessarily terminate schema modification, in the case of bulk operations.
+ */
+ private static class SchemaOperationException extends Exception {
+ public SchemaOperationException() {
+ super();
+ }
+
+ public SchemaOperationException(String message) {
+ super(message);
+ }
+ }
+
+ private static <T> T ensureNotNull(String name, T value) throws SchemaOperationException {
+ if (value == null) {
+ throw new SchemaOperationException("'" + name + "' is a required field");
+ }
+
+ return value;
+ }
+
public enum OpType {
ADD_FIELD_TYPE("add-field-type") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String className = op.getStr(CLASS_NAME);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addFieldTypeOp = (UpsertFieldTypeOperation) op;
+ String name = ensureNotNull("name", addFieldTypeOp.name);
+ String className = ensureNotNull("class", addFieldTypeOp.className);
try {
FieldType fieldType =
- mgr.managedIndexSchema.newFieldType(name, className, op.getDataMap());
+ mgr.managedIndexSchema.newFieldType(name, className, convertToMap(addFieldTypeOp));
mgr.managedIndexSchema =
mgr.managedIndexSchema.addFieldTypes(singletonList(fieldType), false);
return true;
} catch (Exception e) {
log.error("Could not add field type.", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
ADD_COPY_FIELD("add-copy-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String src = op.getStr(SOURCE);
- List<String> dests = op.getStrs(DESTINATION);
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addCopyFieldOp = (AddCopyFieldOperation) op;
+ String src = ensureNotNull("source", addCopyFieldOp.source);
+ List<String> dests = ensureNotNull("dest", addCopyFieldOp.destinations);
// If maxChars is not specified, there is no limit on copied chars
- int maxChars = CopyField.UNLIMITED;
- String maxCharsStr = op.getStr(MAX_CHARS, null);
- if (null != maxCharsStr) {
- try {
- maxChars = Integer.parseInt(maxCharsStr);
- } catch (NumberFormatException e) {
- op.addError(
- "Exception parsing " + MAX_CHARS + " '" + maxCharsStr + "': " + getErrorStr(e));
- }
- if (maxChars < 0) {
- op.addError(MAX_CHARS + " '" + maxCharsStr + "' is negative.");
- }
+ final var maxChars =
+ addCopyFieldOp.maxChars == null ? CopyField.UNLIMITED : addCopyFieldOp.maxChars;
+ if (maxChars < 0) {
+ throw new SchemaOperationException(MAX_CHARS + " '" + maxChars + "' is negative.");
}
- if (op.hasError()) return false;
- if (!op.getValuesExcluding(SOURCE, DESTINATION, MAX_CHARS).isEmpty()) {
- op.addError(
- "Only the '"
- + SOURCE
- + "', '"
- + DESTINATION
- + "' and '"
- + MAX_CHARS
- + "' params are allowed with the 'add-copy-field' operation");
- return false;
- }
try {
mgr.managedIndexSchema = mgr.managedIndexSchema.addCopyFields(src, dests, maxChars);
return true;
} catch (Exception e) {
log.error("Could not add copy field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
+ }
+ }
+ },
+ UPSERT_FIELD("upsert-field") { // Internal only
+ @Override
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addFieldOp = (UpsertFieldOperation) op;
+ String name = ensureNotNull("name", addFieldOp.name);
+ String type = ensureNotNull("type", addFieldOp.type);
+
+ if (mgr.managedIndexSchema.fields.containsKey(addFieldOp.name)) {
+ return OpType.REPLACE_FIELD.perform(op, mgr);
+ } else {
+ return OpType.ADD_FIELD.perform(op, mgr);
}
}
},
ADD_FIELD("add-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String type = op.getStr(TYPE);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addFieldOp = (UpsertFieldOperation) op;
+ String name = ensureNotNull("name", addFieldOp.name);
+ String type = ensureNotNull("type", addFieldOp.type);
try {
+ final String[] propsToOmit = new String[] {OPERATION_TYPE_PROP, NAME, TYPE};
SchemaField field =
- mgr.managedIndexSchema.newField(name, type, op.getValuesExcluding(NAME, TYPE));
+ mgr.managedIndexSchema.newField(
+ name, type, convertToMapExcluding(addFieldOp, propsToOmit));
mgr.managedIndexSchema =
mgr.managedIndexSchema.addFields(singletonList(field), Collections.emptyMap(), false);
return true;
} catch (Exception e) {
log.error("Could not add field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
+ }
+ }
+ },
+ UPSERT_DYNAMIC_FIELD("upsert-dynamic-field") { // Internal only
+ @Override
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addDynFieldOp = (UpsertDynamicFieldOperation) op;
+ String name = ensureNotNull("name", addDynFieldOp.name);
+
+ final boolean fieldExists =
+ Arrays.stream(mgr.managedIndexSchema.dynamicFields)
+ .anyMatch(df -> df.getRegex().equals(name));
+ if (fieldExists) {
+ return OpType.REPLACE_DYNAMIC_FIELD.perform(op, mgr);
+ } else {
+ return OpType.ADD_DYNAMIC_FIELD.perform(op, mgr);
}
}
},
ADD_DYNAMIC_FIELD("add-dynamic-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String type = op.getStr(TYPE);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var addDynFieldOp = (UpsertDynamicFieldOperation) op;
+ String name = ensureNotNull("name", addDynFieldOp.name);
+ String type = ensureNotNull("type", addDynFieldOp.type);
try {
+ final String[] propsToOmit = new String[] {OPERATION_TYPE_PROP, NAME, TYPE};
SchemaField field =
- mgr.managedIndexSchema.newDynamicField(name, type, op.getValuesExcluding(NAME, TYPE));
+ mgr.managedIndexSchema.newDynamicField(
+ name, type, convertToMapExcluding(addDynFieldOp, propsToOmit));
mgr.managedIndexSchema =
mgr.managedIndexSchema.addDynamicFields(
singletonList(field), Collections.emptyMap(), false);
return true;
} catch (Exception e) {
log.error("Could not add dynamic field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
DELETE_FIELD_TYPE("delete-field-type") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- if (op.hasError()) return false;
- if (!op.getValuesExcluding(NAME).isEmpty()) {
- op.addError(
- "Only the '" + NAME + "' param is allowed with the 'delete-field-type' operation");
- return false;
- }
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var deleteFieldTypeOp = (DeleteFieldTypeOperation) op;
+ String name = ensureNotNull("name", deleteFieldTypeOp.name);
try {
mgr.managedIndexSchema = mgr.managedIndexSchema.deleteFieldTypes(singleton(name));
return true;
} catch (Exception e) {
log.error("Could not delete field type", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
DELETE_COPY_FIELD("delete-copy-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String source = op.getStr(SOURCE);
- List<String> dests = op.getStrs(DESTINATION);
- if (op.hasError()) return false;
- if (!op.getValuesExcluding(SOURCE, DESTINATION).isEmpty()) {
- op.addError(
- "Only the '"
- + SOURCE
- + "' and '"
- + DESTINATION
- + "' params are allowed with the 'delete-copy-field' operation");
- return false;
- }
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var deleteCopyField = (DeleteCopyFieldOperation) op;
+ String source = ensureNotNull("source", deleteCopyField.source);
+ List<String> dests = ensureNotNull("dest", deleteCopyField.destinations);
try {
mgr.managedIndexSchema =
mgr.managedIndexSchema.deleteCopyFields(singletonMap(source, dests));
return true;
} catch (Exception e) {
log.error("Could not delete copy field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
DELETE_FIELD("delete-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- if (op.hasError()) return false;
- if (!op.getValuesExcluding(NAME).isEmpty()) {
- op.addError("Only the '" + NAME + "' param is allowed with the 'delete-field' operation");
- return false;
- }
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var deleteFieldOp = (DeleteFieldOperation) op;
+ String name = ensureNotNull("name", deleteFieldOp.name);
try {
mgr.managedIndexSchema = mgr.managedIndexSchema.deleteFields(singleton(name));
return true;
} catch (Exception e) {
log.error("Could not delete field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
DELETE_DYNAMIC_FIELD("delete-dynamic-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- if (op.hasError()) return false;
- if (!op.getValuesExcluding(NAME).isEmpty()) {
- op.addError(
- "Only the '" + NAME + "' param is allowed with the 'delete-dynamic-field' operation");
- return false;
- }
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var deleteDynFieldOp = (DeleteDynamicFieldOperation) op;
+ String name = ensureNotNull("name", deleteDynFieldOp.name);
try {
mgr.managedIndexSchema = mgr.managedIndexSchema.deleteDynamicFields(singleton(name));
return true;
} catch (Exception e) {
log.error("Could not delete dynamic field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
+ }
+ }
+ },
+ UPSERT_FIELD_TYPE("upsert-field-type") { // Internal only, to support v2 API
+ @Override
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var upsertFieldTypeOp = (UpsertFieldTypeOperation) op;
+ String name = ensureNotNull("name", upsertFieldTypeOp.name);
+
+ if (mgr.managedIndexSchema.fieldTypes.containsKey(name)) {
+ return OpType.REPLACE_FIELD_TYPE.perform(op, mgr);
+ } else {
+ return OpType.ADD_FIELD_TYPE.perform(op, mgr);
}
}
},
REPLACE_FIELD_TYPE("replace-field-type") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String className = op.getStr(CLASS_NAME);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var replaceFieldTypeOp = (UpsertFieldTypeOperation) op;
+ String name = ensureNotNull("name", replaceFieldTypeOp.name);
+ String className = ensureNotNull("class", replaceFieldTypeOp.className);
try {
mgr.managedIndexSchema =
- mgr.managedIndexSchema.replaceFieldType(name, className, op.getDataMap());
+ mgr.managedIndexSchema.replaceFieldType(
+ name, className, convertToMap(replaceFieldTypeOp));
return true;
} catch (Exception e) {
log.error("Could not replace field type", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
REPLACE_FIELD("replace-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String type = op.getStr(TYPE);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var replaceFieldOp = (UpsertFieldOperation) op;
+ String name = ensureNotNull("name", replaceFieldOp.name);
+ String type = ensureNotNull("type", replaceFieldOp.type);
FieldType ft = mgr.managedIndexSchema.getFieldTypeByName(type);
if (ft == null) {
- op.addError("No such field type '" + type + "'");
- return false;
+ throw new SchemaOperationException("No such field type '" + type + "'");
}
try {
+ final String[] propsToOmit = new String[] {OPERATION_TYPE_PROP, NAME, TYPE};
mgr.managedIndexSchema =
- mgr.managedIndexSchema.replaceField(name, ft, op.getValuesExcluding(NAME, TYPE));
+ mgr.managedIndexSchema.replaceField(
+ name, ft, convertToMapExcluding(replaceFieldOp, propsToOmit));
return true;
} catch (Exception e) {
log.error("Could not replace field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
},
REPLACE_DYNAMIC_FIELD("replace-dynamic-field") {
@Override
- public boolean perform(CommandOperation op, SchemaManager mgr) {
- String name = op.getStr(NAME);
- String type = op.getStr(TYPE);
- if (op.hasError()) return false;
+ public boolean perform(SchemaChange op, SchemaManager mgr) throws SchemaOperationException {
+ final var replaceDynFieldOp = (UpsertDynamicFieldOperation) op;
+ String name = ensureNotNull("name", replaceDynFieldOp.name);
+ String type = ensureNotNull("type", replaceDynFieldOp.type);
FieldType ft = mgr.managedIndexSchema.getFieldTypeByName(type);
if (ft == null) {
- op.addError("No such field type '" + type + "'");
- return false;
+ throw new SchemaOperationException("No such field type '" + type + "'");
}
try {
+ final String[] propsToOmit = new String[] {OPERATION_TYPE_PROP, NAME, TYPE};
mgr.managedIndexSchema =
mgr.managedIndexSchema.replaceDynamicField(
- name, ft, op.getValuesExcluding(NAME, TYPE));
+ name, ft, convertToMapExcluding(replaceDynFieldOp, propsToOmit));
return true;
} catch (Exception e) {
log.error("Could not replace dynamic field", e);
- op.addError(getErrorStr(e));
- return false;
+ throw new SchemaOperationException(getErrorStr(e));
}
}
};
- public abstract boolean perform(CommandOperation op, SchemaManager mgr);
+ public abstract boolean perform(SchemaChange op, SchemaManager mgr)
+ throws SchemaOperationException;
public static OpType get(String label) {
return Nested.OP_TYPES.get(label);
diff --git a/solr/core/src/java/org/apache/solr/schema/SchemaManagerUtils.java b/solr/core/src/java/org/apache/solr/schema/SchemaManagerUtils.java
new file mode 100644
index 00000000000..627b46cacce
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/schema/SchemaManagerUtils.java
@@ -0,0 +1,60 @@
+/*
+ * 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.schema;
+
+import static org.apache.solr.client.api.model.SchemaChange.OPERATION_TYPE_PROP;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.Map;
+import org.apache.solr.client.api.model.SchemaChange;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.CommandOperation;
+import org.apache.solr.jersey.SolrJacksonMapper;
+
+public class SchemaManagerUtils {
+ public static SchemaChange convertToSchemaChangeOperations(CommandOperation toConvert) {
+ final var opName = toConvert.name;
+ if (SchemaManager.OpType.get(opName) == null) {
+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No such operation: " + opName);
+ }
+
+ final var operationData = toConvert.getDataMap();
+ operationData.put(OPERATION_TYPE_PROP, opName);
+
+ return SolrJacksonMapper.getObjectMapper().convertValue(operationData, SchemaChange.class);
+ }
+
+ // TODO This utility exists because ManagedIndexSchema currently consumes op data in "Map" form.
+ // This should be switched over to using SchemaChangeOperation, and this utility removed.
+ public static Map<String, Object> convertToMap(SchemaChange toConvert) {
+
+ return convertToMapExcluding(toConvert, OPERATION_TYPE_PROP);
+ }
+
+ public static Map<String, Object> convertToMapExcluding(
+ SchemaChange toConvert, String... propsToOmit) {
+ final var opMap =
+ SolrJacksonMapper.getObjectMapper()
+ .convertValue(toConvert, new TypeReference<Map<String, Object>>() {});
+ if (propsToOmit != null) {
+ for (String prop : propsToOmit) {
+ opMap.remove(prop);
+ }
+ }
+ return opMap;
+ }
+}
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java b/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java
index d1c13ae73b4..fb768120010 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java
@@ -120,15 +120,6 @@ public class TestApiFramework extends SolrTestCaseJ4 {
methodNames.add(rsp.getValues()._getStr("/spec[1]/methods[0]", null));
methodNames.add(rsp.getValues()._getStr("/spec[2]/methods[0]", null));
assertTrue(methodNames.contains("POST"));
-
- methodNames = new HashSet<>();
-
- rsp =
- invoke(
- coreHandlers, "/schema/_introspect", "/collections/hello/schema/_introspect", mockCC);
- methodNames.add(rsp.getValues()._getStr("/spec[0]/methods[0]", null));
- methodNames.add(rsp.getValues()._getStr("/spec[1]/methods[0]", null));
- assertTrue(methodNames.contains("POST"));
}
public void testPayload() {
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/V2UpdateSchemaErrorCaseTests.java b/solr/core/src/test/org/apache/solr/handler/admin/api/V2UpdateSchemaErrorCaseTests.java
new file mode 100644
index 00000000000..54d063dc31e
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/handler/admin/api/V2UpdateSchemaErrorCaseTests.java
@@ -0,0 +1,158 @@
+/*
+ * 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.admin.api;
+
+import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito;
+import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
+import static org.hamcrest.Matchers.containsString;
+import static org.mockito.Mockito.mock;
+
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.client.api.model.UpsertDynamicFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldOperation;
+import org.apache.solr.client.api.model.UpsertFieldTypeOperation;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.request.SolrQueryRequest;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class V2UpdateSchemaErrorCaseTests extends SolrTestCase {
+
+ private UpdateSchema schemaApi;
+
+ @BeforeClass
+ public static void ensureWorkingMockito() {
+ assumeWorkingMockito();
+ }
+
+ @Before
+ public void setUpMocks() {
+ final var core = mock(SolrCore.class);
+ final var solrQueryRequest = mock(SolrQueryRequest.class);
+ schemaApi = new UpdateSchema(core, solrQueryRequest);
+ }
+
+ @Test
+ public void testAddFieldOperationRequiresFieldNameAndType() {
+ final var noFieldOp = new UpsertFieldOperation();
+ noFieldOp.type = "text_general";
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addField(null, noFieldOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: fieldName"));
+
+ final var noTypeOp = new UpsertFieldOperation();
+ thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addField("someFieldName", noTypeOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: type"));
+ }
+
+ @Test
+ public void testDeleteFieldOperationRequiresFieldName() {
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.deleteField(null);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: fieldName"));
+ }
+
+ @Test
+ public void testAddDynamicFieldOperationRequiresFieldNameAndType() {
+ final var noFieldOp = new UpsertDynamicFieldOperation();
+ noFieldOp.type = "text_general";
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addDynamicField(null, noFieldOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: dynamicFieldName"));
+
+ final var noTypeOp = new UpsertDynamicFieldOperation();
+ thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addDynamicField("someFieldName", noTypeOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: type"));
+ }
+
+ @Test
+ public void testDeleteDynamicFieldOperationRequiresFieldName() {
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.deleteDynamicField(null);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: dynamicFieldName"));
+ }
+
+ @Test
+ public void testAddFieldTypeOperationRequiresTypeNameAndClass() {
+ final var noTypeOp = new UpsertFieldTypeOperation();
+ noTypeOp.className = "solr.TextField";
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addFieldType(null, noTypeOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: fieldTypeName"));
+
+ final var noClassOp = new UpsertFieldTypeOperation();
+ thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.addFieldType("someFieldName", noClassOp);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: class"));
+ }
+
+ @Test
+ public void testDeleteFieldTypeOperationRequiresTypeName() {
+ var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ schemaApi.deleteFieldType(null);
+ });
+ assertEquals(BAD_REQUEST.code, thrown.code());
+ assertThat(thrown.getMessage(), containsString("Missing required parameter: fieldTypeName"));
+ }
+}
diff --git a/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java b/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java
index 57f69a34ec5..446665aa236 100644
--- a/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java
+++ b/solr/core/src/test/org/apache/solr/rest/schema/TestBulkSchemaAPI.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
@@ -44,7 +45,6 @@ import org.apache.solr.schema.AbstractSpatialPrefixTreeFieldType;
import org.apache.solr.schema.SimilarityFactory;
import org.apache.solr.search.similarities.SchemaSimilarityFactory;
import org.apache.solr.util.LogListener;
-import org.apache.solr.util.RESTfulServerProvider;
import org.apache.solr.util.RestTestBase;
import org.apache.solr.util.RestTestHarness;
import org.junit.After;
@@ -66,16 +66,6 @@ public class TestBulkSchemaAPI extends RestTestBase {
createJettyAndHarness(
tmpSolrHome, "solrconfig-managed-schema.xml", "schema-rest.xml", "/solr", true, null);
- if (random().nextBoolean()) {
- log.info("These tests are run with V2 API");
- restTestHarness.setServerProvider(
- new RESTfulServerProvider() {
- @Override
- public String getBaseURL() {
- return getBaseUrl() + "/____v2/cores/" + DEFAULT_TEST_CORENAME;
- }
- });
- }
}
@After
@@ -126,7 +116,6 @@ public class TestBulkSchemaAPI extends RestTestBase {
}
public void testAnalyzerClass() throws Exception {
-
String addFieldTypeAnalyzerWithClass =
"{\n"
+ "'add-field-type' : {"
@@ -856,14 +845,14 @@ public class TestBulkSchemaAPI extends RestTestBase {
assertTrue("'bleh_s' copyField rule exists in the schema", l.isEmpty());
payload =
- "{\n"
- + " 'add-copy-field' : {\n"
- + " 'source' :'bleh_s',\n"
- + " 'dest':'"
- + newFieldName
- + "'\n"
- + " }\n"
- + " }\n";
+ """
+ {
+ "add-copy-field": {
+ "source": "bleh_s",
+ "dest": "%s"
+ }
+ }""";
+ payload = String.format(Locale.ROOT, payload, newFieldName);
response = harness.post("/schema", json(payload));
map = (Map) fromJSONString(response);
diff --git a/solr/core/src/test/org/apache/solr/schema/SchemaApiFailureTest.java b/solr/core/src/test/org/apache/solr/schema/SchemaApiFailureTest.java
index 9e7717ffd6a..c1e5a64e03b 100644
--- a/solr/core/src/test/org/apache/solr/schema/SchemaApiFailureTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/SchemaApiFailureTest.java
@@ -17,16 +17,17 @@
package org.apache.solr.schema;
+import static org.hamcrest.Matchers.containsStringIgnoringCase;
+
import java.util.Map;
import java.util.concurrent.TimeUnit;
-import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
+import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.cloud.DocCollection;
-import org.apache.solr.common.util.Utils;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -55,15 +56,13 @@ public class SchemaApiFailureTest extends SolrCloudTestCase {
new SchemaRequest.AddField(Map.of("name", "myfield", "type", "string"));
SchemaResponse.UpdateResponse updateResponse = fieldAddition.process(client, COLLECTION);
- BaseHttpSolrClient.RemoteExecutionException ex =
+ final var thrown =
expectThrows(
- BaseHttpSolrClient.RemoteExecutionException.class,
- () -> fieldAddition.process(client, COLLECTION));
+ SolrClient.RemoteSolrException.class,
+ () -> {
+ fieldAddition.process(client, COLLECTION);
+ });
- assertTrue(
- "expected error message 'Field 'myfield' already exists'.",
- Utils.getObjectByPath(ex.getMetaData(), false, "error/details[0]/errorMessages[0]")
- .toString()
- .contains("Field 'myfield' already exists."));
+ assertThat(thrown.getMessage(), containsStringIgnoringCase("Field 'myfield' already exists."));
}
}
diff --git a/solr/core/src/test/org/apache/solr/schema/SchemaManagerUtilsTest.java b/solr/core/src/test/org/apache/solr/schema/SchemaManagerUtilsTest.java
new file mode 100644
index 00000000000..6449175e073
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/schema/SchemaManagerUtilsTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.schema;
+
+import static org.hamcrest.Matchers.containsStringIgnoringCase;
+import static org.hamcrest.Matchers.instanceOf;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.client.api.model.UpsertFieldOperation;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.CommandOperation;
+import org.junit.Test;
+
+/** Unit tests for {@link SchemaManagerUtils} */
+public class SchemaManagerUtilsTest extends SolrTestCase {
+
+ @Test
+ public void testReportsErrorWhenOperationNameNotRecognized() {
+ final var unknownOperationNameCommand = new CommandOperation("unknownOpName", Map.of());
+
+ final var thrown =
+ expectThrows(
+ SolrException.class,
+ () -> {
+ SchemaManagerUtils.convertToSchemaChangeOperations(unknownOperationNameCommand);
+ });
+ assertEquals(400, thrown.code());
+ assertThat(thrown.getMessage(), containsStringIgnoringCase("no such operation"));
+ assertThat(thrown.getMessage(), containsStringIgnoringCase("unknownOpName"));
+ }
+
+ // Not worth testing each and every SchemaChangeOperation, but try a sample one.
+ @Test
+ public void testConvertsCommandOperationToSchemaOperation() {
+ final var replaceFieldDataMap = new HashMap<String, Object>();
+ replaceFieldDataMap.put("name", "someFieldName");
+ replaceFieldDataMap.put("type", "my.FieldType");
+ replaceFieldDataMap.put("stored", true);
+ final var replaceFieldGeneric = new CommandOperation("replace-field", replaceFieldDataMap);
+
+ final var replaceFieldSchemaOp =
+ SchemaManagerUtils.convertToSchemaChangeOperations(replaceFieldGeneric);
+
+ assertThat(replaceFieldSchemaOp, instanceOf(UpsertFieldOperation.class));
+ final var replaceFieldSchemaOpCast = (UpsertFieldOperation) replaceFieldSchemaOp;
+ assertEquals("someFieldName", replaceFieldSchemaOpCast.name);
+ assertEquals("my.FieldType", replaceFieldSchemaOpCast.type);
+ assertEquals(Boolean.TRUE, replaceFieldSchemaOpCast.getAdditionalProperties().get("stored"));
+ }
+}
diff --git a/solr/solr-ref-guide/modules/indexing-guide/pages/schema-api.adoc b/solr/solr-ref-guide/modules/indexing-guide/pages/schema-api.adoc
index 8f0d3c6a6ce..aa4047dc292 100644
--- a/solr/solr-ref-guide/modules/indexing-guide/pages/schema-api.adoc
+++ b/solr/solr-ref-guide/modules/indexing-guide/pages/schema-api.adoc
@@ -122,12 +122,11 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "add-field":{
- "name":"sell_by",
- "type":"pdate",
- "stored":true }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '
+{
+ "type":"pdate",
+ "stored":true
+}' http://localhost:8983/api/collections/techproducts/schema/fields/sell_by
----
====
======
@@ -137,7 +136,7 @@ curl -X POST -H 'Content-type:application/json' --data-binary '{
The `delete-field` command removes a field definition from your schema.
If the field does not exist in the schema, or if the field is the source or destination of a copy field rule, an error is thrown.
-For example, to delete a field named "sell_by", you would POST the following request:
+For example, to delete a field named "sell_by", you would send the following request:
[tabs#delete-request]
======
@@ -157,9 +156,7 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "delete-field" : { "name":"sell_by" }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X DELETE "http://localhost:8983/api/collections/techproducts/schema/fields/sell_by"
----
====
======
@@ -196,12 +193,10 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "replace-field":{
- "name":"sell_by",
- "type":"date",
- "stored":false }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '{
+ "type":"date",
+ "stored":false
+}' http://localhost:8983/api/collections/techproducts/schema/fields/sell_by
----
====
======
@@ -236,12 +231,10 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "add-dynamic-field":{
- "name":"*_s",
- "type":"string",
- "stored":true }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '{
+ "type":"string",
+ "stored":true
+}' "http://localhost:8983/api/collections/techproducts/schema/dynamicfields/*_s"
----
====
======
@@ -251,7 +244,7 @@ curl -X POST -H 'Content-type:application/json' --data-binary '{
The `delete-dynamic-field` command deletes a dynamic field rule from your schema.
If the dynamic field rule does not exist in the schema, or if the schema contains a copy field rule with a target or destination that matches only this dynamic field rule, an error is thrown.
-For example, to delete a dynamic field rule matching "*_s", you can POST a request like this:
+For example, to delete a dynamic field rule matching "*_s", you can send a request like this:
[tabs#delete-dynamic-request]
======
@@ -271,9 +264,7 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "delete-dynamic-field":{ "name":"*_s" }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X DELETE "http://localhost:8983/api/collections/techproducts/schema/dynamicfields/*_s"
----
====
======
@@ -310,12 +301,10 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "replace-dynamic-field":{
- "name":"*_s",
- "type":"text_general",
- "stored":false }
-}' http://localhost:8983/solr/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '{
+ "type": "text_general",
+ "stored": false
+}' "http://localhost:8983/api/collections/techproducts/schema/dynamicfields/*_s"
----
====
======
@@ -387,18 +376,16 @@ V2 API with Two Analyzers::
To define two analyzers with the V2 API, we just use a different endpoint:
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "add-field-type":{
- "name":"myNewTextField",
- "class":"solr.TextField",
- "indexAnalyzer":{
- "tokenizer":{
- "name":"pathHierarchy",
- "delimiter":"/" }},
- "queryAnalyzer":{
- "tokenizer":{
- "name":"keyword" }}}
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '{
+ "class":"solr.TextField",
+ "indexAnalyzer":{
+ "tokenizer":{
+ "name":"pathHierarchy",
+ "delimiter":"/" }},
+ "queryAnalyzer":{
+ "tokenizer":{
+ "name":"keyword" }}
+}' http://localhost:8983/api/collections/techproducts/schema/fieldtypes/myNewTextField
----
====
======
@@ -428,9 +415,7 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "delete-field-type":{ "name":"myNewTxtField" }
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X DELETE "http://localhost:8983/api/collections/techproducts/schema/fieldtypes/myNewTxtField"
----
====
======
@@ -471,15 +456,13 @@ V2 API::
====
[source,bash]
----
-curl -X POST -H 'Content-type:application/json' --data-binary '{
- "replace-field-type":{
- "name":"myNewTxtField",
- "class":"solr.TextField",
- "positionIncrementGap":"100",
- "analyzer":{
- "tokenizer":{
- "name":"standard" }}}
-}' http://localhost:8983/api/collections/techproducts/schema
+curl -X PUT -H 'Content-type:application/json' --data-binary '{
+ "class":"solr.TextField",
+ "positionIncrementGap":"100",
+ "analyzer":{
+ "tokenizer":{
+ "name":"standard" }}
+}' http://localhost:8983/api/collections/techproducts/schema/fieldtypes/myNewTxtField
----
====
======
@@ -592,7 +575,7 @@ The commands are executed in the order in which they are specified.
This means that if you want to create a new field type and in the same request use the field type on a new field, the section of the request that creates the field type must come before the section that creates the new field.
Similarly, since a field must exist for it to be used in a copy field rule, a request to add a field must come before a request for the field to be used as either the source or the destination for a copy field rule.
-The syntax for making multiple requests supports several approaches.
+The v1 syntax for making multiple requests supports several approaches.
First, the commands can simply be made serially, as in this request to create a new field type and then a field that uses that type:
[source,bash]
@@ -619,7 +602,7 @@ curl -X POST -H 'Content-type:application/json' --data-binary '{
}' http://localhost:8983/solr/techproducts/schema
----
-Or, the same command can be repeated, as in this example:
+Multiple commands of the same type may be repeated, as in this example:
[source,bash]
----
@@ -638,7 +621,7 @@ curl -X POST -H 'Content-type:application/json' --data-binary '{
}' http://localhost:8983/solr/techproducts/schema
----
-Finally, repeated commands can be sent as an array:
+Commands sharing a type can also be grouped together within a list:
[source,bash]
----
@@ -653,6 +636,33 @@ curl -X POST -H 'Content-type:application/json' --data-binary '{
}' http://localhost:8983/solr/techproducts/schema
----
+Bulk schema operations are also supported in Solr's v2 API, using a slightly different syntax.
+Individual operations are specified as elements within a list, with the operation name (e.g. `add-field`, `delete-field-type`) specified as a `"operationType"` property on each operation object:
+
+[source,bash]
+----
+curl -X POST -H 'Content-type:application/json' --data-binary '
+ [
+ {
+ "operationType": "add-field"
+ "name": "shelf",
+ "type": "myNewTxtField",
+ "stored": true
+ },
+ {
+ "operationType": "add-field"
+ "name": "location",
+ "type": "myNewTxtField",
+ "stored": true
+ },
+ {
+ "operationType": "add-copy-field"
+ "source": "shelf",
+ "destinations": ["location", "catchall"]
+ }
+ ]' http://localhost:8983/solr/techproducts/schema/bulk
+----
+
=== Schema Changes Among Replicas
When running in SolrCloud mode, changes made to the schema on one node will propagate to all replicas in the collection.
diff --git a/solr/solrj/src/resources/java-template/api.mustache b/solr/solrj/src/resources/java-template/api.mustache
index 0ed28650056..03ee53ebcde 100644
--- a/solr/solrj/src/resources/java-template/api.mustache
+++ b/solr/solrj/src/resources/java-template/api.mustache
@@ -190,9 +190,18 @@ public class {{classname}} {
{{/description}}
public void {{setter}}({{{dataType}}} {{baseName}}) {
this.requestBody.{{baseName}} = {{baseName}};
- }
+ }
{{/vars}}
+ {{#vendorExtensions.x-hasAdditionalFields}}
+ /**
+ * Set additional properties to be included in the request body.
+ */
+ public void setAdditionalProperty(String property, Object value) {
+ requestBody.setAdditionalProperty(property, value);
+ }
+ {{/vendorExtensions.x-hasAdditionalFields}}
+
@Override
@SuppressWarnings("unchecked")
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/SchemaTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/SchemaTest.java
index 6502bd2a5ce..e1f34d2c934 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/SchemaTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/SchemaTest.java
@@ -17,6 +17,7 @@
package org.apache.solr.client.solrj.request;
import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -32,7 +33,6 @@ import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.io.file.PathUtils;
import org.apache.solr.client.solrj.SolrClient;
-import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
import org.apache.solr.client.solrj.request.schema.AnalyzerDefinition;
import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
@@ -41,8 +41,6 @@ import org.apache.solr.client.solrj.response.schema.FieldTypeRepresentation;
import org.apache.solr.client.solrj.response.schema.SchemaRepresentation;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.common.SolrException;
-import org.apache.solr.common.util.NamedList;
-import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.util.RestTestBase;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After;
@@ -64,18 +62,8 @@ public class SchemaTest extends RestTestBase {
private static void assertFailedSchemaResponse(
ThrowingRunnable runnable, String expectedErrorMessage) {
- BaseHttpSolrClient.RemoteExecutionException e =
- expectThrows(BaseHttpSolrClient.RemoteExecutionException.class, runnable);
- SimpleOrderedMap<?> errorMap = (SimpleOrderedMap<?>) e.getMetaData().get("error");
- assertEquals(
- "org.apache.solr.api.ApiBag$ExceptionWithErrObject",
- ((NamedList) errorMap.get("metadata")).get("error-class"));
- List<?> details = (List<?>) errorMap.get("details");
- assertTrue(
- ((List<?>) ((Map<?, ?>) details.get(0)).get("errorMessages"))
- .get(0)
- .toString()
- .contains(expectedErrorMessage));
+ final var e = expectThrows(SolrClient.RemoteSolrException.class, runnable);
+ assertThat(e.getMessage(), containsString(expectedErrorMessage));
}
private static void createStoredStringField(String fieldName, SolrClient solrClient)