ELASTICSEARCH - 自动包含日期,无需预定义日期字段

Mig*_*ios 1 elasticsearch

可以在接收 Elasticsearch 的文档中包含“日期和时间”字段,而无需事先定义。日期和时间对应于elasticsearch的json接收到的日期和时间

这是映射:

{
  "mappings": {
    "properties": {
      "entries":{"type": "nested"
      }
    } 
  }
}
Run Code Online (Sandbox Code Playgroud)

是否可以在映射字段中定义它,以便elasticsearch自动包含当前日期?

Val*_*Val 6

您可以做的是定义一个摄取管道,以便在对文档建立索引时自动添加日期字段。

首先,创建一个管道,如下所示(_ingest.timestamp是您可以访问的内置字段):

PUT _ingest/pipeline/add-current-time
{
  "description" : "automatically add the current time to the documents",
  "processors" : [
    {
      "set" : {
        "field": "@timestamp",
        "value": "{{{_ingest.timestamp}}}"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

然后,当您索引新文档时,需要引用管道,如下所示:

PUT test-index/_doc/1?pipeline=add-current-time
{
   "my_field": "test"
}
Run Code Online (Sandbox Code Playgroud)

建立索引后,文档将如下所示:

GET test-index/_doc/1
=>

{
   "@timestamp": "2020-08-12T15:48:00.000Z",
   "my_field": "test"
}
Run Code Online (Sandbox Code Playgroud)

更新:

由于您使用的是索引模板,因此变得更加容易,因为您可以定义要为每个索引文档运行的默认管道。

在索引模板中,您需要将其添加到索引设置中:

{
  "order": 1,
  "index_patterns": [
    "attom"
  ],
  "aliases": {},
  "settings": {
    "index": {
      "number_of_shards": "5",
      "number_of_replicas": "1",
      "default_pipeline": "add-current-time"     <--- add this
    }
  },
  ...
Run Code Online (Sandbox Code Playgroud)

然后您可以在不引用管道的情况下保留索引文档,这将是自动的。