英文:
Does Spring Data Elasticsearch support multiple date formats for Date Fields
问题
我了解到Elasticsearch支持对日期类型字段使用多种日期格式,但在Spring Data Elasticsearch字段注解中似乎没有看到类似的支持。看起来Spring Data Elasticsearch只接受一个格式的值。
是否可以像Elasticsearch文档中所述的那样添加多个日期格式?
也许可以像这样:
@Field(type = Date, format = {DateFormat.year_month_day, DateFormat.year_month, hour_minute_second_millis})
private java.util.Date day;
英文:
I see Elasticsearch supports multiple date formats for date type fields but could not see the same in Spring Data Elasticsearch field annotation. It seems that Spring Data Elasticsearch only accepts one value for format.
Is it possible to add multiple dates formats as detailed here in the Elasticsearch docs?
Maybe something like:
@Field(type = Date, format = {DateFormat.year_month_day, DateFormat.year_month, hour_minute_second_millis})
private java.util.Date day;
答案1
得分: 1
这可以通过自定义日期格式来实现。
@Field(type = Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private long date;
这使得可以使用纪元日期(epoch date)来保存文档,然后您还可以利用多种日期格式来进行简单的查询:
rangeQuery("date").lt(LocalDate.of(2020, Month.APRIL, 5));
英文:
This can be done using the custom date format.
@Field(type = Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private long date;
Which allows saving documents using epoch date for example, and then you can also leverage the multiple date formats for easy querying:
rangeQuery("date").lt(LocalDate.of(2020, Month.APRIL, 5));
答案2
得分: 0
尝试使用这个自定义日期格式:
@Field(type = FieldType.Date, format = {}, pattern = {"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"})
英文:
Try this custom date format:
@Field(type = FieldType.Date, format = {}, pattern = {"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论