如何在ElasticSearchRepository中使用CRUD应用日期范围查询?

时间:2018-07-11 11:25:40

标签: java elasticsearch crud spring-data-elasticsearch

我正在尝试使用Java springboot在findbyTimeStampBetween中的ElasticSearchRepository方法中获取两个时间戳之间的记录。

该方法仅接受Date对象。当我通过相同的操作时,会出现以下错误。

  

ElasticsearchParseException [无法解析格式为[date_optional_time]的日期字段[IST 2018年7月11日星期三15:19:52]。

我尝试以必需的格式传递String。它无法获取任何记录。使用LocalDateTime(默认情况下格式化为ISO),出现以下错误。

  

UncategorizedExecutionException [执行失败];嵌套:IOException [无法写入类型[class java.time.LocalDateTime]]];

    Date currentTS=new Date();
            Date lastLoadTs=ts.getTimestampValue();
    //ts is an object that stores some timestamp fetched from elasticsearch
    Iterable<Data> dataList = demoService
                    .findDataByTimestampBetween(lastLoadTs,currentTS);

    //ElasticSearchRepository is abstract so here is the interface 
    public interface ESDemoRepository extends ElasticsearchRepository<Data, 
    String>{


    Iterable<Data> findDataByTimestampBetween(Date lastLoadTimeStamp, Date 
    currentTime);

    }
  //Model foe type Data. I need to fetch records from here based on 
   //@timestamp range
    public class Data {
    @Id
    private Integer id;

//    ("EEE MMM dd HH:mm:ss yyyy||EEE MMM  dd HH:mm:ss yyyy")
    @JsonProperty(value = "@timestamp")
    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM- 
    dd'T'HH:mm:ss.SSS")
    private Date timestamp;

    //timestamp class
    @Document(indexName = "timestamp-index", type="doc")
    public class TimeStamp {
    @Id
    private Integer timestampId;
    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @JsonProperty(value = "timestampValue")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM- 
    dd'T'HH:mm:ss.SSS")
    private Date timestampValue;

0 个答案:

没有答案