Solr配置:v2 API现代化接口详解与使用指南

Solr配置:v2 API现代化接口详解与使用指南

v2 API是Solr现代化的自文档化API接口,覆盖了大部分当前Solr API功能。随着v2 API达到完全覆盖,以及SolrJ和Admin UI等Solr内部API使用从旧API转换到v2 API,旧API最终将被淘汰。

v2 API设计理念

实验性质声明

v2 API被归类为”实验性”功能。在发展过程中,它可能会以向后不兼容的方式进行更改,以覆盖额外功能。

禁用v2 API

如果需要,可以通过系统属性禁用所有v2 API端点:

1
-Ddisable.v2.api=true

与旧API的共存

目前,两种API风格共存,所有旧API将继续正常工作,无需任何更改。

v2 API与旧API的核心差异

1. 命令格式差异

  • 旧API:命令和相关参数通过HTTP GET请求的URL请求参数提供
  • v2 API:大多数API命令通过JSON主体POST到v2 API端点提供,同时在适当的地方支持HTTP GET和DELETE方法

2. 端点结构差异

v2 API端点结构已经理性化和规范化,提供更清晰的层次结构和一致的命名约定。

3. 文档化差异

v2 API是自文档化的:在任何有效的v2 API路径后附加/_introspect,API规范将以JSON格式返回。

v2 API路径前缀

核心路径映射表

路径前缀 支持的操作
/api/collections 创建、别名、备份和恢复集合
/api/c/_collection-name_/update 更新请求
/api/c/_collection-name_/config 配置请求
/api/c/_collection-name_/schema 模式请求
/api/c/_collection-name_/_handler-name_ 特定处理器请求
/api/c/_collection-name_/shards 分片操作:分割分片、创建分片、添加副本
/api/c/_collection-name_/shards/_shard-name_ 删除分片、强制leader选举
/api/c/_collection-name_/shards/_shard-name_/_replica-name_ 删除副本
/api/cores 创建核心
/api/cores/_core-name_ 重新加载、重命名、删除和卸载核心
/api/node 执行overseer操作、重新加入leader选举
/api/cluster 添加角色、删除角色、设置集群属性
/api/c/.system/blob 上传和下载blob及元数据

API内省机制

基本内省功能

在任何有效的v2 API路径后附加/_introspect,即可获得JSON格式的API规范:

1
http://localhost:8983/api/c/_introspect

按HTTP方法过滤

限制内省输出仅包含特定HTTP方法,使用method请求参数:

1
2
3
http://localhost:8983/api/c/_introspect?method=POST
http://localhost:8983/api/c/_introspect?method=GET
http://localhost:8983/api/c/_introspect?method=DELETE

按命令过滤

大多数端点支持通过POST发送正文中的命令。限制内省输出仅包含一个命令:

1
http://localhost:8983/api/c/gettingstarted/_introspect?method=POST&command=modify

内省输出解释

GET API内省示例

1
http://localhost:8983/api/c/gettingstarted/get/_introspect

响应结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"spec":[{
"documentation":"https://solr.apache.org/guide/solr/latest/configuration-guide/realtime-get.html",
"description":"RealTime Get allows retrieving documents by ID before the documents have been committed to the index.",
"methods":["GET"],
"url":{
"paths":["/c/gettingstarted/get"],
"params":{
"id":{
"type":"string",
"description":"A single document ID to retrieve."
},
"ids":{
"type":"string",
"description":"One or more document IDs to retrieve. Separate by commas if more than one ID is specified."
},
"fq":{
"type":"string",
"description":"An optional filter query to add to the query."
}
}
}
}],
"WARNING":"This response format is experimental. It is likely to change in the future.",
"availableSubPaths":{}
}

关键字段说明

documentation

  • 在线Solr参考指南中此API部分的URL

description

  • 功能/变量/命令等的文本描述

spec/methods

  • 此API支持的HTTP方法

spec/url/paths

  • 此API支持的URL路径

spec/url/params

  • 支持的URL请求参数列表

availableSubPaths

  • 有效URL子路径列表及每个支持的HTTP方法

POST API内省示例

1
http://localhost:8983/api/c/gettingstarted/_introspect?method=POST&command=modify

