# Elasticsearch解析异常：无法解析搜索模板 [my_template]；错误：Mustache 运行时错误在第10行：[{{#users}}] 部分缺少结束标签

- **ID:** `elasticsearch/search-template-runtime-error`
- **领域:** elasticsearch
- **类别:** config_error
- **错误码:** `ES_SEARCH_TEMPLATE_PARSE_ERR`
- **验证级别:** ai_generated
- **修复率:** 86%

## 根因

用 Mustache 语法编写的搜索模板存在格式错误的标签（未闭合的部分、不匹配的分隔符或嵌套错误），导致无法编译。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.17.0 | active | — | — |
| 8.11.0 | active | — | — |
| 8.12.0 | active | — | — |

## 解决方案

1. ```
   Validate the template using `POST _scripts/painless/_execute` or a Mustache linter to find unclosed sections. Fix by ensuring every `{{#section}}` has a corresponding `{{/section}}`.
   ```
2. ```
   Simplify the template by breaking it into smaller parts and testing each with `GET /my_index/_search/template`.
   ```
3. ```
   Use `POST _scripts/my_template` to update the template with corrected syntax, then test with `GET /_scripts/my_template`.
   ```

## 无效尝试

- **** — Loses template dynamicity, requiring separate queries for each use case, increasing code complexity. (90% 失败率)
- **** — May create nested section errors if the section is already closed, leading to further parsing failures. (80% 失败率)
