You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [Update API](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-update) enables you to script document updates, allowing you to update, delete, or skip modifying a document.
12
+
13
+
The following examples show common use cases, such as incrementing a counter and adding or removing elements from a list, as well as how to:
14
+
15
+
-[Update part of a document](#update-part-document)
16
+
-[Detect noop updates](#detect-noop-updates)
17
+
-[Insert or update documents with upsert](#upsert)
18
+
-[Simplify upsert with doc_as_upsert](#doc-as-upsert)
19
+
20
+
First, let's index a simple doc:
21
+
22
+
```console
23
+
PUT test/_doc/1
24
+
{
25
+
"counter" : 1,
26
+
"tags" : ["red"]
27
+
}
28
+
```
29
+
% TESTSETUP
30
+
31
+
To increment the counter, you can submit an update request with the
32
+
following script:
33
+
34
+
```console
35
+
POST test/_update/1
36
+
{
37
+
"script" : {
38
+
"source": "ctx._source.counter += params.count",
39
+
"lang": "painless",
40
+
"params" : {
41
+
"count" : 4
42
+
}
43
+
}
44
+
}
45
+
```
46
+
47
+
Similarly, you could use and update script to add a tag to the list of tags
48
+
(this is just a list, so the tag is added even it exists):
49
+
50
+
```console
51
+
POST test/_update/1
52
+
{
53
+
"script": {
54
+
"source": "ctx._source.tags.add(params.tag)",
55
+
"lang": "painless",
56
+
"params": {
57
+
"tag": "blue"
58
+
}
59
+
}
60
+
}
61
+
```
62
+
63
+
You could also remove a tag from the list of tags. The Painless
64
+
function to `remove` a tag takes the array index of the element
65
+
you want to remove. To avoid a possible runtime error, you first need to
66
+
make sure the tag exists. If the list contains duplicates of the tag, this
## Update part of a document [update-part-document]
139
+
140
+
The following partial update adds a new field to the
141
+
existing document:
142
+
143
+
```console
144
+
POST test/_update/1
145
+
{
146
+
"doc": {
147
+
"name": "new_name"
148
+
}
149
+
}
150
+
```
151
+
152
+
If both `doc` and `script` are specified, then `doc` is ignored. If you
153
+
specify a scripted update, include the fields you want to update in the script.
154
+
155
+
156
+
## Detect noop updates [detect-noop-updates]
157
+
158
+
By default updates that don't change anything detect that they don't change
159
+
anything and return `"result": "noop"`:
160
+
161
+
```console
162
+
POST test/_update/1
163
+
{
164
+
"doc": {
165
+
"name": "new_name"
166
+
}
167
+
}
168
+
```
169
+
% TEST[continued]
170
+
171
+
If the value of `name` is already `new_name`, the update
172
+
request is ignored and the `result` element in the response returns `noop`:
173
+
174
+
```console
175
+
176
+
{
177
+
"_shards": {
178
+
"total": 0,
179
+
"successful": 0,
180
+
"failed": 0
181
+
},
182
+
"_index": "test",
183
+
"_id": "1",
184
+
"_version": 2,
185
+
"_primary_term": 1,
186
+
"_seq_no": 1,
187
+
"result": "noop"
188
+
}
189
+
```
190
+
191
+
You can disable this behavior by setting `"detect_noop": false`:
192
+
193
+
```console
194
+
POST test/_update/1
195
+
{
196
+
"doc": {
197
+
"name": "new_name"
198
+
},
199
+
"detect_noop": false
200
+
}
201
+
```
202
+
203
+
## Insert or update documents with upsert [upsert]
204
+
205
+
An upsert operation lets you update an existing document or insert a new one if it doesn't exist, in a single request.
206
+
207
+
In this example, if the product with ID `1` exists, its price will be updated to `100`. If the product does not exist, a new document with ID `1` and a price of `50` will be inserted.
208
+
209
+
```console
210
+
POST /test/_update/1
211
+
{
212
+
"doc": {
213
+
"product_price": 100
214
+
},
215
+
"upsert": {
216
+
"product_price": 50
217
+
}
218
+
}
219
+
```
220
+
221
+
## Run a scripted upsert [scripted-upsert]
222
+
223
+
To run the script whether or not the document exists, set `scripted_upsert` to
224
+
`true`:
225
+
226
+
```console
227
+
POST test/_update/1
228
+
{
229
+
"scripted_upsert": true,
230
+
"script": {
231
+
"source": """
232
+
if ( ctx.op == 'create' ) {
233
+
ctx._source.counter = params.count
234
+
} else {
235
+
ctx._source.counter += params.count
236
+
}
237
+
""",
238
+
"params": {
239
+
"count": 4
240
+
}
241
+
},
242
+
"upsert": {}
243
+
}
244
+
```
245
+
246
+
## Simplify upsert with doc_as_upsert [doc-as-upsert]
247
+
248
+
Instead of sending a partial `doc` plus an `upsert` doc, you can set
249
+
`doc_as_upsert` to `true` to use the contents of `doc` as the `upsert`
250
+
value:
251
+
252
+
```console
253
+
POST test/_update/1
254
+
{
255
+
"doc": {
256
+
"name": "new_name"
257
+
},
258
+
"doc_as_upsert": true
259
+
}
260
+
```
261
+
262
+
::::{note}
263
+
Using [ingest pipelines](https://www.elastic.co/guide/en/elasticsearch/reference/8.18/ingest.html) with `doc_as_upsert` is not supported.
This page is about Search connectors that synchronize third-party data into {{es}}. If you’re looking for Kibana connectors to integrate with services like generative AI model providers, refer to [Kibana Connectors](docs-content://deploy-manage/manage-connectors.md).
17
+
This page is about Content connectors that synchronize third-party data into {{es}}. If you’re looking for Kibana connectors to integrate with services like generative AI model providers, refer to [Kibana Connectors](docs-content://deploy-manage/manage-connectors.md).
18
18
:::
19
19
20
20
A _connector_ is an Elastic integration that syncs data from an original data source to {{es}}. Use connectors to create searchable, read-only replicas of your data in {{es}}.
@@ -30,46 +30,60 @@ These connectors are written in Python and the source code is available in the [
30
30
As of Elastic 9.0, managed connectors on Elastic Cloud Hosted are no longer available. All connectors must be [self-managed](/reference/search-connectors/self-managed-connectors.md).
31
31
::::
32
32
33
-
34
-
Connectors are available for the following third-party data sources:
0 commit comments