ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];
spring boot集成ES进行时间范围查询,报错如下:
* Failed to execute phase [query], all shards failed; shardFailures {[ZVpk9AuzQdCYQrkIIseGxQ][swapping][0]: RemoteTransportException[[ZVpk9Au][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]]; nested: IllegalArgumentException[Parse failure at index [0] of [Sun Dec 31 16:00:00 UTC 2017]]; }{[ZVpk9AuzQdCYQrkIIseGxQ][swapping][1]: RemoteTransportException[[ZVpk9Au][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]]; nested: IllegalArgumentException[Parse failure at index [0] of [Sun Dec 31 16:00:00 UTC 2017]]; }
* Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];
原因是由于:
java传入查询参数,对于
ES中,index的mapping设置的时间字段的格式是不能解析的。才导致了报了上面这个错误。
解决方案:
只需要在java中,将查询的时间字段由传入时间对象 变成 传入时间戳。即可!!
如下:
报错代码:
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("buildDate").from(builder.getBuildDate());
正确方式:
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("buildDate").from(builder.getBuildDate().getTime());