如何使elasticsearch为所有索引中的每个文档添加时间戳字段?

Gau*_*m M 34 mapping timestamp elasticsearch

Elasticsearch专家,

我一直无法找到一种简单的方法来告诉ElasticSearch为所有索引(以及所有文档类型)中添加的所有文档插入_timestamp字段.

我看到了一个特定类型的例子:http: //www.elasticsearch.org/guide/reference/mapping/timestamp-field/

并且还查看特定类型的所有索引的示例(使用_all):http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/

但是我无法找到任何关于添加它的文档,无论索引和类型如何,都会添加它.

Flu*_*mur 38

Elasticsearch过去常常支持自动为正在编制索引的文档添加时间戳,但在2.0.0中不推荐使用此功能

从最新的(5.x)文档:

_timestamp和_ttl字段已弃用,现已删除.作为_timestamp的替代,您应该使用应用程序端的当前时间戳填充常规日期字段.


小智 33

您可以在创建索引时提供它.

$curl -XPOST localhost:9200/test -d '{
"settings" : {
    "number_of_shards" : 1
},
"mappings" : {
    "_default_":{
        "_timestamp" : {
            "enabled" : true,
            "store" : true
        }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

然后,它将自动为您放入索引的所有内容创建_timestamp.然后在请求_timestamp字段时索引某些东西后,它将被返回.

  • 这已被弃用.请谨慎使用.最好的是创建自己的日期字段. (13认同)
  • 7.2 中返回错误:{“error”:{“root_cause”:[{“type”:“mapper_parsing_exception”,“reason”:“根映射定义具有不支持的参数:[_default_:{_timestamp={store=true,启用” =true}}]"}],"type":"mapper_parsing_exception","re​​ason":"无法解析映射 [_doc]: 根映射定义具有不受支持的参数: [_default_ : {_timestamp={store=true,enabled= true}}]","caused_by":{"type":"mapper_parsing_exception","re​​ason":"根映射定义具有不受支持的参数:[_default_ : {_timestamp={store=true,enabled=true}}]"} },“状态”:400} (6认同)

man*_*noj 17

添加另一种获取索引时间戳的方法。希望这可以帮助某人。

摄取管道可用于在索引文档时添加时间戳。这是一个示例示例:

PUT _ingest/pipeline/indexed_at
{
  "description": "Adds indexed_at timestamp to documents",
  "processors": [
    {
      "set": {
        "field": "_source.indexed_at",
        "value": "{{_ingest.timestamp}}"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

早些时候,弹性搜索使用命名管道,因为需要在用于编写/索引文档的弹性搜索端点中指定“管道”参数。(参考:链接)这有点麻烦,因为您需要在应用程序端更改端点。

随着 Elastic 搜索版本 >= 6.5,您现在可以使用index.default_pipeline设置为索引指定默认管道。(详情请参考链接

这是设置默认管道的方法:

PUT ms-test/_settings
{
  "index.default_pipeline": "indexed_at"
}
Run Code Online (Sandbox Code Playgroud)

我还没有尝试过,因为没有升级到 ES 6.5,但是上面的命令应该可以工作。


Joe*_*ook 8

您可以利用默认索引管道脚本处理器,从而模拟您从DjangoSQLauto_now_add了解的功能。DEFAULT GETDATE()

添加默认日期的过程yyyy-MM-dd HH:mm:ss如下:

1. 创建管道并指定允许其运行的索引:

PUT _ingest/pipeline/auto_now_add
{
  "description": "Assigns the current date if not yet present and if the index name is whitelisted",
  "processors": [
    {
      "script": {
        "source": """
          // skip if not whitelisted
          if (![ "myindex",
                 "logs-index",
                 "..."
              ].contains(ctx['_index'])) { return; }
          
          // don't overwrite if present
          if (ctx['created_at'] != null) { return; }
          
          ctx['created_at'] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        """
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

旁注:摄取处理器的 Painless 脚本上下文记录在此处

2. 更新所有索引default_pipeline中的设置:

PUT _all/_settings
{
  "index": {
    "default_pipeline": "auto_now_add"
  }
}
Run Code Online (Sandbox Code Playgroud)

旁注:您可以使用多目标语法限制目标索引:

PUT myindex,logs-2021-*/_settings?allow_no_indices=true
{
  "index": {
    "default_pipeline": "auto_now_add"
  }
}
Run Code Online (Sandbox Code Playgroud)

3. 将文档摄取到配置的索引之一:

PUT myindex/_doc/1
{
  "abc": "def"
}
Run Code Online (Sandbox Code Playgroud)

4. 验证日期字符串是否已添加:

GET myindex/_search
Run Code Online (Sandbox Code Playgroud)