Updates Flashcards

1
Q

Simple Update

A

POST /my_index/_update/1
{
“doc”: {
“title”: “Updated Elasticsearch Guide”,
“views”: 150
}
}

This example updates the fields title and views of the document with ID 1 in the my_index index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Script based update

A

POST /my_index/_update/1
{
“script”: {
“source”: “ctx._source.views += params.increment”,
“params”: {
“increment”: 10
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Upsert

A

An “upsert” is used to update a document if it exists, or create a new one if it doesn’t.

POST /my_index/_update/1
{
“doc”: {
“title”: “Elasticsearch Basics”,
“views”: 100
},
“upsert”: {
“title”: “Elasticsearch Basics”,
“views”: 100
}
}

If the document with ID 1 exists, it will be updated with the doc values. If it doesn’t exist, the content in upsert will be used to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Updating Nested or Array Fields

A

You can update nested fields or fields that contain arrays in a similar way.

POST /my_index/_update/1
{
“script”: {
“source”: “ctx._source.tags.add(params.tag)”,
“params”: {
“tag”: “search”
}
}
}

In this example, the tags array will have the new element “search” added to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Updating using the bulk api

A

If you need to update multiple documents at once, you can use the Bulk API, which allows sending multiple update requests in a single batch.

POST /_bulk
{ “update”: { “_index”: “my_index”, “_id”: “1” } }
{ “doc”: { “views”: 200 } }
{ “update”: { “_index”: “my_index”, “_id”: “2” } }
{ “doc”: { “views”: 300 } }

This request updates the views field for documents with IDs 1 and 2 in the my_index index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Replacing the document

A

If you want to replace the entire document, you can use the index API with the PUT method.

PUT /my_index/_doc/1
{
“title”: “Complete Elasticsearch Guide”,
“views”: 250,
“tags”: [“search”, “elasticsearch”]
}

In this case, the entire document with ID 1 will be replaced with the new content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Update By Query

A

The update by query API in Elasticsearch allows you to update multiple documents that match a specified query, without retrieving the documents first. This operation is useful for bulk updates when you need to modify fields across a set of documents based on specific criteria.

POST /my_index/_update_by_query
{
“script”: {
“source”: “ctx._source.views += params.increment”,
“params”: {
“increment”: 5
}
},
“query”: {
“term”: {
“status”: “published”
}
}
}

The query filters documents where the status is “published”.
The script increments the views field by 5 for all matching documents.

POST /my_index/_update_by_query?conflicts=proceed

conflicts: Determines the behavior if there are version conflicts during the update. Can be set to “proceed” to continue updating even if there are conflicts.

refresh: If set to true, forces an index refresh after the update, making changes visible immediately.

POST /my_index/_update_by_query?refresh=true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly