非SolrCloud模式下通过REST API/SolrJ创建带独立Schema的Solr Core
Got it, let's tackle this problem step by step. The key issue you're hitting is that using basic_configs directly shares the configuration across cores—so modifying one core's schema changes the source config set. Here's how to create cores with independent, fresh schemas via REST API or SolrJ without pre-creating instance directories first:
1. REST API Approach
The fix here is to use the copyConfig=true parameter in the Core Admin CREATE API. This tells Solr to copy the config set into a new, dedicated instance directory instead of sharing the original configs.
Step 1: Create a Core with Copied Configs
Run this GET request (replace localhost:8983 with your Solr host/port and my-new-core with your desired core name):
curl -X GET "http://localhost:8983/solr/admin/cores?action=CREATE&name=my-new-core&configSet=basic_configs©Config=true&instanceDir=my-new-core"
copyConfig=true: Critical parameter that duplicates thebasic_configsinto the newinstanceDir(no more shared configs!)instanceDir=my-new-core: Specifies the directory where Solr will store this core's unique configs and data (Solr will auto-create this directory if it doesn't exist)
Step 2: Modify the Core's Schema Independently
Now you can use the Schema API to update this core's schema without affecting basic_configs or other cores. For example, add a new field:
curl -X POST -H "Content-Type: application/json" "http://localhost:8983/solr/my-new-core/schema" -d '{ "add-field": { "name": "custom_field", "type": "string", "stored": true } }'
This change only applies to my-new-core—the original basic_configs remains untouched, so you can repeat Step 1 to create as many unique cores as needed.
2. SolrJ Approach
You can replicate the same logic using SolrJ by setting the copyConfig parameter in your create request. Here's a working code example:
import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.request.CoreAdminRequest; import org.apache.solr.common.params.CoreAdminParams; public class UniqueCoreCreator { public static void main(String[] args) { String solrBaseUrl = "http://localhost:8983/solr"; try (SolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) { // 1. Create core with copied configs CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create(); createRequest.setCoreName("my-solrj-core"); createRequest.setConfigSet("basic_configs"); createRequest.setCopyConfig(true); // Enable config copying createRequest.setInstanceDir("my-solrj-core"); solrClient.request(createRequest); // 2. Modify the schema dynamically String schemaUpdate = "{\"add-field\": {\"name\": \"solrj_custom_field\", \"type\": \"text_general\", \"stored\": true}}"; CoreAdminRequest.Schema schemaRequest = new CoreAdminRequest.Schema(); schemaRequest.setCoreName("my-solrj-core"); schemaRequest.setAction(CoreAdminParams.SchemaAction.UPDATESCHEMA); schemaRequest.setContent(schemaUpdate); solrClient.request(schemaRequest); System.out.println("Core created and schema updated successfully!"); } catch (Exception e) { e.printStackTrace(); } } }
Key Notes
- Permissions: Ensure Solr has write access to the directory where it creates the
instanceDir(default is Solr'sserver/solrfolder). - Schema Types: If you're using
managed-schema(Solr's default), the Schema API is the recommended way to make dynamic changes. If you preferschema.xml, you can edit the file in the core'sinstanceDirand then reload the core via the API. - No SolrCloud Required: Both methods work perfectly in standalone Solr mode (no ZooKeeper needed).
内容的提问来源于stack exchange,提问作者PythaLye

