【问题标题】:Elastic Date: The date gets stored as OffsetDateTime in an object format弹性日期:日期以对象格式存储为 OffsetDateTime
【发布时间】:2021-05-22 08:33:06
【问题描述】:

我在 elastic 中索引的数据是来自 Java 的 OffsetDateTime,方式如下。

Timestamp closedDate = resultSet.getTimestamp("closedon");
org.threeten.bp.OffsetDateTime offsetDate= Instant.ofEpochMilli(closedDate.toInstant().toEpochMilli())
                    .atOffset(ZoneOffset.UTC)

我将此 offsetDate 存储到 elastic 中,它会转换为以下内容..

"closedDate": {
                        "dateTime": {
                            "date": {
                                "year": 2020,
                                "month": 6,
                                "day": 15
                            },
                            "time": {
                                "hour": 11,
                                "minute": 26,
                                "second": 1,
                                "nano": 37000000
                            }
                        }

我想使用类似以下的简单方法来查询它,但它不起作用,因为格式都不同。如何更改要编入索引的数据的格式?

{
           "range": {
            "closedDate": {
              "gte": "2021-05-22T00:00:00.000Z",
              "lte": "2021-02-01T00:00:00.000Z",
              "format": "strict_date_optional_time"
            }
          }
        }

映射:

"closedOn": {
                    "properties": {
                        "dateTime": {
                            "properties": {
                                "date": {
                                    "properties": {
                                        "day": {
                                            "type": "long"
                                        },
                                        "month": {
                                            "type": "long"
                                        },
                                        "year": {
                                            "type": "long"
                                        }
                                    }
                                },
                                "time": {
                                    "properties": {
                                        "hour": {
                                            "type": "long"
                                        },
                                        "minute": {
                                            "type": "long"
                                        },
                                        "nano": {
                                            "type": "long"
                                        },
                                        "second": {
                                            "type": "long"
                                        }
                                    }
                                }
                            }
                        },

【问题讨论】:

  • 您能分享一下您的索引映射吗?我不认为 'closedDate' 属于 'date' 类型
  • @SahilGupta,无法将映射放在 cmets 中,所以我用映射编辑了问题..
  • wl 更改映射并尝试@SahilGupta

标签: java date elasticsearch date-format


【解决方案1】:

是的,索引映射错误,我使用了 json 序列化程序,在映射为 "type":"date" 后它现在可以正常工作了。

感谢萨希尔·古普塔。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多