Updating default ttl value in elasticsearch?

I know that when creating the mapping we can give something like,

        "_ttl" : {
          "enabled" : true,
          "default" : 900000
        }

to enable _ttl with a default value. I would like to know whether can be update this default _ttl value by updating the mapping ?

Thanks. Any help is highly appreciated.

Hi @Rajind_Ruparathna,

let me start by pointing out that _ttl is deprecated in Elasticsearch 2.x.

Did you try to update the mapping?

Here is a complete example that worked fine for me (tested with Elasticsearch 2.3.4):

DELETE /my_index

PUT /my_index
{
  "mappings": {
    "my_type": {
      "_ttl": {
        "enabled": true,
        "default": "1m"
      }
    }
  }
}

PUT /my_index/my_type/1
{
  "text": "Will expire in 1 minute"
}

GET /my_index/my_type/1


PUT /my_index/_mapping/my_type
{
      "_ttl": {
         "enabled": true,
         "default": "24h"
      }
}

PUT /my_index/my_type/2
{
  "text": "Will expire in a day"
}

GET /my_index/my_type/2

Daniel

1 Like

Thanks. I will try that. I did try to update the mapping but got indexAlreadyExistsException. I was doing something wrong back then.

If you get that it is usually because you are using the create index API to
update the mapping. They look fairly similar and I've mixed them up a few
times so I know the error message well.

1 Like

yes. that was the issue. thanks.

@nik9000, @danielmitterdorfer One small question, say I have set a default _ttl value and now i want to update the mapping so that there is no such default ttl value. I've tried with,
{
"_ttl": {
"enabled": true,
}
}

but doesn't seem to remove the default _ttl value.

Hi @Rajind_Ruparathna,

that's correct. This is not possible. Issuing:

PUT /my_index/_mapping/my_type
{
      "_ttl": {
         "enabled": false
      }
}

throws an error:

{
   "error": {
      "root_cause": [
         {
            "type": "illegal_argument_exception",
            "reason": "_ttl cannot be disabled once it was enabled."
         }
      ],
      "type": "illegal_argument_exception",
      "reason": "_ttl cannot be disabled once it was enabled."
   },
   "status": 400
}

You have to create a new index if you need to disable it.

Daniel

1 Like

Yes. I tried and got the same result. Thanks. cheers.

You're welcome. Glad that I could help you.