Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Detailed description of flexible search date type
Detailed description of flexible search date type
introduce

I have been vague about the date type in elasticsearch, which is very common in use. I decided to spend more time to get to know it thoroughly this time, hoping to help the students who use it.

Note: This article uses version 6.3 of Flexible Search, and the rules of other versions may change. If there is any incompatibility, please refer to? official document

Date data type

Elasticsearch data is stored in JSON format, while? Isn't it in JSON? Dating? Data type, so although it is in Elasticsearch? Dating? Type, but it must be converted to another format when displayed.

Dating? Types are displayed in Elasticsearch in the following format:

A string in date and time format, such as "2015-01-01"or "2015/0/012:"

What does an integer of type long mean? Milliseconds from the era, translated into self? 1970-0 1-0 1 00:00:00 UTC? The number of milliseconds that have passed since then.

Integer of type int, what do you mean? The number of seconds since the era refers to since? 1970-0 1-0 1 00:00:00 UTC? The number of seconds since.

The latter two descriptions both contain UTC. What is UTC?

UTC (Coordinated Universal Time) is called Universal Time, and the time difference between Chinese mainland and UTC is +8, which means UTC+8.

No matter what display format the date is, it is converted to UTC when stored in Elasticsearch, and the time zone is also counted. The number of milliseconds since the era? And as a storage format.

When querying the date, the following process will be performed:

Range query is converted to long integer format.

Get the result of polymerization

According to the date format field, the date type (long integer data) in the result is converted back to the corresponding display format.

Default format of date

Can the format type of the date be passed? Format? To specify, if not specified, the default format will be used:

"Strict Date Optional Time || Epoch Milliseconds"

What does this mean?

Let's make it clear first, shall we be strict _ Date _ Optional _ Time?

A universal ISO date-time parser, in which date is required and time is

Optional. ? The complete details are here.

This is elasticsearch official website's explanation, which means that as long as the ISO datetime parser can parse normally, what is it? Strict date and optional time. What are the grammars?

Date-opt-time = date-element ['t' [time-element] [offset]] Date element? = Standard Date Element | Order Date Element | Week Date Element Standard Date Element? = yyyy ['-'mm ['-'DD]] Order-Date-Element? = yyyy ['-'DDD] week date element = xxxx '-W' ww['-' e] time element? = hh [minute-element] | [fraction] minute-element =':' mm [second-element] | [fraction] second-element =':' ss [fraction] decimal? = ('.|',') number+

Among them, everything in brackets is optional and can be filled in or not. With what? Standard date element? take for example

20 18- 1 1- 19

20 18

20 18- 1 1

The above three formats all meet the requirements.

Except? strict_date_optional_time? Or in the format of epoch_millis, that is, the number of milliseconds from epoch.

take for example

Put my _ index {"mappings": {"_ doc": {"properties": {"date": {"type": "date"}? } }? } } PUT my _ index/_ doc/ 1 { " date ":" 20 15-0 1-0 1 " } PUT my _ index/_ doc/2 { " date ":" 20 15-0 1-0 12: 10:30Z " } PUT my _ index/?

The date data in the PUT request above all meet the default requirements.

How to specify multiple date formats?

You can specify multiple date formats for the same date field, as long as you use? ||? Just separate. In the index, date formats will be matched one by one until a matching format is found.

If the date format is. The number of milliseconds since the era? , which will be converted to the specified first date format when querying.

For example, interested students can practice on the senses.

Put my _ index {"mappings": {"doc": {"properties": {"date": "date", "format": "yyyy-mm-DD hh: mm: SS ||| yyyy-mm-DD||| epoch _ millis"}? } }? } } PUT/my _ index/doc/ 1 { " date ":" 20 18-09-24 19:23:45 " } PUT/my _ index/doc/2 { " date ":" 20 18-09-25 " } GET my _ index/_ search { " query ":{ " match _ all ":{ }? }}

summary

This paper mainly expounds the format, grammar and conversion process of date types in elsaticserach. Are the date types stored in elasticsearch uniformly used? The number of milliseconds since the era? Format, displayed in different formats and distinguished. ? Welcome friends who have been Java engineers for one to five years to join the Java group: 89 12 19277.

Provide free Java architecture learning materials in the group (including high availability, high concurrency, high performance and distribution, Jvm performance tuning, Spring source code, MyBatis, Netty, Redis, Kafka, MySQL, Zookeeper, Tomcat, Docker, Dubbo, Nginx and other knowledge points). Make rational use of every minute to learn and improve yourself, and don't use it any more. Fight hard while you are young, and give yourself an explanation in the future!