如果未在映射中启用,elasticsearch 不会在字段中返回 ttl

标签 elasticsearch mapping ttl

我创建了 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在任何时候,鉴于支持它的工作增加,您应该只在需要时启用它。

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

相关文章:

ruby-on-rails - Rails-如何在searchkick中过滤字符串字段?

sql-server - 删除文档中的重复记录(Elasticsearch)

java - 使用 Java 将任何操作系统的全局输入事件映射到另一个输入事件

java - 使用 Jackson 将 JSON 反序列化为 ArrayList<POJO>

mongodb - MGO TTL 索引创建以选择性地删除文档

node.js - Mongoose - 如何根据另一个字段更改 TTL?

linux - 如何关闭 BeagleBone Black Wireless 上的 GPIO 端口

pdf - Elasticsearch-提取PDF内容并使用base64进行编码

node.js - Elasticsearch索引/缓存未清除/卡住

java - 使用流 Java 8 从 List<Object>,List<Object> 转换为 List<AnotherObject>