我在elasticsearch - 如果未在映射中启用,elasticsearch不会在字段中返回ttl-LMLPHPelasticsearch中创建了indexdoc。为文档添加映射。

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是您工作的一部分。

您可以随时启用ojit_code,因此,要增加支持它的工作,仅应在需要时启用它。

关于elasticsearch - 如果未在映射中启用,elasticsearch不会在字段中返回ttl,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38597094/

10-16 07:22