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());