

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在时间序列集合中使用时间戳字段

亚马逊中的每个指数 OpenSearch 无服务器 NextGen 时间序列集合有指定的 *时间戳字段* — 单曲 `date` 字段，表示索引中每个文档的事件时间。 OpenSearch Serverless 使用此字段来组织和管理时间序列数据。您收录到的每份文档 NextGen 时间序列集合必须包含指定的时间戳字段。如果文档缺少时间戳字段或提供的值无效， OpenSearch Serverless 拒绝该文档。有关此验证的更多信息，请参阅 [摄取文档](#serverless-timestamp-field-ingest)。

**注意**  
时间戳字段仅适用于 *NextGen 时间序列* 集合。它不适用于经典时间序列集合，对搜索或矢量搜索集合没有影响。

**Topics**
+ [指定时间戳字段](#serverless-timestamp-field-setting)
+ [摄取文档](#serverless-timestamp-field-ingest)
+ [更新文档](#serverless-timestamp-field-update)

## 指定时间戳字段
<a name="serverless-timestamp-field-setting"></a>

您可以使用为索引指定时间戳字段 `index.timestamp_field` 索引设置。这个设置是 **可选的**。 OpenSearch Serverless 按以下优先顺序解析索引的时间戳字段：

1. 这个 `index.timestamp_field` 如果在创建索引时指定了索引的设置，则在索引上进行设置。

1. 这个 `index.timestamp_field` 由匹配的索引模板定义的设置（如果索引未直接指定索引模板）。

1. 默认字段 `@timestamp`，如果索引和匹配的模板都没有指定时间戳字段。

以下 OpenSearch REST API 请求会创建一个索引来指定名为的字段 `event-time` 作为时间戳字段。替换 {{my-logs}} 使用你的索引名称和 {{event-time}} 使用您要用作时间戳字段的字段的名称。

```json
PUT /{{my-logs}}
{
  "settings": {
    "index": {
      "timestamp_field": "{{event-time}}"
    }
  }
}
```

如果索引和匹配的索引模板均未指定设置，则索引使用 `@timestamp`，并且每份文件都必须包含 `@timestamp` 领域。

如果您将文档收录到尚不存在的索引中， OpenSearch 无服务器会自动创建索引。自动创建的索引无法指定 `index.timestamp_field` 直接设置，所以 OpenSearch Serverless 从匹配的索引模板或默认的索引模板解析其时间戳字段 `@timestamp` 如果没有模板匹配，则为字段。要为自动创建的索引指定特定的时间戳字段，请定义一个索引模板来设置 `index.timestamp_field`。

**注意**  
指定的时间戳字段必须映射为顶级字段 `date` 领域。它不能是对象或嵌套字段。 OpenSearch Serverless 将时间戳字段按降序设置为索引排序字段。

## 摄取文档
<a name="serverless-timestamp-field-ingest"></a>

当您将文档收录到时间序列集合中时， OpenSearch Serverless 会验证指定的时间戳字段：
+ 如果文档包含时间戳字段且其值是有效的 `date`， OpenSearch 无服务器为文档编制索引。
+ 如果文档不包含指定的时间戳字段， OpenSearch 无服务器拒绝该文档。
+ 如果文档包含该字段但其值无效 `date`， OpenSearch 无服务器拒绝该文档。

OpenSearch Serverless 仅拒绝验证失败的单个文档，而不是整个批量请求。回复会确定每份被拒绝的文件及其原因。

## 更新文档
<a name="serverless-timestamp-field-update"></a>

现有文档的时间戳是 **不可改变的**。您可以更新文档的其他字段，但不能更改其时间戳。
+ 不包含时间戳字段的更新成功。 OpenSearch Serverless 保留文档的原始时间戳。
+ 包含与文档原始时间戳值相同的时间戳字段的更新成功。
+ 将时间戳更改为其他值或提供的更新无效 `date`，被拒绝，文档保持不变。

要更正文档的时间戳，请删除该文档，然后使用更正后的值再次对其进行索引。