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

REST APIs —— Document APIs —— Delete API

Removes a JSON document from the specified index.

从指定的索引中移除 JSON 文档。

1、Request(请求)

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

        DELETE /< index>/_doc/<_id>

        DELETE /< index>/_doc/<_id>
    

2、Prerequisites(先决条件)

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

If the Elasticsearch security features are enabled, you must have the delete or write index privilege for the target index or index alias.

如果启用了 Elasticsearch 安全特性,则必须拥有目标索引或索引别名的删除或写入索引特权。

3、Description(描述)

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

You use DELETE to remove a document from an index. You must specify the index name and document ID.

使用 DELETE 从索引中删除文档。必须指定索引名称和文档 ID。

4、Optimistic concurrency control(乐观并发控制)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete.html#optimistic-concurrency-control-delete

Delete operations can be made conditional and only be performed if the last modification to the document was assigned the sequence number and primary term specified by the if_seq_no and if_primary_term parameters. If a mismatch is detected, the operation will result in a VersionConflictException and a status code of 409. See Optimistic concurrency control for more details.

删除操作可以被设定为条件执行,只有在文档的最后修改被分配的序列号和主要期限与`if_seq_no`和`if_primary_term`参数指定的值相等才会执行。如果发现不匹配,操作将导致`VersionConflictException`异常和状态码409。有关详细信息,请参见乐观并发控制。(https://www.elastic.co/guide/en/elasticsearch/reference/8.8/optimistic-concurrency-control.html)

5、Versioning(版本控制)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete.html#optimistic-concurrency-control-delete

Each document indexed is versioned. When deleting a document, the version can be specified to make sure the relevant document we are trying to delete is actually being deleted and it has not changed in the meantime. Every write operation executed on a document, deletes included, causes its version to be incremented. The version number of a deleted document remains available for a short time after deletion to allow for control of concurrent operations. The length of time for which a deleted document’s version remains available is determined by the index.gc_deletes index setting and defaults to 60 seconds.

索引的每个文档都有版本控制。删除文档时,可以指定版本,以确保我们试图删除的相关文档实际上已被删除,同时没有发生更改。对文档执行的每个写操作(包括删除操作)都会导致其版本增加。已删除文档的版本号在删除后短时间内仍然可用,以便控制并发操作。已删除文档的版本保持可用的时间长度由 index.gc _ delete 索引设置确定,默认为60秒。

6、Routing(路由)

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

If routing is used during indexing, the routing value also needs to be specified to delete a document.

如果在索引期间使用路由,则还需要指定路由值来删除文档。

If the _routing mapping is set to required and no routing value is specified, the delete API throws a RoutingMissingException and rejects the request.

如果 _routing 映射设置为必需且未指定路由值,则 delete API 将引发 RoutingMissingException 并拒绝请求。

        curl -X DELETE "localhost:9200/my-index-000001/_doc/1?routing=shard-1&pretty"

This request deletes the document with id 1, but it is routed based on the user. The document is not deleted if the correct routing is not specified.

这个请求删除 id 为1的文档,但是会根据用户进行路由。如果未指定正确的路由,则不删除文档。

7、Automatic index creation(自动创建索引)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete.html#delete-index-creation

If an external versioning variant is used, the delete operation automatically creates the specified index if it does not exist. For information about manually creating indices, see create index API.

如果使用了外部版本控制变量,则如果指定的索引不存在,则删除操作将自动创建该索引。有关手动创建索引的信息,请参见创建索引 API。

8、Distributed(分发的)

https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-delete.html#delete-index-creation

The delete operation gets hashed into a specific shard id. It then gets redirected into the primary shard within that id group, and replicated (if needed) to shard replicas within that id group.

Delete 操作被散列到特定的分片 ID 中。然后,它被重定向到该 id 组中的主分片,并被复制(如果需要)到该 id 组中的分片副本。

9、Timeout(超时)

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

The primary shard assigned to perform the delete operation might not be available when the delete operation is executed. Some reasons for this might be that the primary shard is currently recovering from a store or undergoing relocation. By default, the delete operation will wait on the primary shard to become available for up to 1 minute before failing and responding with an error. The timeout parameter can be used to explicitly specify how long it waits. Here is an example of setting it to 5 minutes:

执行删除操作时,分配用于执行删除操作的主分片可能不可用。出现这种情况的一些原因可能是主分片当前正从存储中恢复或正在进行重新定位。默认情况下,删除操作将在主分片上等待最多1分钟,然后才会失败并响应错误。Timeout 参数可用于显式指定它等待的时间。下面是一个设置为5分钟的例子:

posted @ 2023-06-02 16:34  左扬  阅读(16)  评论(0编辑  收藏  举报
levels of contents