The default date format in Elasticsearch

In Elasticsearch, when you define a field type as datea type, you can store dates in a parseable format that Elasticsearch can then convert to a long number in milliseconds for internal storage. The default date format is "strict_date_optional_time||epoch_millis".

This format includes the following two date formats:

  1. strict_date_optional_time: This date format consists of a date and an optional time. If no time is provided, it defaults to midnight. This date format usually looks like 2023-07-11T20:30:00Zor 2023-07-11. This format strictly follows the ISO 8601 date and time format, which means that it must contain the year, month, and date, and can also contain information such as time and time zone.

  2. epoch_millis: This is the number of milliseconds since 1970-01-01T00:00:00Z, a common Unix timestamp notation.

So, when you send a date as a string to Elasticsearch, it tries to parse the string to match one of these two formats. Then, this date will be converted to the corresponding long number and stored.

However, it's worth noting that when you retrieve this date field from Elasticsearch, it will be returned in the default date format unless you specify a different date format in the request.

dateThe default date format of the Elasticsearch type is "strict_date_optional_time||epoch_millis", which does not include date formats such as "20230711". If you want to store dates in this format, you need to specify a custom date format when creating the index.

For example, you can use a date format like "yyyyMMdd" in your mapping:

{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "SETTLE_DATE_LOC": {
    
    
        "type": "date",
        "format": "yyyyMMdd"
      }
    }
  }
}

In this case, the date format "20230711" can be stored and parsed correctly.

Note that the above mapping defines a new date format, which only includes year, month, day, and does not include time or time zone information. The date format is "yyyyMMdd". Using this date format, you can represent dates as "20230711".


From an SQL point of view, DATE_FORMAT(a.SETTLE_DATE_LOC, '%Y-%m-%d') as SETTLE_DATE_LOCthis statement is correct. This will a.SETTLE_DATE_LOCconvert the value of the field to a date string in 'YYYY-MM-DD' format.

Then, if you want to save this date string to Elasticsearch, you need to ensure that the SETTLE_DATE_LOCmapping type of the field in Elasticsearch is dateand can parse the date in the format of 'YYYY-MM-DD'.

Fortunately, this date format is one of Elasticsearch's default date formats, so you don't need to specify any custom formats. SETTLE_DATE_LOCJust set the type of the field in Elasticsearch to date, and then you can save the date string in the format of 'YYYY-MM-DD' to this field.

Guess you like

Origin blog.csdn.net/u011197085/article/details/131651588