Elasticsearch CRUD Tutorial: A Complete Guide to Create, Read, Update, and Delete Data on devtools kibana

NHAILA Achraf
3 min readNov 1, 2024

--

To Install Elasticsearch & Kibana, Please Follow These Steps:

A Simple Guide to Installing Elasticsearch and Kibana with Docker Compose

Elasticsearch is a powerful, open-source search and analytics engine capable of handling large amounts of data.
In this guide, we’ll walk through performing the basic CRUD operations — Create, Read, Update, and Delete — in Elasticsearch. By following along, you’ll learn how to manage data, specifically for a use case where we store product information. We’ll use Elasticsearch’s REST API, which communicates via JSON, making it easy to integrate with various tools.

Table of Contents

  1. Creating an Index
  2. Adding Documents
  3. Reading Documents
  4. Updating Documents
  5. Deleting Documents

Requirements

To follow this tutorial, you’ll need:

  • Elasticsearch installed and running (you can use Docker or a local installation).
  • A tool to make HTTP requests (e.g., curl, Postman, or a similar tool).

Creating an Index in Elasticsearch

An index in Elasticsearch is like a database in a relational system; it organizes and stores data logically.

To create an index named products, use the PUT method:

PUT /products

If needed, you can customize your index by configuring settings such as the number of shards and replicas, as well as defining the data structure (mappings) for fields.

Example: Creating a Custom Index with Settings and Mappings

PUT /products
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"product_name": { "type": "text" },
"category": { "type": "keyword" },
"price": { "type": "float" },
"in_stock": { "type": "boolean" }
}
}
}

In this setup:

  • Shards split the data for efficient management.
  • Replicas provide data backups for reliability.
  • Mappings define fields (product_name, category, price, in_stock) and their data types.

Step 2: Adding Documents (Create)

In Elasticsearch, a document is a record of data (similar to a row in a database table) and is stored within an index. Each document can have various fields and values.

To add a document to the products index, use the POST method with _doc:

POST /products/_doc
{
"product_name": "Wireless Headphones",
"category": "Electronics",
"price": 99.99,
"in_stock": true
}

Adding a Document with a Custom ID

Using the PUT method, you can assign a custom ID to the document:

PUT /products/_doc/1
{
"product_name": "Smartphone",
"category": "Electronics",
"price": 499.99,
"in_stock": false
}

Adding more documents to the index:

PUT /products/_doc/2
{
"product_name": "Laptop",
"category": "Computers",
"price": 899.99,
"in_stock": true
}

PUT /products/_doc/3
{
"product_name": "Coffee Maker",
"category": "Home Appliances",
"price": 59.99,
"in_stock": true
}

Step 3: Reading Documents (Read)

To retrieve a specific document by its ID, use the GET method with the document’s ID:

GET /products/_doc/1

This command returns the document’s data along with metadata, such as the index name, document ID, and version. For the document’s data without metadata, use the _source endpoint:

GET /products/_doc/1/_source

Step 4: Updating Documents (Update)

Elasticsearch allows both partial and full updates to documents. For a partial update, use the POST method with _update to modify only specific fields.

Example: Partially Update a Document

Change the stock status for a product:

POST /products/_update/1
{
"doc": {
"in_stock": true
}
}

This modifies only the in_stock field for the document with ID 1.

Full Replacement of a Document

To fully replace an existing document, use the PUT method with a new document structure:

PUT /products/_doc/1
{
"product_name": "Smartwatch",
"category": "Electronics",
"price": 199.99,
"in_stock": true
}

Step 5: Deleting Documents (Delete)

To remove a specific document, use the DELETE method with the document’s ID:

DELETE /products/_doc/1

This deletes the document with ID 1 from the products index.

Bulk Deletion Using a Query

To delete multiple documents that match a specific query, use _delete_by_query:

POST /products/_delete_by_query
{
"query": {
"match": {
"category": "Home Appliances"
}
}
}

This command deletes all documents where the category field is "Home Appliances".

Summary

In this tutorial, you learned the essential CRUD operations in Elasticsearch:

  • Create: Add new records to an index.
  • Read: Retrieve data.
  • Update: Modify existing data.
  • Delete: Remove data.

Using Elasticsearch’s CRUD operations, you can manage data efficiently for applications such as product catalogs, search engines, and more. Try these steps to start managing your own Elasticsearch data with ease!

--

--

NHAILA Achraf
NHAILA Achraf

No responses yet