使用数据流
设置数据流之后,你可以执行以下操作:
添加文档到数据流
POST my-data-stream/_doc
{
"@timestamp": "2099-03-08T11:06:07.000Z",
"user": {
"id": "8a4f500d"
},
"message": "Login successful"
}
你不能使用索引 API 的 PUT /<target>/_doc/<_id> 请求格式向数据流添加新文档。要指定文档 ID,请改用 PUT /<target>/_create/<_id> 格式。只支持 op_type 为 create 的操作。
要使用单个请求添加多个文档,请使用批量 API。只支持 create 操作。
PUT my-data-stream/_bulk?refresh
{ "create":{ } }
{ "@timestamp": "2099-03-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
{ "create":{ } }
{ "@timestamp": "2099-03-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
{ "create":{ } }
{ "@timestamp": "2099-03-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
搜索数据流
以下搜索 API 支持数据流:
获取数据流的统计信息
使用数据流统计 API 获取一个或多个数据流的统计信息:
GET /_data_stream/my-data-stream/_stats?human=true
手动翻转数据流
要立即触发翻转:
POST my-data-stream/_rollover/或者将翻转推迟到下一个索引事件发生时:
POST my-data-stream/_rollover?lazy对于更新不频繁的数据流,使用第二种方式可以避免产生空的支持索引。
打开已关闭的支持索引
你无法搜索已关闭的支持索引,即使通过搜索其数据流也不行。你也无法更新或删除已关闭索引中的文档。
要重新打开已关闭的支持索引,请直接向该索引提交打开索引 API 请求:
POST .ds-my-data-stream-2099.03.07-000001/_open/
要重新打开数据流的所有已关闭支持索引,请向该流提交打开索引 API 请求:
POST my-data-stream/_open/
重索引到数据流
使用重索引 API 将文档从现有索引、别名或数据流复制到数据流。因为数据流是仅追加的,重索引到数据流必须使用 op_type 为 create 的操作。重索引不能更新数据流中的现有文档。
POST /_reindex
{
"source": {
"index": "archive"
},
"dest": {
"index": "my-data-stream",
"op_type": "create"
}
}
通过查询更新数据流中的文档
使用 update by query API 更新数据流中匹配给定查询的文档:
POST my-data-stream/_update_by_query
{
"query": {
"match": {
"user.id": "l7gk7f82"
}
},
"script": {
"source": "ctx._source.user.id = params.new_id",
"params": {
"new_id": "XgdX0NoX"
}
}
}
通过查询删除数据流中的文档
使用 delete by query API 删除数据流中匹配给定查询的文档:
POST my-data-stream/_delete_by_query
{
"query": {
"match": {
"user.id": "vlb44hny"
}
}
}
更新或删除备份索引中的文档
如果需要,你可以通过向包含该文档的支持索引发送请求来更新或删除数据流中的文档。你需要:
要获取这些信息,请使用搜索请求:
GET my-data-stream/_search
{
"seq_no_primary_term": true,
"query": {
"match": {
"user.id": "yWIumJd7"
}
}
}
响应:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": ".ds-my-data-stream-2099.03.08-000003",
"_id": "bfspvnIBr7VVZlfp2lqX",
"_seq_no": 0,
"_primary_term": 1,
"_score": 0.2876821,
"_source": {
"@timestamp": "2099-03-08T11:06:07.000Z",
"user": {
"id": "yWIumJd7"
},
"message": "Login successful"
}
}
]
}
}
- 包含匹配文档的支持索引。
- 该文档的文档 ID。
- 该文档的当前序列号。
- 该文档的主分片期数。
要更新该文档,请使用带有有效 if_seq_no 和 if_primary_term 参数的索引 API 请求:
PUT .ds-my-data-stream-2099-03-08-000003/_doc/bfspvnIBr7VVZlfp2lqX?if_seq_no=0&if_primary_term=1
{
"@timestamp": "2099-03-08T11:06:07.000Z",
"user": {
"id": "8a4f500d"
},
"message": "Login successful"
}
要删除该文档,请使用删除 API:
DELETE .ds-my-data-stream-2099.03.08-000003/_doc/bfspvnIBr7VVZlfp2lqX
要在单个请求中删除或更新多个文档,请使用批量 API 的 delete、index 和 update 操作。对于 index 操作,请包含有效的 if_seq_no 和 if_primary_term 参数。
PUT /_bulk?refresh
{ "index": { "_index": ".ds-my-data-stream-2099.03.08-000003", "_id": "bfspvnIBr7VVZlfp2lqX", "if_seq_no": 0, "if_primary_term": 1 } }
{ "@timestamp": "2099-03-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }