curl -iX POST 'http://127.0.0.1:9200/getdeal_workform/_search?typed_keys=true' -d '{"_source":{"includes":["getdealid","fromtela","openx","wfid","fromname","wkfmno","workformno","fromtime","fmcontent","statedesc","wscode","dcntaname","dfmclaname","wskey","accsubject","stateflag"]},"from":0,"query":{"bool":{"must":[{"range":{"fromtime":{"to":"2025-07-08 00:00:00","format":"yyyy-MM-dd HH:mm:ss"}}},{"match":{"tablename":{"query":"t"}}}]}},"size":10,"sort":[{"getdealid":{"order":"desc"}}]}' # HTTP/1.1 400 Bad Request # X-elastic-product: Elasticsearch # content-type: application/vnd.elasticsearch+json;compatible-with=8 # content-length: 689 # # {"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: For input string: \"2025-07-08 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-07-08 00:00:00\"","index_uuid":"pyq8S-9xTYaXeUhxGT9Mvg","index":"getdeal_workform","caused_by":{"type":"number_format_exception","reason":"For input string: \"2025-07-08 00:00:00\""}}}]},"status":400} 2025-07-08 16:39:03.264 [http-nio-8180-exec-23] ERROR com.xhh.nphm.framework.web.exception.GlobalExceptionHandler - [notFount,64] - 访问的URL[/system/lookup/telGetAllEsList]发生异常[es/search] failed: [search_phase_execution_exception] all shards failed co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
时间: 2025-07-08 21:47:21 浏览: 3
在 Elasticsearch 查询中,当出现错误信息 `failed to create query: For input string: "2025-07-08 00:00:00"` 时,通常表示查询的字段类型与输入的数据格式不匹配。例如,字段可能被定义为数值类型(如 `long` 或 `integer`),而用户尝试以字符串形式传递日期值。
### 错误原因分析
1. 如果字段是 **date 类型**:Elasticsearch 支持将字符串形式的日期直接用于查询,但前提是字段映射必须定义为 `date` 类型,并且字符串格式需符合字段定义的日期格式。
2. 如果字段是 **text 或 keyword 类型**:可以使用 term 查询进行精确匹配,但不能直接用于范围查询或数值比较。
3. 如果字段是 **数值类型(如 long、integer)**:尝试传入字符串会导致解析失败,从而抛出 `NumberFormatException` 异常[^2]。
### 解决方案
#### 1. 检查字段映射类型
确保字段的映射类型与查询内容相匹配。例如,如果字段是 `date` 类型,则可以使用如下方式构建查询:
```json
{
"query": {
"term": {
"create_time": "2025-07-08 00:00:00"
}
}
}
```
如果字段是 `date` 类型,上述查询可以正常工作,因为 Elasticsearch 会自动将字符串转换为对应的日期格式[^3]。
#### 2. 调整字段映射
如果字段未正确映射为 `date` 类型,可以通过更新索引映射来调整字段类型。以下是一个更新映射的示例:
```json
PUT /hotel/_mapping
{
"properties": {
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
```
此操作将 `create_time` 字段设置为 `date` 类型,并指定其格式为 `yyyy-MM-dd HH:mm:ss`,以便支持类似 `"2025-07-08 00:00:00"` 的字符串输入[^3]。
#### 3. 使用正确的查询方式
如果字段是 `keyword` 类型,则应使用 `term` 查询进行精确匹配:
```json
{
"query": {
"term": {
"create_time.keyword": "2025-07-08 00:00:00"
}
}
}
```
如果字段是 `text` 类型,则需要启用 `not_analyzed` 选项,或者使用 `.keyword` 子字段进行精确匹配。
#### 4. 避免将字符串传递给数值类型字段
如果字段是数值类型(如 `long` 或 `integer`),则不能直接使用字符串格式的日期进行查询。此时,需要将日期转换为时间戳(如毫秒数)后再进行查询:
```json
{
"query": {
"term": {
"create_time": 1751961600000 // 对应 "2025-07-08 00:00:00" 的时间戳
}
}
}
```
#### 5. 处理异常的根本原因
通过检查返回的错误信息中的 `caused_by` 部分,可以找到导致异常的具体原因。例如,如果错误信息显示 `For input string: "abc"`,则表明传入了非数值类型的字符串到数值字段中。因此,需要确保查询参数的类型与字段定义一致[^4]。
### 示例代码
以下是一个使用 Java 构建日期查询的示例:
```java
@Test
public void testDateQuery() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
Arrays.stream("127.0.0.1:9200".split(","))
.map(host -> {
String[] split = host.split(":");
String hostName = split[0];
int port = Integer.parseInt(split[1]);
return new HttpHost(hostName, port, HttpHost.DEFAULT_SCHEME_NAME);
})
.filter(Objects::nonNull)
.toArray(HttpHost[]::new)
)
);
SearchRequest searchRequest = new SearchRequest("hotel");
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("create_time", "2025-07-08 00:00:00");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString() + " " + hit.getScore());
}
}
```
###
阅读全文
相关推荐


















