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!

Share:
52,826

Related videos on Youtube

Jimmy Lin
Author by

Jimmy Lin

Updated on July 09, 2022

Comments

  • Jimmy Lin
    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
      Mohan Kumar over 8 years
      one can use double-pipe to support multiple format. e.g. "yyyMMdd||yyyy-MM-dd"
  • Jimmy Lin
    Jimmy Lin almost 11 years
    How "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
    ramseykhalaf almost 11 years
    That is the mapping I gave. The "format": "yyyy/MM/dd HH:mm:ss" tells elasticsearch to accept dates in that format.
  • Jimmy Lin
    Jimmy Lin almost 11 years
    How can I push/let elasticsearch know the what data is ?
  • Jimmy Lin
    Jimmy Lin almost 11 years
    Oh, I should define the data type before I index them, right ?
  • ramseykhalaf
    ramseykhalaf almost 11 years
    Ah, you need to set up a mapping first. I made a gist.
  • ramseykhalaf
    ramseykhalaf almost 11 years
    Look at the elasticsearch docs on put mapping api
  • ramseykhalaf
    ramseykhalaf almost 11 years
    Think of mapping = schema in SQL world. Not really that accurate, but will help you understand what mapping means
  • Jimmy Lin
    Jimmy Lin almost 11 years
    Done, 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
    Jimmy Lin almost 11 years
    What's the different between {key : 1} and {"key" : 1} in elasticsearch ? You didn't use double quote, but still works.
  • ramseykhalaf
    ramseykhalaf almost 11 years
    You 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
    Maxrunner over 9 years
    Any reason why in my document the date appears as long even with the format as custom?
  • ramseykhalaf
    ramseykhalaf over 9 years
    I believe the date is parsed according to your format. It will then be stored as some kind of datetime internally, which is stored as a Long (in Java) representing the number of milliseconds since 1 Jan 1970.
  • Maxrunner
    Maxrunner over 9 years
    Oh ok thanks! Still having trouble with using the range on dates.
  • ocergynohtna
    ocergynohtna about 9 years
    Dead 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
    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.