0
我在elasticsearch中創建了index
和doc
。爲doc添加映射。elasticsearch如果未在映射中啓用,則不在字段中返回ttl
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}
當我創建文檔時,我給了文檔ttl
。
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}'
當我試圖讓使用新映射的ttl
在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
} ]
}
}
啓用ttl
索引。
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"
}
}
}
}
}
}'
然後添加新記錄。
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
}
,並嘗試再次獲得ttl
,它在領域返回ttl
。
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
}
} ]
}
}
在映射中啓用ttl
以強制在文檔中生效?
是的,默認情況下'_ttl'沒有啓用,所以你需要啓用它才能使TTL工作,但它不會影響已經創建的文檔。 – Val
@Val如果它無法設置'ttl',那麼'PUT'調用應該會引發錯誤?因爲,我得到'created:true',那麼我認爲它取得了我所有的價值觀,有沒有什麼辦法來定義這個問題,如果有些東西不起作用,請告訴我。 – Nilesh
如果'_ttl'未啓用,'ttl'參數會被忽略,因此您將不會收到任何錯誤。知道你的映射以及你是否啓用了TTL是你工作的一部分。 – Val