0

This is my mapping for one of the properties in my ElasticSearch model:

"timestamp":{
  "type":"date",
  "format":"dd-MM-yyyy||yyyy-MM-dd'T'HH:mm:ss.SSSZ||epoch_millis"
}

I'm not sure if I'm misunderstanding the documentation. It clearly says:

The first format will also act as the one that converts back from milliseconds to a string representation.

And that is exactly what I want. I would like to be able to read directly (if possible) the dates as dd-MM-yyyy.

Unfortunately, when I go to the document itself (so, accessing to the ElasticSearch's endpoint directly, not via the application layer) I still get:

"timestamp" : "2014-01-13T15:48:25.000Z",

What am I missing here?.

1
  • 2
    what you get back is what you store in the source, ES will never modify the source document you're sending to it, unless you configure an ingestion pipeline to make some modifications. The format is used by ES in order to know how to interpret and index your data. Commented Nov 16, 2018 at 11:52

1 Answer 1

1

As @Val mentioned, you'd get the value/format as how it is being indexed.

However if you want to view the date in particular format regardless of the format it has been indexed, you can make use of Script Fields. Note that it would be applied at querying time.

Below query is what your solution would be.

POST <your_index_name>/_search
{
  "query":{
    "match_all":{ }
  },
  "script_fields":{
    "timestamp":{
       "script":{
         "inline": "def sf = new SimpleDateFormat(\"dd-MM-yyyy\");def dt = new Date(doc['timestamp'].value);def mydate = sf.format(dt);return mydate;"
       }
    }
  }
}

Let me know how it goes.

Sign up to request clarification or add additional context in comments.

2 Comments

Any luck with the above query? Does it fulfil your requirement?
It does @Kamal thank you!. Not completely satisfied with the fact that it has to be specified for every query and I cannot decide something like a default format, but I understand now why that is. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.