配置项作用 #
script.allowed_types 配置项控制允许执行的脚本类型(内联脚本或存储脚本)。
是否可选 #
是
默认值 #
[] (空列表,允许所有类型)
配置项类型 #
动态配置 - 可以在运行时修改
配置格式 #
# 允许所有类型(默认)
script.allowed_types: []
# 只允许存储脚本
script.allowed_types:
- stored
# 只允许内联脚本
script.allowed_types:
- inline
# 允许两种类型
script.allowed_types:
- inline
- stored
# 禁用所有脚本
script.allowed_types:
- none
脚本类型说明 #
| 类型 | 说明 | 安全性 |
|---|---|---|
inline | 在请求中直接编写的脚本 | 较低,需验证 |
stored | 预先存储的脚本 | 较高,可审查 |
推荐设置 #
| 环境 | 推荐值 | 说明 |
|---|---|---|
| 开发环境 | [] 或 ["inline", "stored"] | 允许所有类型 |
| 生产环境 | ["stored"] | 只允许存储脚本 |
| 高安全环境 | ["stored"] | 便于审查和管理 |
使用示例 #
默认配置:
script.allowed_types: []
生产环境配置(推荐):
# 只允许存储脚本
script.allowed_types:
- stored
开发环境配置:
# 允许所有类型
script.allowed_types:
- inline
- stored
安全建议 #
生产环境最佳实践:
- 只允许存储脚本
script.allowed_types:
- stored
- 存储脚本的优势
- 可预先审查
- 集中管理
- 版本控制
- 便于审计
内联脚本 vs 存储脚本 #
内联脚本示例:
GET /_search
{
"query": {
"script": {
"source": "doc['field1'].value * 2"
}
}
}
存储脚本示例:
// 1. 存储脚本
POST _scripts/calculate_score
{
"script": {
"lang": "painless",
"source": "doc['field1'].value * params.multiplier"
}
}
// 2. 使用脚本
GET /_search
{
"query": {
"script": {
"id": "calculate_score",
"params": {
"multiplier": 2
}
}
}
}
配置验证 #
# 查看当前配置
GET /_cluster/settings?filter_path=*.script.allowed_types
# 测试内联脚本(如果允许)
GET /_search
{
"query": {
"script": {
"source": "doc['field1'].value * 2"
}
}
}
常见问题 #
问题 1:脚本类型不被允许
错误信息:
illegal_argument_exception: can't execute scripts of type [inline]
解决方案:
# 添加允许的类型
script.allowed_types:
- inline
- stored
问题 2:安全顾虑
解决方案:
# 只允许存储脚本
script.allowed_types:
- stored
注意事项 #
- 动态配置:可以在运行时修改
- 安全考虑:生产环境建议只允许存储脚本
- 空列表:表示允许所有类型
- none 值:禁用所有脚本执行
- 兼容性:升级时需要考虑现有脚本类型
相关配置项 #
| 配置项 | 默认值 | 说明 |
|---|---|---|
script.allowed_contexts | [] | 允许的脚本上下文 |
script.max_size_in_bytes | 65535 | 脚本最大大小 |
script.disable_max_compilations_rate | false | 是否禁用编译速率限制 |
完整配置示例 #
# easysearch.yml
# 生产环境配置
script.allowed_types:
- stored
script.allowed_contexts:
- aggs
- search





