Learn Elasticsearch With Mangas: A 5 Steps Hands-on (Create, Insert, Read, Update and Delete).

Akintola L. F. ADJIBAO
4 min readJun 23, 2021

--

Image by sebastianperezhdez from Pixabay

One Piece — Elichiro Oda, Golgo 13 — Takao Saito, Dragon Ball — Akira Toriyama: does it ring a bell? More than 1 billion of sales: just unbelievable. Now imagine that you’re able to create an Elasticsearch’s datastore where you can store your favorite mangas’ data, analyze them to get insights.

I don’t think of myself as a huge fan of mangas but since I saw those statistics, I’ve hardly missed the opportunity to use mangas to learn new stuff.

And that what we’re going to do in this article with Elasticsearch. In this hands-on article, we’ll learn how to perform basic operations, I mean how to Create, Read, Update and Delete a record in Elasticsearch.

Actually, what is Elasticsearch?

Elasticsearch is a software (we call it an engine) used to store and perform advanced searches and analysis on stored data of all types, including textual, numerical, geospatial, structured, and unstructured. Want to know more about Elasticsearch’s fundamentals, read this article Get to know Elasticsearch, the Giant of Searching and Analytics.

Ready? Without further discussion, let’s dive right in.

Prerequisites

  • Elasticsearch installed: Yes you need to install Elasticsearch before we start, follow this link How to install Elasticsearch.
  • curl: on most Linux distributions, curl is already installed but you can install it by yourself.

Okay, amigo, let’s get started!

1. Let’s create our mangas’ index

The first step in our journey to set up an Elasticsearch datastore is to create an index. Think of an index as a database table where you can store the same kind of information (maybe a list of songs or a list of your favorite books). In this article Elasticsearch 7.13 is there: Software Engineer, Get to Know the Giant of Searching and Analytics, you’ll find an in-depth explanation of what is an index.

So to create an index, execute this command in a terminal:

curl -XPUT -H "Content-Type: application/json" "127.0.0.1:9200/mangas?pretty"

?pretty is not mandatory. You can leave it. It’s used to format the result of our curl command result in a fancy way. Well, our index is created. Now, let’s look at the interesting things 👌. We’re going to insert our first manga record.

2. Create/Insert your first manga in Elasticsearch

What pieces of information do you think we need to store about manga?

Basically, I think there are 4 attributes to look at:

  • the id of our manga so that we can track it easily,
  • the title,
  • the author,
  • and the URL of the related Anime. Great.

One of the well-known mangas up this time is inevitably One Piece. The first thing I really like about it is the way the characters were drawn.

To insert it in your Elasticsearch index, copy and execute this command in your terminal.

Let’s break this command down:

  • curl: is a tool — more specifically, a command-line tool— used here to interact with Elasticsearch API;
  • -H “Content-Type: application/json”: here we specify to Elasticsearch’s API that we want to interact with it using JSON;
  • “127.0.0.1:9200/mangas/_doc/1”: 127.0.0.1:9200 (IP: Port ) is the address on which Elasticsearch is running on our local machines, mangas is the name of your index and _doc/1 indicates the unique identifier of the manga we want to insert;
  • {“id”: 1, …, “url”: “https://myanimelist.net/manga/13/One_Piece”}: this is the main part of our operation. We indicate to Elasticsearch the data of your manga.

So far so good. We have one manga record in our index. Great.

3. Read

The second operation of your learning journey is reading.

Let us test if our manga has been well inserted. For that, run this command:

curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/mangas/_search?pretty"

Have you got the data of the manga we’ve just inserted? Wonderful 👏!

Here we’re asking Elasticsearch through its API to show us all the mangas we have in our store. For that reason, we use GET to gather the list of mangas from our store.

4. Update

Let’s assume that we made a mistake on the URL or it’s not working anymore. We have to update the URL of our manga’s anime.

To do so, let’s run this command:

The structure of this command is almost the same as the one we used to query the list of mangas. The main things that changed are the operation verb — POST instead of GET and our manga reference link that’s got _update now.

Let us check if the update has been done.

curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/mangas/_search?pretty"

Our manga record has been updated. Perfect!

5. Delete 😢

Unfortunately, we have to delete our manga from Elasticsearch. But since we know how to insert it in our index, no problem. Go ahead and delete our record with this command.

curl -XDELETE -H "Content-Type: application/json" "127.0.0.1:9200/mangas/_doc/1?pretty"

Here we’re using the verb DELETE to tell to Elasticsearch we’d like to delete our manga record.

Et voila !!!

Let us check again if there is any manga over here.

curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/mangas/_search?pretty"

Our manga is no more there.

End of our journey. I can’t say that you’re already the Goku of Elasticsearch but if you keep on, you’ll soon become very proficient at it.

Now you’ve got the power to perform CRUD operations against an Elasticsearch index. I’d like to know what you’re planning to do with Elasticsearch in the next couple of days. Till next time, take care.

--

--

Akintola L. F. ADJIBAO
Akintola L. F. ADJIBAO

Written by Akintola L. F. ADJIBAO

Senior DevOps Engineer (8+years), Speaker, CKA | AWS Certified DevOps Professional | Terraform Certified https://www.upwork.com/freelancers/~01d11b6c65945a1160

No responses yet