如何在Solr Cloud模式下通过API为集合创建自定义Schema?
Solr Cloud 9.2 自定义Schema配置说明
核心问题解答
是的,Solr Cloud模式下,所有集群共享的配置(包括Schema)必须存储在ZooKeeper中,无法通过修改单个节点的本地文件来生效。下面是两种创建并为members集合分配自定义Schema的可行方法:
方法一:使用Schema API逐步定义(推荐)
Solr 9推荐通过Schema API管理Schema,这种方式会自动将配置同步到ZK,无需手动上传文件。
- 添加自定义字段类型
比如创建适合中文分词的字段类型:
curl -X POST -H "Content-Type: application/json" http://localhost:8983/api/collections/members/schema -d '{ "add-field-type": { "name": "text_cn", "class": "solr.TextField", "analyzer": { "tokenizer": { "class": "solr.HMMChineseTokenizerFactory" }, "filters": [ { "class": "solr.LowerCaseFilterFactory" } ] } } }'
- 添加业务字段
根据需求添加所需字段:
curl -X POST -H "Content-Type: application/json" http://localhost:8983/api/collections/members/schema -d '{ "add-field": { "name": "username", "type": "string", "stored": true, "indexed": true }, "add-field": { "name": "user_desc", "type": "text_cn", "stored": true, "indexed": true } }'
你还可以通过API执行删除字段、修改字段属性等其他Schema操作。
方法二:上传完整配置集到ZK并绑定集合
如果你已有包含managed-schema、solrconfig.xml的完整配置文件,可以通过Solr自带工具上传到ZK,再关联到集合。
准备配置集文件夹
创建一个文件夹,放入你的managed-schema、solrconfig.xml以及必要的辅助文件(如自定义分词器jar、语言配置文件夹等)。上传配置集到ZK
使用Solr安装目录下的zkcli.sh(Windows用zkcli.bat)执行上传:
cd /path/to/solr/bin ./zkcli.sh -zkhost localhost:2181 -cmd upconfig -confdir /path/to/your/custom/config-folder -confname members_custom_config
zkhost:ZK集群地址,单机版填localhost:2181即可confdir:你的自定义配置文件夹路径confname:ZK中存储的配置集名称,可自定义
- 绑定集合到新配置集
修改members集合的配置属性,关联刚上传的配置集:
curl -X POST -H "Content-Type: application/json" http://localhost:8983/api/collections/members -d '{ "set-property": { "config": "members_custom_config" } }'
注意事项
- Solr 9默认使用Managed Schema,不建议手动编辑
schema.xml,Managed Schema会由Solr自动维护并同步到ZK。 - 修改Schema后,已有数据不会自动适配新字段,需要重新索引数据才能让新Schema生效。
- 确保
solrconfig.xml中的schemaFactory配置与Schema类型匹配,默认的ManagedIndexSchemaFactory对应managed-schema。
内容的提问来源于stack exchange,提问作者lenchester
相关产品推荐
相关产品推荐

