Skip to content

Commit 083284a

Browse files
authored
Add examples for delete by query API (#3574)
1 parent c15f4cf commit 083284a

14 files changed

+404
-101
lines changed

output/openapi/elasticsearch-openapi.json

Lines changed: 32 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output/openapi/elasticsearch-serverless-openapi.json

Lines changed: 31 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output/schema/schema.json

Lines changed: 54 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/_doc_ids/table.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ dissect-processor,https://www.elastic.co/guide/en/elasticsearch/reference/{branc
142142
distance-units,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/api-conventions.html#distance-units
143143
docs-bulk,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-bulk.html
144144
docs-delete-by-query,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-delete-by-query.html
145+
docs-delete-by-query-rethrottle,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-delete-by-query.html#docs-delete-by-query-rethrottle
145146
docs-delete,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-delete.html
146147
docs-get,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-get.html
147148
docs-index,https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/docs-index_.html

specification/_global/delete_by_query/DeleteByQueryRequest.ts

Lines changed: 114 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,91 @@ import { Duration } from '@_types/Time'
3535

3636
/**
3737
* Delete documents.
38+
*
3839
* Deletes documents that match the specified query.
40+
*
41+
* If the Elasticsearch security features are enabled, you must have the following index privileges for the target data stream, index, or alias:
42+
*
43+
* * `read`
44+
* * `delete` or `write`
45+
*
46+
* You can specify the query criteria in the request URI or the request body using the same syntax as the search API.
47+
* 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.
48+
* 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.
49+
*
50+
* NOTE: 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.
51+
*
52+
* While processing a delete by query request, Elasticsearch performs multiple search requests sequentially to find all of the matching documents to delete.
53+
* A bulk delete request is performed for each batch of matching documents.
54+
* If a search or bulk request is rejected, the requests are retried up to 10 times, with exponential back off.
55+
* If the maximum retry limit is reached, processing halts and all failed requests are returned in the response.
56+
* Any delete requests that completed successfully still stick, they are not rolled back.
57+
*
58+
* You can opt to count version conflicts instead of halting and returning by setting `conflicts` to `proceed`.
59+
* 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.
60+
*
61+
* **Throttling delete requests**
62+
*
63+
* To control the rate at which delete by query issues batches of delete operations, you can set `requests_per_second` to any positive decimal number.
64+
* This pads each batch with a wait time to throttle the rate.
65+
* Set `requests_per_second` to `-1` to disable throttling.
66+
*
67+
* Throttling uses a wait time between batches so that the internal scroll requests can be given a timeout that takes the request padding into account.
68+
* The padding time is the difference between the batch size divided by the `requests_per_second` and the time spent writing.
69+
* By default the batch size is `1000`, so if `requests_per_second` is set to `500`:
70+
*
71+
* ```
72+
* target_time = 1000 / 500 per second = 2 seconds
73+
* wait_time = target_time - write_time = 2 seconds - .5 seconds = 1.5 seconds
74+
* ```
75+
*
76+
* Since the batch is issued as a single `_bulk` request, large batch sizes cause Elasticsearch to create many requests and wait before starting the next set.
77+
* This is "bursty" instead of "smooth".
78+
*
79+
* **Slicing**
80+
*
81+
* Delete by query supports sliced scroll to parallelize the delete process.
82+
* This can improve efficiency and provide a convenient way to break the request down into smaller parts.
83+
*
84+
* Setting `slices` to `auto` lets Elasticsearch choose the number of slices to use.
85+
* This setting will use one slice per shard, up to a certain limit.
86+
* If there are multiple source data streams or indices, it will choose the number of slices based on the index or backing index with the smallest number of shards.
87+
* Adding slices to the delete by query operation creates sub-requests which means it has some quirks:
88+
*
89+
* * You can see these requests in the tasks APIs. These sub-requests are "child" tasks of the task for the request with slices.
90+
* * Fetching the status of the task for the request with slices only contains the status of completed slices.
91+
* * These sub-requests are individually addressable for things like cancellation and rethrottling.
92+
* * Rethrottling the request with `slices` will rethrottle the unfinished sub-request proportionally.
93+
* * Canceling the request with `slices` will cancel each sub-request.
94+
* * Due to the nature of `slices` each sub-request won't get a perfectly even portion of the documents. All documents will be addressed, but some slices may be larger than others. Expect larger slices to have a more even distribution.
95+
* * Parameters like `requests_per_second` and `max_docs` on a request with `slices` are distributed proportionally to each sub-request. Combine that with the earlier point about distribution being uneven and you should conclude that using `max_docs` with `slices` might not result in exactly `max_docs` documents being deleted.
96+
* * Each sub-request gets a slightly different snapshot of the source data stream or index though these are all taken at approximately the same time.
97+
*
98+
* If you're slicing manually or otherwise tuning automatic slicing, keep in mind that:
99+
*
100+
* * Query performance is most efficient when the number of slices is equal to the number of shards in the index or backing index. If that number is large (for example, 500), choose a lower number as too many `slices` hurts performance. Setting `slices` higher than the number of shards generally does not improve efficiency and adds overhead.
101+
* * Delete performance scales linearly across available resources with the number of slices.
102+
*
103+
* Whether query or delete performance dominates the runtime depends on the documents being reindexed and cluster resources.
104+
*
105+
* **Cancel a delete by query operation**
106+
*
107+
* Any delete by query can be canceled using the task cancel API. For example:
108+
*
109+
* ```
110+
* POST _tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel
111+
* ```
112+
*
113+
* The task ID can be found by using the get tasks API.
114+
*
115+
* Cancellation should happen quickly but might take a few seconds.
116+
* The get task status API will continue to list the delete by query task until this task checks that it has been cancelled and terminates itself.
39117
* @rest_spec_name delete_by_query
40118
* @availability stack since=5.0.0 stability=stable
41119
* @availability serverless stability=stable visibility=public
120+
* @index_privileges read,delete
42121
* @doc_tag document
122+
* @doc_id docs-delete-by-query
43123
*/
44124
export interface Request extends RequestBase {
45125
urls: [
@@ -50,8 +130,8 @@ export interface Request extends RequestBase {
50130
]
51131
path_parts: {
52132
/**
53-
* Comma-separated list of data streams, indices, and aliases to search.
54-
* Supports wildcards (`*`).
133+
* A comma-separated list of data streams, indices, and aliases to search.
134+
* It supports wildcards (`*`).
55135
* To search all data streams or indices, omit this parameter or use `*` or `_all`.
56136
*/
57137
index: Indices
@@ -66,10 +146,12 @@ export interface Request extends RequestBase {
66146
allow_no_indices?: boolean
67147
/**
68148
* Analyzer to use for the query string.
149+
* This parameter can be used only when the `q` query string parameter is specified.
69150
*/
70151
analyzer?: string
71152
/**
72153
* If `true`, wildcard and prefix queries are analyzed.
154+
* This parameter can be used only when the `q` query string parameter is specified.
73155
* @server_default false
74156
*/
75157
analyze_wildcard?: boolean
@@ -80,17 +162,19 @@ export interface Request extends RequestBase {
80162
conflicts?: Conflicts
81163
/**
82164
* The default operator for query string query: `AND` or `OR`.
165+
* This parameter can be used only when the `q` query string parameter is specified.
83166
* @server_default OR
84167
*/
85168
default_operator?: Operator
86169
/**
87-
* Field to use as default where no field prefix is given in the query string.
170+
* The field to use as default where no field prefix is given in the query string.
171+
* This parameter can be used only when the `q` query string parameter is specified.
88172
*/
89173
df?: string
90174
/**
91-
* Type of index that wildcard patterns can match.
175+
* The type of index that wildcard patterns can match.
92176
* If the request can target data streams, this argument determines whether wildcard expressions match hidden data streams.
93-
* Supports comma-separated values, such as `open,hidden`. Valid values are: `all`, `open`, `closed`, `hidden`, `none`.
177+
* It supports comma-separated values, such as `open,hidden`.
94178
* @server_default open
95179
*/
96180
expand_wildcards?: ExpandWildcards
@@ -102,21 +186,25 @@ export interface Request extends RequestBase {
102186
ignore_unavailable?: boolean
103187
/**
104188
* If `true`, format-based query failures (such as providing text to a numeric field) in the query string will be ignored.
189+
* This parameter can be used only when the `q` query string parameter is specified.
105190
* @server_default false
106191
*/
107192
lenient?: boolean
108193
/**
109-
* Maximum number of documents to process.
194+
* The maximum number of documents to process.
110195
* Defaults to all documents.
196+
* When set to a value less then or equal to `scroll_size`, a scroll will not be used to retrieve the results for the operation.
111197
*/
112198
max_docs?: long
113199
/**
114-
* Specifies the node or shard the operation should be performed on.
115-
* Random by default.
200+
* The node or shard the operation should be performed on.
201+
* It is random by default.
116202
*/
117203
preference?: string
118204
/**
119205
* If `true`, Elasticsearch refreshes all shards involved in the delete by query after the request completes.
206+
* This is different than the delete API's `refresh` parameter, which causes just the shard that received the delete request to be refreshed.
207+
* Unlike the delete API, it does not support `wait_for`.
120208
* @server_default false
121209
*/
122210
refresh?: boolean
@@ -127,33 +215,35 @@ export interface Request extends RequestBase {
127215
request_cache?: boolean
128216
/**
129217
* The throttle for this request in sub-requests per second.
218+
* @server_default -1
130219
*/
131220
requests_per_second?: float
132221
/**
133-
* Custom value used to route operations to a specific shard.
222+
* A custom value used to route operations to a specific shard.
134223
*/
135224
routing?: Routing
136225
/**
137-
* Query in the Lucene query string syntax.
226+
* A query in the Lucene query string syntax.
138227
*/
139228
q?: string
140229
/**
141-
* Period to retain the search context for scrolling.
230+
* The period to retain the search context for scrolling.
231+
* @ext_doc_id scroll-search-results
142232
*/
143233
scroll?: Duration
144234
/**
145-
* Size of the scroll request that powers the operation.
235+
* The size of the scroll request that powers the operation.
146236
* @server_default 1000
147237
*/
148238
scroll_size?: long
149239
/**
150-
* Explicit timeout for each search request.
151-
* Defaults to no timeout.
240+
* The explicit timeout for each search request.
241+
* It defaults to no timeout.
152242
*/
153243
search_timeout?: Duration
154244
/**
155245
* The type of the search operation.
156-
* Available options: `query_then_fetch`, `dfs_query_then_fetch`.
246+
* Available options include `query_then_fetch` and `dfs_query_then_fetch`.
157247
*/
158248
search_type?: SearchType
159249
/**
@@ -162,25 +252,26 @@ export interface Request extends RequestBase {
162252
*/
163253
slices?: Slices
164254
/**
165-
* A comma-separated list of <field>:<direction> pairs.
255+
* A comma-separated list of `<field>:<direction>` pairs.
166256
*/
167257
sort?: string[]
168258
/**
169-
* Specific `tag` of the request for logging and statistical purposes.
259+
* The specific `tag` of the request for logging and statistical purposes.
170260
*/
171261
stats?: string[]
172262
/**
173-
* Maximum number of documents to collect for each shard.
263+
* The maximum number of documents to collect for each shard.
174264
* If a query reaches this limit, Elasticsearch terminates the query early.
175265
* Elasticsearch collects documents before sorting.
266+
*
176267
* Use with caution.
177268
* Elasticsearch applies this parameter to each shard handling the request.
178269
* When possible, let Elasticsearch perform early termination automatically.
179270
* Avoid specifying this parameter for requests that target data streams with backing indices across multiple data tiers.
180271
*/
181272
terminate_after?: long
182273
/**
183-
* Period each deletion request waits for active shards.
274+
* The period each deletion request waits for active shards.
184275
* @server_default 1m
185276
*/
186277
timeout?: Duration
@@ -190,12 +281,14 @@ export interface Request extends RequestBase {
190281
version?: boolean
191282
/**
192283
* The number of shard copies that must be active before proceeding with the operation.
193-
* Set to all or any positive integer up to the total number of shards in the index (`number_of_replicas+1`).
284+
* Set to `all` or any positive integer up to the total number of shards in the index (`number_of_replicas+1`).
285+
* The `timeout` value controls how long each write request waits for unavailable shards to become available.
194286
* @server_default 1
195287
*/
196288
wait_for_active_shards?: WaitForActiveShards
197289
/**
198290
* If `true`, the request blocks until the operation is complete.
291+
* If `false`, Elasticsearch performs some preflight checks, launches the request, and returns a task you can use to cancel or get the status of the task. Elasticsearch creates a record of this task as a document at `.tasks/task/${taskId}`. When you are done with a task, you should delete the task document so Elasticsearch can reclaim the space.
199292
* @server_default true
200293
*/
201294
wait_for_completion?: boolean
@@ -206,7 +299,7 @@ export interface Request extends RequestBase {
206299
*/
207300
max_docs?: long
208301
/**
209-
* Specifies the documents to delete using the Query DSL.
302+
* The documents to delete specified with Query DSL.
210303
*/
211304
query?: QueryContainer
212305
/**

specification/_global/delete_by_query/DeleteByQueryResponse.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,64 @@ import { Duration, DurationValue, UnitMillis } from '@_types/Time'
2525

2626
export class Response {
2727
body: {
28+
/**
29+
* The number of scroll responses pulled back by the delete by query.
30+
*/
2831
batches?: long
32+
/**
33+
* The number of documents that were successfully deleted.
34+
*/
2935
deleted?: long
36+
/**
37+
* An array of failures if there were any unrecoverable errors during the process.
38+
* If this array is not empty, the request ended abnormally because of those failures.
39+
* Delete by query is implemented using batches and any failures cause the entire process to end but all failures in the current batch are collected into the array.
40+
* You can use the `conflicts` option to prevent reindex from ending on version conflicts.
41+
*/
3042
failures?: BulkIndexByScrollFailure[]
43+
/**
44+
* This field is always equal to zero for delete by query.
45+
* It exists only so that delete by query, update by query, and reindex APIs return responses with the same structure.
46+
*/
3147
noops?: long
48+
/**
49+
* The number of requests per second effectively run during the delete by query.
50+
*/
3251
requests_per_second?: float
52+
/**
53+
* The number of retries attempted by delete by query.
54+
* `bulk` is the number of bulk actions retried.
55+
* `search` is the number of search actions retried.
56+
*/
3357
retries?: Retries
3458
slice_id?: integer
3559
task?: TaskId
3660
throttled?: Duration
61+
/**
62+
* The number of milliseconds the request slept to conform to `requests_per_second`.
63+
*/
3764
throttled_millis?: DurationValue<UnitMillis>
3865
throttled_until?: Duration
66+
/**
67+
* This field should always be equal to zero in a `_delete_by_query` response.
68+
* It has meaning only when using the task API, where it indicates the next time (in milliseconds since epoch) a throttled request will be run again in order to conform to `requests_per_second`.
69+
*/
3970
throttled_until_millis?: DurationValue<UnitMillis>
71+
/**
72+
* If `true`, some requests run during the delete by query operation timed out.
73+
*/
4074
timed_out?: boolean
75+
/**
76+
* The number of milliseconds from start to end of the whole operation.
77+
*/
4178
took?: DurationValue<UnitMillis>
79+
/**
80+
* The number of documents that were successfully processed.
81+
*/
4282
total?: long
83+
/**
84+
* The number of version conflicts that the delete by query hit.
85+
*/
4386
version_conflicts?: long
4487
}
4588
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
summary: Delete all documents
2+
# method_request: POST /my-index-000001,my-index-000002/_delete_by_query
3+
description: Run `POST /my-index-000001,my-index-000002/_delete_by_query` to delete all documents from multiple data streams or indices.
4+
# type: request
5+
value: |-
6+
{
7+
"query": {
8+
"match_all": {}
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
summary: Delete a single document
2+
# method_request: POST my-index-000001/_delete_by_query
3+
description: Run `POST my-index-000001/_delete_by_query` to delete a document by using a unique attribute.
4+
# type: request
5+
value: |-
6+
{
7+
"query": {
8+
"term": {
9+
"user.id": "kimchy"
10+
}
11+
},
12+
"max_docs": 1
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
summary: Slice manually
2+
# method_request: POST my-index-000001/_delete_by_query
3+
description: >
4+
Run `POST my-index-000001/_delete_by_query` to slice a delete by query manually. Provide a slice ID and total number of slices.
5+
# type: request
6+
value: |-
7+
{
8+
"slice": {
9+
"id": 0,
10+
"max": 2
11+
},
12+
"query": {
13+
"range": {
14+
"http.response.bytes": {
15+
"lt": 2000000
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)