Menu

CRUD Operations in Elasticsearch

CRUD Operations in Elasticsearch (Create, Read, Update, and Delete)

In the previous lesson, you learned how Elasticsearch organizes data using indices, documents, fields, and data types.

Now it's time to start working with data.

Imagine you're building an online shopping application.

You need to:

  • Add new products.
  • View product details.
  • Update prices.
  • Delete discontinued products.

These four basic operations are called CRUD Operations.

CRUD stands for:

  • Create
  • Read
  • Update
  • Delete

Every application that stores data uses CRUD operations.

By the end of this lesson, you'll learn:

  • What CRUD operations are.
  • How Elasticsearch performs each operation.
  • How REST APIs are used.
  • Simple examples for beginners.
  • Best practices for working with documents.

What is a REST API?

Before learning CRUD operations, let's understand one important concept.

Applications communicate with Elasticsearch using REST APIs.

REST API is simply a way for one application to send requests to another over the internet or a local network.

Think of it like ordering food at a restaurant.

  • You tell the waiter what you want.
  • The waiter takes your request to the kitchen.
  • The kitchen prepares the food.
  • The waiter brings the food back.

Similarly:

Application

REST API Request

Elasticsearch

REST API Response

Understanding HTTP Methods

REST APIs use different HTTP methods depending on what you want to do.

Method

Purpose

POSTCreate new data
GETRead existing data
PUTCreate or replace a document
DELETEDelete data

Each method tells Elasticsearch what action to perform.

CRUD Overview

Create → Add new document

Read → View document

Update → Modify document

Delete → Remove document

These operations form the foundation of working with Elasticsearch.

CREATE Operation

Suppose an electronics store wants to add a new laptop.

The product information might look like this:

{

"name": "Dell Inspiron 15",

"brand": "Dell",

"price": 58999,

"category": "Laptop",

"rating": 4.7

}

When this document is sent to Elasticsearch, it is stored inside the appropriate index.

For example:

Products Index

├── Laptop

├── Mouse

├── Keyboard

├── Dell Inspiron 15   ← Newly Added

Now the product becomes searchable.

What Happens Internally?

When Elasticsearch receives a new document:

  1. Validates the request.
  2. Stores the document.
  3. Assigns or uses a document ID.
  4. Indexes the data.
  5. Makes it searchable.

This entire process usually takes only milliseconds.

READ Operation

Reading means retrieving data already stored in Elasticsearch.

Suppose a customer searches for:

Dell Inspiron 15

Elasticsearch searches its index and returns the matching document.

Result:

{

"name": "Dell Inspiron 15",

"brand": "Dell",

"price": 58999

}

The application then displays the information to the user.

Real-Life Example

Imagine searching your contacts for:

Rahul

Your phone quickly finds Rahul's contact.

This is similar to Elasticsearch performing a Read operation.

UPDATE Operation

Businesses frequently update information.

For example:

Yesterday's price:

₹58,999

Today's offer:

₹54,999

Instead of creating another document, Elasticsearch updates the existing one.

Updated document:

{

"name": "Dell Inspiron 15",

"price": 54999

}

The rest of the document remains unchanged unless explicitly updated.

Why Are Updates Important?

Updates are commonly used for:

  • Product prices
  • Customer addresses
  • Inventory levels
  • Ratings
  • Employee details
  • Order status

Without updates, applications would constantly create duplicate records.

DELETE Operation

Suppose a product is discontinued.

It should no longer appear in search results.

The document can simply be removed.

Before:

Products

Laptop

Mouse

Keyboard

Smart Watch

After deleting Smart Watch:

Products

Laptop

Mouse

Keyboard

The deleted product is no longer searchable.

Real-Life Example

Imagine deleting an old contact from your phone.

Once deleted, searching for that contact won't return any results.

The Delete operation works the same way.

Complete CRUD Workflow

Let's see the complete lifecycle of a document.

Create

Store Document

Index Document

Read Document

Update Document

Delete Document

Every application repeatedly performs these operations.

Example Scenario

Imagine you're managing an online bookstore.

Step 1: Add Book

Python Programming

₹799

Stored in Elasticsearch.

Step 2: Customer Searches

Search:

Python

Book appears.

Step 3: Price Changes

New Price:

₹699

Update the document.

Step 4: Book Goes Out of Print

Delete the document.

Simple!

Document IDs

Every document stored in Elasticsearch has a unique Document ID.

Think of it as an Aadhaar number or Employee ID.

Example:

Document ID

1001

1002

1003

Even if two products have the same name, their IDs will always be different.

Document IDs help Elasticsearch quickly identify the correct document.

Automatic vs Custom IDs

There are two ways to assign IDs.

Automatic ID

Elasticsearch automatically generates one.

Example:

P9kLm34AbX12

Useful when you don't care what the ID looks like.

Custom ID

You provide the ID yourself.

Example:

Product_1001

Useful when documents already have unique identifiers from another system.

What Happens if You Create a Document with an Existing ID?

Suppose document ID:

1001

already exists.

If you create another document with the same ID using a method that replaces documents, Elasticsearch overwrites the existing document.

That's why choosing IDs carefully is important.