0

I created index and doc in . 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 ?

Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • Yes, `_ttl` is not enabled by default, so you need to enable it in order for TTL to work, but it won't affect already created documents. – Val Jul 26 '16 at 18:54
  • @Val if its not able to set `ttl` then `PUT` call should raise error? Because, I get `created: true`, then I assume it take all my values, is there any way to define that, if something not works, let me know. – Nilesh Jul 26 '16 at 19:43
  • The `ttl` parameter is silently ignored if `_ttl` is not enabled, you won't get any error because of that. It's part of your job to know your mappings and if you enabled TTL or not. – Val Jul 27 '16 at 03:56
  • Is there any performance issue if we enable `ttl` or not ? if we enable `ttl` and planning to use that for document in future then its good or we can enable when we want ? – Nilesh Jul 27 '16 at 13:55
  • You can enable ttl at any time. So given the increased work to support it, you should only enable it when needed. – Val Jul 27 '16 at 13:58
  • Thanks @Val, I will work on this. can you please post this in answer, if someone has same query, he/she can get answer from this. – Nilesh Jul 27 '16 at 14:00
  • @Val this is right or not, I am not sure, but I have another question for elasticsearch http://stackoverflow.com/questions/38533990/use-existing-field-as-id-using-elasticsearch-dsl-python-doctype – Nilesh Jul 28 '16 at 19:14

1 Answers1

1

Yes, _ttl is not enabled by default, so you need to enable it in order for TTL to work, but it won't affect already created documents.

Note that the ttl parameter is silently ignored if _ttl is not enabled in your mapping, you won't get any error because of that. It's part of your job to know your mappings and if you enabled TTL or not.

You can enable _ttl at any time, so given the increased work to support it, you should only enable it when needed.

Val
  • 207,596
  • 13
  • 358
  • 360