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