DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Elasticsearch 报错 Invalid index name must be lowercase

分析创建 Elasticsearch 索引报 Invalid index name must be lowercase 的命名约束根因与自动化小写转换方案。

问题现象

向 Elasticsearch 创建索引或写入新文档时返回 HTTP 400:

{
  "error": {
    "root_cause": [
      {
        "type": "invalid_index_name_exception",
        "reason": "Invalid index name [UserLogs-2026], must be lowercase",
        "index_uuid": "_na_",
        "index": "UserLogs-2026"
      }
    ],
    "type": "invalid_index_name_exception",
    "reason": "Invalid index name [UserLogs-2026], must be lowercase",
    "index_uuid": "_na_",
    "index": "UserLogs-2026"
  },
  "status": 400
}

触发根因

  1. Elasticsearch 索引命名硬性规范:为了保证跨底层操作系统(Linux/Windows 大小写敏感差异)文件系统的统一性,索引名称必须全为小写字母。
  2. 索引名称包含了大写字母(如 UserLogs-2026)。

解决步骤与操作命令

步骤一:将索引名称全部改为纯小写

# 错误写法
curl -X PUT "http://localhost:9200/UserLogs-2026"

# 正确写法
curl -X PUT "http://localhost:9200/user_logs-2026"

步骤二:在应用程序代码层自动强制 toLowerCase()

String indexName = ("user_logs_" + topicName).toLowerCase();

验证确认

再次运行索引创建: 返回 {"acknowledged": true, "shards_acknowledged": true, "index": "user_logs-2026"},完全符合规范。