Spring Data Elasticsearch支持为日期字段使用多种日期格式吗?

huangapple go评论64阅读模式
英文:

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'"})

huangapple
  • 本文由 发表于 2020年8月22日 05:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63530215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定