Elasticsearch CRUD Operations + Bulk API
Elasticsearch can be operated by calling the REST API from the following tools.
- Kibana Dev Tools
- cURL
- Others
CRUD Operations + Bulk API
The Elasticsearch REST API supports the following CRUD operations for documents.
| REST API | CRUD operation | Description |
|---|---|---|
| Index API | Create | Add a document to an index |
| Get API | Read | Retrieve a document |
| Update API | Update | Modify a document |
| Delete API | Delete | Delete a document |
These are called Single document APIs, and one document is processed with a single API call.
To process multiple documents with one API call, use the Bulk API.
Index API (Create)
Use the Index API to add a document to an Elasticsearch index.
The Index API can be used as follows.
Using the Index API (curl)
curl "localhost:9200/2021-12-tweet/_doc/1?pretty" -XPUT -H "Content-Type: application/json" -d '
{
"date":"2021/12/13 09:00 KST",
"Tweet":"트위터를 시작했습니다.",
"User ID":"devkuma"
}'
Using the Index API (Kibana)
PUT /2021-12-tweet/_doc/1
{
"date":"2021/12/13 09:00 KST",
"Tweet":"트위터를 시작했습니다.",
"User ID":"devkuma"
}
When using Kibana Dev Tools, paste the command above and click the triangle button in the upper-right corner.
Adding a document to a data stream
A data stream is a set of multiple indexes grouped as one.
For more information about data streams, see the official documentation below.
Data streams | Elasticsearch Guide [7.16] | Elastic
To add a document to a data stream, use the REST API as follows.
Using the Index API (Kibana)
PUT /tweet/_create/1
{
"date" : "2021/12/13 09:00 KST",
"Tweet" : "트위터를 시작했습니다.",
"User ID" : "devkuma"
}
Get API (Read)
Use the Get API to retrieve the document you created.
Using the Get API (curl)
curl "localhost:9200/2021-12-tweet/_doc/1?pretty"
Using the Get API (Kibana)
GET /2021-12-tweet/_doc/1
Response from the command above
{
"_index" : "2021-12-tweet",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"date" : "2021/12/13 09:00 KST",
"Tweet" : "트위터를 시작했습니다.",
"User ID" : "devkuma"
}
}
Update API (Update)
Use the Update API to modify a document.
Using the Update API (curl)
curl "localhost:9200/2021-12-tweet/_update/1?pretty" -XPOST -H "Content-Type: application/json" -d '
{
"doc" : {
"date" : "2021/12/13 09:00 KST",
"Tweet" : "문서를 업데이트했습니다.",
"User ID" : "devkuma"
}
}'
Using the Update API (Kibana)
POST /2021-12-tweet/_update/1
{
"doc" : {
"date" : "2021/12/13 09:00 KST",
"Tweet" : "문서를 업데이트했습니다.",
"User ID" : "devkuma"
}
}
Check the update result with the Get API.
Checking the update result (curl)
curl "localhost:9200/2021-12-tweet/_doc/1?pretty"
Checking the update result (Kibana)
GET /2021-12-tweet/_doc/1
Response from the command above
{
"_index" : "2021-12-tweet",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"date" : "2021/12/13 09:00 KST",
"Tweet" : "문서를 업데이트했습니다.",
"User ID" : "devkuma"
}
}
Delete API (Delete)
Use the Delete API to delete a document.
Using the Delete API (curl)
curl "localhost:9200/2021-12-tweet/_doc/1?pretty" -XDELETE
Using the Delete API (Kibana)
DELETE /2021-12-tweet/_doc/1
Confirm that the document was deleted with the Get API.
Checking the delete result (curl)
curl "localhost:9200/2021-12-tweet/_doc/1?pretty"
Checking the delete result (Kibana)
GET /2021-12-tweet/_doc/1
Response from the command above
{
"_index" : "2021-12-tweet",
"_type" : "_doc",
"_id" : "1",
"found" : false
}
Bulk API
So far, each REST API call has processed only one document.
With the Bulk API, you can process multiple documents in one batch through the REST API.
The Bulk API is used as follows.
Contents of bulk.json
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : { "_id" : "1", "_index" : "test" } }
{ "doc" : { "field2" : "value2" } }
Using the Bulk API (curl)
curl "localhost:9200/_bulk?pretty" -XPOST --data-binary @bulk.json -H "Content-Type: application/json"
Using the Bulk API (Kibana)
POST /_bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : { "_id" : "1", "_index" : "test" } }
{ "doc" : { "field2" : "value2" } }
The differences between Create, Index, and Update are as follows.
- Create: Creates the document if it does not exist in the index.
- Index: Replaces the document in the index.
- Update: Modifies part of the document in the index.