2016-07-26 38 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,所以考虑到增加支持它的工作,您应该只在需要时启用它。

相关问题