Add field with timestamp in documents


(Sergey) #1

Hi!!!
I've created index:
PUT 'http://localhost:9200/twitter/
and i want that my index add automaticly @timestamp field to all my documents when indexing.
What should I do? THX for helping me.


(Nemo) #2

Did you check below link?

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-timestamp-field.html

Add timestamp field in mapping.


(Sergey) #3

@Pradeep_Gowda
Yeah I checked it but still not see it in the _source like a field.


(Nemo) #4

Can you try below?

Mapping

 PUT tweets
    {
      
      "settings": {
        "number_of_shards": 1
      },
      "mappings": {
        "_default_": {
          "_timestamp": {
            "enabled": true,
            "store": true,
            "_field_names": "_timestamp"
          }
        }
      }
    }

Insert

curl -XPUT 'http://localhost:9200/tweets/tweet/1' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:11",
    "message" : "trying out Elasticsearch"
}'

Query

GET tweets/_search 
{
"fields": ["_timestamp", "_source"],
  "query": {
    "match_all": {}
  }
}

Result

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "tweets",
            "_type": "tweet",
            "_id": "2",
            "_score": 1,
            "_source": {
               "user": "kimchy1",
               "post_date": "2009-11-15T14:12:11",
               "message": "trying out Elasticsearch"
            },
            "fields": {
               "_timestamp": 1437418271898
            }
         }
      ]
   }
}

_timestamp field will be added automatically and you can access it by _timestamp


(system) #5