【发布时间】:2016-07-27 22:02:16
【问题描述】:
我在elasticsearch 中创建了index 和doc。为文档添加映射。
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}'
当我尝试在fields 中获取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" : 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 如果它不能设置
ttl那么PUT调用应该引发错误?因为,我得到created: true,然后我假设它采用了我所有的值,有没有办法定义它,如果有什么不起作用,请告诉我。 -
如果
_ttl未启用,ttl参数将被静默忽略,因此您不会收到任何错误。了解您的映射以及您是否启用了 TTL 是您工作的一部分。 -
如果我们启用
ttl是否有任何性能问题?如果我们启用ttl并计划将来将其用于文档,那么它很好,或者我们可以在需要时启用? -
您可以随时启用ttl。因此,鉴于支持它的工作量增加,您应该只在需要时启用它。
标签: elasticsearch elasticsearch mapping ttl