Erik Iverson
Erik Iverson

Reputation: 882

How does Elasticsearch convert dates to JSON string representations?

I have a question about how Elasticsearch converts dates to JSON string representations. I'm using version 1.4.2 for this example.

First, POST a simple document containing a date field called 'postDate' to 'myindex' with type 'datetest':

curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
    "content": "Hello World!",
    "postDate": "2009-11-15T14:12:12"
}'

We can look at the field mapping for postDate that was automatically generated.

curl -XGET "http://localhost:9200/myindex/_mapping/"

##postDate":{"type":"date","format":"dateOptionalTime"}

Now let's POST another document. This time postDate has a UTC offset included.


curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
    "content": "Hello World!",
    "postDate": "2009-11-15T14:12:12-07:00"
}'

Now, let's retrieve our documents that we created above:

curl -XGET 'http://localhost:9200/myindex/datetest/_search?q=Hello'

  "hits": {
    "total": 2,
    "max_score": 0.11506981,
    "hits": [
      {
        "_index": "myindex",
        "_type": "datetest",
        "_id": "AUw0TyJgqFHXxhSON3r8",
        "_score": 0.11506981,
        "_source": {
          "content": "Hello World!",
          "postDate": "2009-11-15T14:12:12"
        }
      },
      {
        "_index": "myindex",
        "_type": "datetest",
        "_id": "AUw0VIQbqFHXxhSON3r-",
        "_score": 0.095891505,
        "_source": {
          "content": "Hello World!",
          "postDate": "2009-11-15T14:12:12-07:00"
        }
      }
    ]
  }

You see that postDate is formatted in each of the documents returned exactly as it was POSTed. This is confusing to me though. According to,

http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html

"The date type is a special type which maps to JSON string type. It follows a specific format that can be explicitly set. All dates are UTC. Internally, a date maps to a number type long, with the added parsing stage from string to long and from long to string." (emphasis mine)

My two questions are:

1) Since, "internally, a date maps to a number type long", how or where is elasticsearch storing the meta-information on how the date was POSTed?

2) Am I able to specify an output-format for dates, regardless of how they were POSTed. That is, can I control the "long to string" conversion independently of how the date was input?

Upvotes: 1

Views: 1250

Answers (1)

Roger
Roger

Reputation: 348

The source is stored as is and, when you retrieve it, Elasticsearch returns the same document you posted.

It "stores" long type in the index, and uses it to perform the queries internally, but you don't see it.

Do not confuse source with index.

Like when you index a string "Erik Iverson", it would save in the index 2 terms [erik] [iverson], but when you retrieve the document, you get the original string because the source wasn't changed

Upvotes: 2

Related Questions