2016-07-26 37 views
0

我在中創建了indexdoc。爲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}' 

當我試圖讓使用新映射的ttlfields

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以強制在文檔中生效?

+0

是的,默認情況下'_ttl'沒有啓用,所以你需要啓用它才能使TTL工作,但它不會影響已經創建的文檔。 – Val

+0

@Val如果它無法設置'ttl',那麼'PUT'調用應該會引發錯誤?因爲,我得到'created:true',那麼我認爲它取得了我所有的價值觀,有沒有什麼辦法來定義這個問題,如果有些東西不起作用,請告訴我。 – Nilesh

+0

如果'_ttl'未啓用,'ttl'參數會被忽略,因此您將不會收到任何錯誤。知道你的映射以及你是否啓用了TTL是你工作的一部分。 – Val

回答

1

是的,默認情況下,_ttl未啓用,因此您需要啓用它才能使TTL正常工作,但不會影響已創建的文檔。

請注意,如果您的映射中未啓用_ttl,則會默認忽略ttl參數,因此您不會收到任何錯誤。知道你的映射以及你是否啓用了TTL是你工作的一部分。

您可以在任何時候啓用_ttl,所以考慮到增加支持它的工作,您應該只在需要時啓用它。

相關問題