是否有任何 Solr API 来读取 Solr schema.xml?
我需要它的原因是 Solr faceting 不向后兼容。如果索引没有定义字段 A,但程序尝试为字段 A 生成分面,则所有分面都会失败。因此,我需要在运行时检查索引中有哪些字段,并动态生成构面。
自 Solr 4.2 起,Schema REST API 允许您通过以下方式获取架构:
http://localhost:8983/solr/schema
或使用核心名称:
http://localhost:8983/solr/mycorename/schema
从 Solr 4.4 开始,您还可以修改您的架构。
您可以使用以下方式获取架构http://localhost:8983/solr/admin/file/?contentType=text/xml;charset=utf-8&file=schema.xml
它是原始 xml,因此必须对其进行解析以获取所需的信息。
但是,如果您的程序生成了无效的方面,也许您应该只修复程序而不是尝试解决此问题。
实际上,您有 Schema API。Solr 模式 API 允许使用 REST API 来获取有关 schema.xml 的信息
在 Solr 4.2 和 4.3 中,它只允许 GET(只读)访问,但在 Solr 4.4 中,可以将新字段和 copyField 指令添加到架构中。未来的 Solr 版本将扩展此功能以允许更新更多模式元素
API 入口点
/collection/schema: retrieve the entire schema
/collection/schema/fields: retrieve information about all defined fields, or create new fields with optional copyField directives
/collection/schema/fields/name: retrieve information about a named field, or create a new named field with optional copyField directives
/collection/schema/dynamicfields: retrieve information about dynamic field rules
/collection/schema/dynamicfields/name: retrieve information about a named dynamic rule
/collection/schema/fieldtypes: retrieve information about field types
/collection/schema/fieldtypes/name: retrieve information about a named field type
/collection/schema/copyfields: retrieve information about copy fields, or create new copyField directives
/collection/schema/name: retrieve the schema name
/collection/schema/version: retrieve the schema version
/collection/schema/uniquekey: retrieve the defined uniqueKey
/collection/schema/similarity: retrieve the global similarity definition
/collection/schema/solrqueryparser/defaultoperator: retrieve the default operator
例子
Input 获取所有字段的列表。
curl http://localhost:8983/solr/collection1/schema/fields?wt=json
输入 以 JSON 格式获取整个架构。
curl http://localhost:8983/solr/collection1/schema?wt=json
更多信息:apache-solr-ref-guide-4.5.pdf (搜索 Schema API)