Search code examples
javadateelasticsearchdate-format

Elastic Date: The date gets stored as OffsetDateTime in an object format


The data I am indexing into elastic is an OffsetDateTime from Java in the following manner.

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

I am storing this offsetDate into elastic and it converts into the following..

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

I want to use something simple like the following to query it but its not working because the formats are all different. How do I go about changing the format of the data that is getting indexed?

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

Mapping:

"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"
                                        }
                                    }
                                }
                            }
                        },

Solution

  • yes the index was mapped wrongly, I used a json serializer and it works fine now after the mapping is of "type":"date".

    Thanks Sahil Gupta.