Store Date Format in elasticsearch
52,826
You are nearly there. Set your mapping like this:
{"LastUpdate": {
"type" : "date",
"format" : "yyyy/MM/dd HH:mm:ss"}
}
Read the docs on the date mapping and its options and the date format parameter (one of the options to the date mapping).
Good luck!
Related videos on Youtube
Author by
Jimmy Lin
Updated on July 09, 2022Comments
-
Jimmy Lin almost 2 years
I met a problem when I want to add one datetime string into Elasticsearch.
The document is below:
{"LastUpdate" : "2013/07/24 00:00:00"}
This document raised an error which is
"NumberFormatException" [For input string: \"20130724 00:00:00\"]
I know that I can use the Date Format in Elasticsearch, but I don't know how to use even I read the document on the website.
{"LastUpdate": { "properties": { "type": "date", "format": "yyyy-MM-dd"} } }
and
{"LastUpdate": { "type": "date", "format": "yyyy-MM-dd" } }
are wrong.
How can I transfer the datetime string into date format in Elasticsearch?
How can I store the datetime string directly into Elasticsearch?
-
Mohan Kumar over 8 yearsone can use double-pipe to support multiple format. e.g. "yyyMMdd||yyyy-MM-dd"
-
-
Jimmy Lin almost 11 yearsHow "format" knows the datetime string is "2013/07/24 00:00:00" ? Or it will use the datetime now ? I change to "LastUpdate": {"type": "date", "format": "yyyy/MM/dd HH:mm:ss"}, it raised another error which is "ElasticSearchIllegalArgumentException[unknown property [type]]"
-
ramseykhalaf almost 11 yearsThat is the mapping I gave. The
"format": "yyyy/MM/dd HH:mm:ss"
tells elasticsearch to accept dates in that format. -
Jimmy Lin almost 11 yearsHow can I push/let elasticsearch know the what data is ?
-
Jimmy Lin almost 11 yearsOh, I should define the data type before I index them, right ?
-
ramseykhalaf almost 11 yearsAh, you need to set up a mapping first. I made a gist.
-
ramseykhalaf almost 11 yearsLook at the elasticsearch docs on put mapping api
-
ramseykhalaf almost 11 yearsThink of mapping = schema in SQL world. Not really that accurate, but will help you understand what mapping means
-
Jimmy Lin almost 11 yearsDone, one more question. Is id must be a number, I use sha1 of one string, and it's seems doesn't work. I found that your search is also brilliant.
-
Jimmy Lin almost 11 yearsWhat's the different between {key : 1} and {"key" : 1} in elasticsearch ? You didn't use double quote, but still works.
-
ramseykhalaf almost 11 yearsYou should use double quotes, I was being lazy, and some places it works, but it is NOT the standard.
_id
is stored as a string. If you give a number as the_id
it will be stored as a string representation of that number. -
Maxrunner over 9 yearsAny reason why in my document the date appears as long even with the format as custom?
-
ramseykhalaf over 9 yearsI believe the date is parsed according to your format. It will then be stored as some kind of
datetime
internally, which is stored as aLong
(in Java) representing the number of milliseconds since 1 Jan 1970. -
Maxrunner over 9 yearsOh ok thanks! Still having trouble with using the range on dates.
-
ocergynohtna about 9 yearsDead links in answer. Here's the updated urls. Core Types: elastic.co/guide/en/elasticsearch/reference/current/… Mapping Date: elastic.co/guide/en/elasticsearch/hadoop/current/…
-
ramseykhalaf about 9 years@ocergynohtna Thanks for the heads up. But actually the links you gave weren't exactly what I was linking to prior. Have updated answer.