1.Elasticsearch date ç±»å详解
Elasticsearch date ç±»å详解
å¼è¨
ä¸ç´å¯¹ elasticsearch ä¸ç date ç±»å认è¯æ¯è¾æ¨¡ç³ï¼èä¸å¨ä½¿ç¨ä¸åæ¯è¾å¸¸è§,源码owllook网站源码è¿æ¬¡å³å®å¤è±äºæ¶é´ï¼å½»åºå¼æå®ï¼å¸æè½å¯¹ç¨å°çåå¦æä¾å¸®å©ã
注æï¼æ¬ææµè¯ä½¿ç¨æ¯ elasticsearch 6.3 çæ¬ï¼å ¶ä»çæ¬è§åå¯è½ææååï¼å¦éå°ä¸å ¼å®¹çæ åµï¼è¿è¯·åè å®æ¹ææ¡£
Date æ°æ®ç±»å
elasticsearch æ°æ®æ¯ä»¥ JSON æ ¼å¼åå¨çï¼è JSON ä¸æ¯å¹¶æ²¡æ date æ°æ®ç±»åï¼å æ¤ Elasticsearch ä¸è½ç¶æ date ç±»åï¼ä½å¨å±ç¤ºæ¶å´è¦è½¬åæå¦å¤çæ ¼å¼ã
date ç±»åå¨ Elasticsearch å±ç¤ºçæ ¼å¼æä¸é¢å ç§ï¼
å°æ¥ææ¶é´æ ¼å¼ååçå符串ï¼å¦ "--" æè "// ::"
long åçæ´æ°ï¼æä¹æ¯ milliseconds-since-the-epoch ï¼ç¿»è¯ä¸ä¸å°±æ¯èª -- :: UTC 以æ¥ç»è¿ç毫ç§æ°ã
int åçæ´æ°ï¼æä¹æ¯ seconds-since-the-epoch , æ¯æèª -- :: UTC 以æ¥ç»è¿çç§æ°ã
å两ç§çæè¿°éé½å å« UTC ï¼ä»ä¹æ¯ UTC å¢ï¼
UTC(Universal Time Coordinated) å«åä¸çç»ä¸æ¶é´ï¼ä¸å½å¤§éå UTC çæ¶å·®æ¯ + 8 ,ä¹å°±æ¯ UTC+8ã
ä¸è®º date æ¯ä»ä¹å±ç¤ºæ ¼å¼ï¼å¨ Elasticsearch å é¨åå¨æ¶é½æ¯è½¬æ¢æ UTCï¼å¹¶ä¸ææ¶åºä¹ä¼è®¡ç®è¿å»ï¼ä»èå¾å° milliseconds-since-the-epoch 并ä½ä¸ºåå¨çæ ¼å¼ã
å¨æ¥è¯¢æ¥ææ¶ï¼ä¼æ§è¡ä¸é¢çè¿ç¨ï¼
转æ¢æ long æ´å½¢æ ¼å¼çèå´(range) æ¥è¯¢
å¾å°èåçç»æ
å°ç»æä¸ç date ç±»åï¼long æ´åæ°æ®ï¼æ ¹æ® date format å段转æ¢å对åºçå±ç¤ºæ ¼å¼
Date çé»è®¤æ ¼å¼
Date çæ ¼å¼åç±»åæ¯å¯ä»¥éè¿ format æ¥æå®çï¼å¦æ没ææå®ï¼å°±ä¼ä½¿ç¨é»è®¤çæ ¼å¼ï¼
"strict_date_optional_time||epoch_millis"
è¿è¡¨ç¤ºä»ä¹ææå¢ï¼
å æ¥å¼æ strict_date_optional_time
A generic ISO datetime parser where the date is mandatory and the time is
optional. Full details here .
è¿æ¯ elasticsearch å®ç½ç解éï¼è¡¨ç¤ºåªè¦æ¯ ISO datetime parser å¯ä»¥æ£å¸¸è§£æçé½æ¯ strict_date_optional_time ãé½æåªäºè¯æ³å¢ï¼
date-opt-time=date-element ['T'[time-element] [offset]]date-element = std-date-element | ord-date-element | week-date-element std-date-element = yyyy ['-'MM ['-'dd]] ord-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] fraction = ('.'|',') digit+
å ¶ä¸ä¸æ¬å·å çé½æ¯å¯éçï¼å¯å¡«å¯ä¸å¡«ã以 std-date_element 举个ä¾å
--
-
ä¸é¢ 3 ç§æ ¼å¼é½æ»¡è¶³è¦æ±ã
é¤äº strict_date_optional_time ï¼è¿å¯ä»¥æ¯ epoch_millis æ ¼å¼ï¼å³ epoch 以æ¥ç毫ç§æ°ã
举个ä¾å
PUT my_index{ "mappings": { "_doc": { "properties": { "date": { "type":"date"} } } }}PUT my_index/_doc/1{ "date":"--"} PUT my_index/_doc/2{ "date":"--T::Z"} PUT my_index/_doc/3{ "date":} GET my_index/_search{ "sort": { "date":"asc"}
ä¸é¢ç PUT 请æ±ä¸ç date æ°æ®å满足é»è®¤çè¦æ±ã
å¦ä½æå®å¤ä¸ª date æ ¼å¼?
åä¸ä¸ª date å段å¯ä»¥æå®å¤ä¸ª date æ ¼å¼ï¼åªè¦ä½¿ç¨ || åéå°±å¯ä»¥äºãå¨ç´¢å¼ï¼é½ä¼å¯¹ date æ ¼å¼æ¨ä¸ªè¿è¡å¹é ï¼ç´å°æ¾å°å¹é çæ ¼å¼ä¸ºæ¢ã
å¦æåå¨æ¶ date æ ¼å¼ä¸º milliseconds-since-the-epoch ï¼å¨æ¥è¯¢æ¶ä¼å°å ¶è½¬æ¢ä¸ºæå®ç第ä¸ä¸ª date æ ¼å¼ã
举个ä¾å,æå ´è¶£çåå¦å¯å¨ sense ä¸å¨æå®è·µä¸ã
PUT my_index{ "mappings": { "doc": { "properties": { "date": { "type":"date","format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"} } } }}PUT /my_index/doc/1{ "date":"-- ::"}PUT /my_index/doc/2{ "date":"--"}GET my_index/_search{ "query": { "match_all": { } }}
å°ç»
æ¬æ主è¦è®²è§£äº elsaticserach ä¸ç date ç±»åæ ¼å¼ï¼è¯æ³å转æ¢è¿ç¨ãelasticsearch ä¸åå¨ date ç±»åç»ä¸ä½¿ç¨ milliseconds-since-the-epoch æ ¼å¼ï¼å±ç¤ºæ¶æ ¹æ®ä¸åæ ¼å¼èåºåã 欢è¿å·¥ä½ä¸å°äºå¹´çJavaå·¥ç¨å¸æå们å å ¥Java群ï¼
群å æä¾å è´¹çJavaæ¶æå¦ä¹ èµæï¼éé¢æé«å¯ç¨ãé«å¹¶åãé«æ§è½ååå¸å¼ãJvmæ§è½è°ä¼ãSpringæºç ï¼MyBatisï¼Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginxçå¤ä¸ªç¥è¯ç¹çæ¶æèµæï¼åçå©ç¨èªå·±æ¯ä¸åæ¯ä¸ç§çæ¶é´æ¥å¦ä¹ æåèªå·±ï¼ä¸è¦åç¨"没ææ¶é´âæ¥æ©é¥°èªå·±ææ³ä¸çææ°ï¼è¶å¹´è½»ï¼ä½¿å²æ¼ï¼ç»æªæ¥çèªå·±ä¸ä¸ªäº¤ä»£ï¼