{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: For input string: \"2025-06-07 00:00:00\"","index_uuid":"pyq8S-9xTYaXeUhxGT9Mvg","index":"getdeal_workform"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"getdeal_workform","node":"DRYANDKzTy6-GMiFw3XoDw","reason":{"type":"query_shard_exception","reason":"failed to create query: For input string: \"2025-06-07 00:00:00\"","index_uuid":"pyq8S-9xTYaXeUhxGT9Mvg","index":"getdeal_workform","caused_by":{"type":"number_format_exception","reason":"For input string: \"2025-06-07 00:00:00\""}}}]},"status":400} 2025-07-07 17:13:28.687 [http-nio-8180-exec-21] ERROR com.xhh.nphm.framework.web.exception.GlobalExceptionHandler - [notFount,64] - 访问的发生异常[es/search] failed: [search_phase_execution_exce
时间: 2025-07-07 15:13:13 浏览: 13
在 Elasticsearch 查询过程中,出现 `QueryShardException` 和 `NumberFormatException` 错误通常与字段类型不匹配、查询格式错误或日期字符串处理不当有关。以下是可能导致该问题的原因及解决方案:
### 1. **字段类型与查询方式不匹配**
- 如果尝试对一个 `date` 类型的字段使用 `term` 查询,并传入日期字符串(如 `"2025-06-07 00:00:00"`),可能会失败,因为 `term` 查询适用于精确值匹配,而 `date` 字段可能需要使用范围查询或经过适当格式化[^2]。
- 推荐使用 `range` 查询或 `bool` 查询结合 `must` 子句来替代 `term` 查询。
```json
{
"query": {
"bool": {
"must": [
{
"range": {
"create_time": {
"gte": "2025-06-07 00:00:00",
"lt": "2025-06-08 00:00:00"
}
}
}
]
}
}
}
```
### 2. **日期格式未在映射中定义**
- 如果字段被定义为 `date` 类型但没有指定具体的日期格式,则默认支持 ISO8601 格式(如 `"yyyy-MM-dd HH:mm:ss"`)。若输入的日期字符串不符合索引映射中定义的格式,Elasticsearch 将无法解析并抛出异常。
- 检查字段映射可以确认是否支持 `"2025-06-07 00:00:00"` 这种格式:
```json
GET /hotel/_mapping
```
- 若映射中未定义对应格式,可以在创建索引时显式声明日期格式:
```json
PUT /hotel
{
"mappings": {
"properties": {
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
```
### 3. **使用 term 查询对非 keyword 字段进行精确匹配**
- 如果字段是 `text` 或 `date` 类型而非 `keyword`,使用 `term` 查询将导致失败,因为 `term` 查询不会分析文本内容。对于 `text` 字段,建议使用 `match` 查询;对于 `date` 字段,推荐使用 `range` 查询或通过 `script` 转换后再执行 `term` 查询[^4]。
```json
{
"query": {
"script": {
"script": {
"source": "doc['create_time'].value == params.date",
"params": {
"date": "2025-06-07T00:00:00Z"
}
}
}
}
}
```
### 4. **参数传递错误**
- 如果查询参数是以数组形式传入(例如来自用户请求),而在构建 `term` 查询时直接传入数组,会导致类型不匹配并引发异常。确保 `term` 查询接受的是单一值,而不是集合[^4]。
```java
if (params.getBrandId() != null && !params.getBrandId().isEmpty()) {
boolQuery.filter(QueryBuilders.termQuery("brandId", params.getBrandId().get(0)));
}
```
### 5. **DSL 构建工具版本兼容性问题**
- 使用 Java High Level REST Client 时,某些版本对 DSL 构建存在限制,建议升级到 Elasticsearch 7.x 及以上版本并使用新的 Java API 客户端,以避免潜在的语法解析问题[^2]。
---
阅读全文
相关推荐



















