Elasticsearch专题精讲—— REST APIs —— Document APIs —— Delete by query API

左扬(你们的胃叫胃,孤的叫胃PLUS) / 2023-06-02 / 原文

REST APIs —— Document APIs —— Delete by query API

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete-by-query.html

Deletes documents that match the specified query.

删除与指定查询匹配的文档。

        curl -X POST "localhost:9200/my-index-000001/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'
        {
          "query": {
            "match": {
              "user.id": "elkbee"
            }
          }
        }'
    

1、Request(请求)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete-by-query.html#docs-delete-by-query-api-request

        POST //_delete_by_query

        POST//_delete_by_query
    

2、Prerequisites(先决条件)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete-by-query.html#docs-delete-by-query-api-prereqs

If the Elasticsearch security features are enabled, you must have the following index privileges for the target data stream, index, or alias:

如果启用了 Elasticsearch 安全特性,您必须对目标数据流、索引或别名拥有以下索引特权:

    • read
    • write
    • 删除或写入

3、Description(描述)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete-by-query.html#docs-delete-by-query-api-desc

You can specify the query criteria in the request URI or the request body using the same syntax as the Search API.

您可以使用与 SearchAPI 相同的语法在请求 URI 或请求体中指定查询条件。

When you submit a delete by query request, Elasticsearch gets a snapshot of the data stream or index when it begins processing the request and deletes matching documents using internal versioning. If a document changes between the time that the snapshot is taken and the delete operation is processed, it results in a version conflict and the delete operation fails.

当通过查询请求提交删除时,Elasticsearch 在开始处理请求时获取数据流或索引的快照,并使用内部版本控制删除匹配的文档。如果文档在获取快照和处理删除操作之间发生更改,则会导致版本冲突,并且删除操作失败。

Documents with a version equal to 0 cannot be deleted using delete by query because internal versioning does not support 0 as a valid version number.

版本等于0的文档不能通过查询删除,因为内部版本控制不支持0作为有效的版本号。

While processing a delete by query request, Elasticsearch performs multiple search requests sequentially to find all of the matching documents to delete. A bulk delete request is performed for each batch of matching documents. If a search or bulk request is rejected, the requests are retried up to 10 times, with exponential back off. If the maximum retry limit is reached, processing halts and all failed requests are returned in the response. Any delete requests that completed successfully still stick, they are not rolled back.

在处理按查询请求删除时,Elasticsearch 会顺序执行多个搜索请求,以找到要删除的所有匹配文档。对每批匹配的文档执行批量删除请求。如果一个搜索或批量请求被拒绝,请求将被重试10次,并以指数形式后退。如果达到最大重试限制,则在响应中返回处理暂停和所有失败的请求。任何成功完成的删除请求仍然存在,它们不会回滚。

You can opt to count version conflicts instead of halting and returning by setting conflicts to proceed. Note that if you opt to count version conflicts the operation could attempt to delete more documents from the source than max_docs until it has successfully deleted max_docs documents, or it has gone through every document in the source query.

您可以选择计算版本冲突,而不是通过设置冲突来停止和返回。请注意,如果选择计算版本冲突,则操作可能会尝试从源中删除比 max_docs 更多的文档,直到成功删除 max_docs 文档,或者遍历源查询中的每个文档。

4、Refreshing shards(刷新分片)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete-by-query.html#_refreshing_shards

Specifying the refresh parameter refreshes all shards involved in the delete by query once the request completes. This is different than the delete API’s refresh parameter, which causes just the shard that received the delete request to be refreshed. Unlike the delete API, it does not support wait_for.

指定刷新参数refresh将在请求完成后刷新delete by query涉及的所有分片。这与删除API的refresh参数不同,后者仅导致接收删除请求的分片被刷新。与删除API不同,它不支持wait_for参数。

我理解意思是说: 简而言之,refresh 参数会在请求完成后强制刷新所有涉及到删除操作的分片,让更改立即反映在搜索结果中。与之相比,删除 API 的 refresh 参数只会刷新接收到该请求的分片。需要注意的是,delete by query 不支持 wait_for 参数,因此执行删除操作时需要等待刷新完成并不保证删除结果立即在搜索结果中体现。