index
とdoc
をelasticsearchに作成しました。 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}
私はドキュメントを作成するときに、私はdocに関する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'呼び出しでエラーが発生しますか?なぜなら、私は '創造された:真実 'を得て、私はそれがすべての価値観を取っていると考えているからです。 – Nilesh
'_ttl'が有効になっていないと、' ttl'パラメータは暗黙的に無視されます。そのため、あなたは何のエラーも受け取りません。あなたのマッピングを知ること、そしてTTLを有効にしたのかどうかはあなたの仕事の一部です。 – Val