响应结构包含commands部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"spec":[{
"documentation":"https://solr.apache.org/guide/solr/latest/configuration-guide/collections-api.html",
"description":"Several collection-level operations are supported with this endpoint",
"methods":["POST"],
"url":{
"paths":["/collections/{collection}", "/c/{collection}"]
},
"commands":{
"modify":{
"documentation":"https://solr.apache.org/guide/solr/latest/deployment-guide/collection-management.html#modifycollection",
"description":"Modifies specific attributes of a collection.",
"type":"object",
"properties":{
"replicationFactor":{
"type":"string",
"description":"The number of replicas to be created for each shard."
}
}
}
}
}],
"availableSubPaths":{
"/c/gettingstarted/select":["POST", "GET"],
"/c/gettingstarted/config":["POST", "GET"],
"/c/gettingstarted/schema":["POST", "GET"],
"/c/gettingstarted/export":["POST", "GET"],
"/c/gettingstarted/admin/ping":["POST", "GET"],
"/c/gettingstarted/update":["POST"]
}
}

commands部分中的每个条目都有一个命令名称作为键,值是使用JSON schema描述命令结构的JSON对象。

API调用示例

1. 修改集合属性

为”gettingstarted”集合设置复制因子:

1
2
3
curl http://localhost:8983/api/c/gettingstarted \
-H 'Content-type:application/json' \
-d '{ modify: { replicationFactor: "3" } }'

响应:

1
{"responseHeader":{"status":0,"QTime":842}}

2. 查看集群状态

1
curl http://localhost:8983/api/cluster

响应:

1
{"responseHeader":{"status":0,"QTime":0},"collections":["gettingstarted",".system"]}

3. 设置集群属性

1
2
3
curl http://localhost:8983/api/cluster \
-H 'Content-type: application/json' \
-d '{ set-property: { name: maxCoresPerNode, val: "100" } }'

响应:

1
{"responseHeader":{"status":0,"QTime":4}}

v2 API实际应用场景

1. 集合管理操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建集合
curl -X POST http://localhost:8983/api/collections \
-H 'Content-type:application/json' \
-d '{
"create": {
"name": "mycollection",
"config": "myconfigset",
"numShards": 2,
"replicationFactor": 2
}
}'

# 删除集合
curl -X POST http://localhost:8983/api/collections \
-H 'Content-type:application/json' \
-d '{
"delete": {
"name": "mycollection"
}
}'

2. 配置管理操作

1
2
3
4
5
6
7
8
9
10
11
12
13
# 更新请求处理器配置
curl -X POST http://localhost:8983/api/c/mycollection/config \
-H 'Content-type:application/json' \
-d '{
"add-requesthandler": {
"name": "/myhandler",
"class": "solr.SearchHandler",
"defaults": {
"wt": "json",
"indent": "true"
}
}
}'

3. 模式管理操作

1
2
3
4
5
6
7
8
9
10
11
# 添加字段
curl -X POST http://localhost:8983/api/c/mycollection/schema \
-H 'Content-type:application/json' \
-d '{
"add-field": {
"name": "myfield",
"type": "text_general",
"stored": true,
"indexed": true
}
}'

4. 文档更新操作

1
2
3
4
5
6
7
8
9
10
# 更新文档
curl -X POST http://localhost:8983/api/c/mycollection/update \
-H 'Content-type:application/json' \
-d '[
{
"id": "doc1",
"title": "Document Title",
"content": "Document content"
}
]'

v2 API的优势

1. 一致性

  • 统一的端点结构和命名约定
  • 标准化的响应格式
  • 一致的错误处理机制

2. 可发现性

  • 自文档化API规范
  • 内省机制提供实时API文档
  • 清晰的参数类型和描述

3. 现代化

  • RESTful设计原则
  • JSON-first方法
  • 支持标准HTTP方法

4. 可扩展性

  • 模块化架构设计
  • 易于添加新功能
  • 向后兼容性考虑

最佳实践建议

1. API版本管理

  • 明确标识使用的API版本
  • 关注API变更通知
  • 制定迁移计划

2. 错误处理

  • 实现完善的错误处理逻辑
  • 解析标准化响应格式
  • 建立监控和告警机制

3. 文档维护

  • 利用内省机制生成文档
  • 保持API使用文档更新
  • 建立API测试套件

4. 性能考虑

  • 合理使用批量操作
  • 避免不必要的API调用
  • 实现适当的缓存策略

v2 API代表了Solr API设计的未来方向,虽然目前仍处于实验阶段,但其现代化的设计理念和自文档化特性使其成为构建Solr应用的优秀选择。

, ,
© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero