How to make elasticsearch document ttl work?

12,343

Solution 1

Here is what works for me:

curl -XPUT 'http://localhost:9200/blog/user/_mapping' -d '{"user": {"_ttl": {"enabled": true, "default": 5000}}}'

curl -XPUT 'http://localhost:9200/blog/user/phb' -d '{"name" : "Pointy-Haired Boss"}'

sleep 60  # this is the default deletion interval for the expired documents

curl -XGET http://localhost:9200/blog/user/phb  # 404

Solution 2

@bereal is right.

For ttl to work, you have to enable it first in mapping (by default, it's disabled), and then set the TTL value when indexing documents.

curl -XPUT 'mybox:9200/blog/user/_mapping?pretty' -d '{
  "user": {
     "_ttl": {"enabled": true}
}'

curl -XPUT 'mybox:9200/blog/user/dilbert' -d '{ "name" : "Dilbert Brown", "_ttl": "3m"}'

curl -XGET 'mybox:9200/blog/user/dilbert?pretty'

For more information, please reference

Share:
12,343
Archibald
Author by

Archibald

Updated on June 17, 2022

Comments