How to make elticearch document ttl work?

I installed debian package

I can push data using curl:

curl -XPUT 'http://mybox:9200/blog/user/dilbert' -d '{ "name": "Dilbert Brown" }' 

And select it

 curl -XGET 'http://mybox:9200/blog/user/dilbert' 

result:

 { "_index": "blog", "_type": "user", "_id": "dilbert", "_version": 2, "exists": true, "_source": { "name": "Dilbert Brown" } } 

And find it with

 curl -XGET 'http://mybox:9200/blog/user/_search?q=name:Dilbert+Brown&pretty=True' 

I want to click the same record with ttl 5 seconds and 5 seconds later to get 404 http status code when trying to retrieve this record. Also, the entry should not appear in search results.

NOTE. I have tried various combinations of search configurations from

None of them helped me.

Can someone mention a simple sequence of steps that would allow me to achieve the target result?

+6
source share
3 answers

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 
+11
source

@bereal is right.

For ttl to work, you must first enable it when matching (by default, it is 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, contact

+7
source

Please note that you can only set TTL when creating a new mapping (type). You cannot enable it after creation. I have already tried many ways to enable it after the release of the product, but I can’t. So I have to update the code to use the new one that was created with TTL support. For more information, I see here: http://grokbase.com/t/gg/elasticsearch/132v5y0w11/problem-with-ttl

0
source

Source: https://habr.com/ru/post/946488/


All Articles