我创建了 index
和 doc
在 elasticsearch .为文档添加映射。
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 工作,但它不会影响已创建的文档。
请注意 ttl
如果 _ttl
,参数将被静默忽略未在您的映射中启用,因此您不会收到任何错误。了解映射以及是否启用 TTL 是您工作的一部分。
您可以启用 _ttl
在任何时候,鉴于支持它的工作增加,您应该只在需要时启用它。
关于如果未在映射中启用,elasticsearch 不会在字段中返回 ttl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38597094/