Goal
Programmatically add or delete fields in a Solr collection schema using the Schema API without manually editing schema.xml.
Environment
Solr 8.11 and above.
This guide assumes Solr is deployed in SolrCloud mode and managed via ZooKeeper.
Guide
Update Solr to use ManagedIndexSchemaFactory
To enable Schema API operations, Solr must use ManagedIndexSchemaFactory. If the current solrconfig.xml uses ClassicIndexSchemaFactory, the schema will be read-only and any attempt to use the Schema API will return schema is not editable.
Steps:
-
Open the
solrconfig.xmlfile for each configset used by your collections. - Locate the following line:
<schemaFactory class="ClassicIndexSchemaFactory"/>
- Replace it with:
<schemaFactory class="ManagedIndexSchemaFactory"/>
- Upload the updated configset to ZooKeeper:
$SOLR_HOME/server/scripts/cloud-scripts/zkcli.sh -zkhost <ZK_HOST> -cmd upconfig -confdir <configset_dir> -confname <configset_name>
- Reload the associated collection:
curl http://<solr_host>:8983/solr/admin/collections?action=RELOAD&name=<collection_name>
Note: Replace <ZK_HOST>, <configset_dir>, <configset_name>, <solr_host>, and <collection_name> with your actual values.
Once reloaded, the UI should reflect a new managed-schema file alongside a .bak version of the old schema.xml.
Add or remove fields using the Schema API
You can now use curl, Postman, or other REST clients to add or remove fields.
Example API call to add a field:
curl -u <username>:<password> -X POST -H 'Content-type:application/json' --data-binary '{
"add-field": {
"name":"recurring_amt",
"type":"string",
"stored":true
}
}' http://<solr_host>:8983/api/collections/<collection_name>/schema
Note: Ensure the target collection is using a configset that has been updated and reloaded with ManagedIndexSchemaFactory.
Reindexing considerations
Not all schema changes require reindexing. Typically:
-
Adding new fields that are not used in existing queries: no reindexing required
-
Modifying properties like
storedorindexed: no reindexing required -
Changing field types or analyzers: reindexing required
Refer to the Solr documentation for more guidance: https://solr.apache.org/guide/8_11/reindexing.html
Troubleshooting tip
If after updating solrconfig.xml and reloading the collection the Schema API still returns "schema is not editable", verify:
-
The config was successfully pushed to ZooKeeper using
zkcli.sh -
The collection was reloaded post-upload
-
The
managed-schemafile is visible in the Admin UI
Restarting Solr is not required after using the Schema API if all the above steps are completed correctly.