I created index
and doc
in elasticsearch. Add mapping for doc.
curl http://localhost:9200/test -X POST
{"acknowledged":true}
curl http://localhost:9200/test/student_doc/_mappings -X PUT -d '{
"student_doc" : {
"properties" : {
"name" : {
"properties" : {
"student_id" : {
"type" : "string"
},
"tags": {
"type" : "string"
}
}
}
}
}
}'
{"acknowledged":true}
When I create doc, I gave ttl
for the doc.
curl http://localhost:9200/test/student_doc/4?ttl=2500 -X PUT -d '{"student_id": "4", "tags": ["test"]}' -H 'Content-type: application/json'
{"_index":"test","_type":"student_doc","_id":"4","_version":1,"created":true}'
When I try to get the ttl
in fields
curl http://localhost:9200/test/_search?pretty -X POST -d '{"fields": ["_ttl"]}'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test",
"_type" : "student_doc",
"_id" : "4",
"_score" : 1.0
} ]
}
}
I enable ttl
in index using new mappings.
curl http://localhost:9200/test/student_doc/_mappings -X PUT -d '{
"student_doc" : {
"_ttl": {"enabled": true},
"properties" : {
"name" : {
"properties" : {
"student_id" : {
"type" : "string"
},
"tags": {
"type" : "string"
}
}
}
}
}
}'
Then add new record.
curl "http://localhost:9200/test/student_doc/5?ttl=2500&pretty" -X PUT -d '{"student_id": "5", "tags": ["test"]}' -H 'Content-type: application/json'
{
"_index" : "test",
"_type" : "student_doc",
"_id" : "5",
"_version" : 1,
"created" : true
}
And try to get ttl
again and it returns the ttl
in fields.
curl http://localhost:9200/test/_search?pretty -X POST -d '{"fields": ["_ttl"]}'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test",
"_type" : "student_doc",
"_id" : "4",
"_score" : 1.0
}, {
"_index" : "test",
"_type" : "student_doc",
"_id" : "5",
"_score" : 1.0,
"fields" : {
"_ttl" : -420
}
} ]
}
}
It is compulsory to enabled ttl
in mappings to get it effected in document ?