Skip to main content

Elasticsearch- II

 ELASTICSEARCH                                         



In this blog we are going to learn how to use elasticsearch and kibana using python.

Before getting into this, make sure that you have elasticsearch and kibana installed. To know the detailed procedure about the installation of elasticsearch and kibana, click the link below to learn the installation process from scratch.

https://crescenttechnocratsclub.blogspot.com/2022/09/blog-post.html

For python, let’s use VS Code . Visual Studio (VS) Code is a streamlined code editor with support for development operations like debugging, task running, and version control. Python IDLE is also fine to use but we will work with vs code in this blog. Install VS code using the following link :https://code.visualstudio.com/download

Click open folder and open the folder in which elasticsearch and kibana are installed in vs code as shown below. 

                                     




Now create a new python file as shown below with any name of your choice (example:namethatyouwant.py).

                               



Type the following code to connect elasticsearch with python.

                        


Save and run the program (Run button in vs code is at the top right corner)

Now, you have successfully connected elasticsearch with python.

Let’s create an index,

               


Run the program. If you get “true” as your output, your index has been successfully created. If not, try again with some other index name.

Now, let’s update some data and visualize them using kibana.

Follow the below code to update the data into the index that you have created.

Just comment (#) for the lines of code used to create an index and follow the remaining lines of code. If we try to create an existing index, it’ll show an error stating that the index already exists. 

                     

In the above code, ‘name’ is the key and ‘karthikaa’ is the value of that particular key(i.e., key: value format).

Your key and value can also be in numbers.

Now save and run the program.

Let’s view our data using kibana.

Open kibana (i.,e local:5601), now to view the data that we have updated, we should first create our index in Kibana.

To create that, click on “Connect to your Elasticsearch index” as shown in the illustration below.

                                         

Type the entire index name which you have created and click on the Next step followed by create index pattern.

Now, let’s check our data by clicking the discover icon that was mentioned in the previous steps. After getting into a new page, change the index name as shown below

                




You can also get particular data using their id and can also change and update our data. To get a particular data using its id, use the following code.

                        


Now let’s try to delete an index. Follow the code given below.

                         

As a result, there will be no index named “newindex” anymore. You can check it through kibana.

 Let’s dive into the fun part of this article, which is searching.

We can search elements using “query” and “match”.

Before jumping to this, make sure that you have created an index and updated some data into it.

To use a query for searching an element follow the code that is illustrated below with its output in the terminal.

In the below code, I have created an index named “karu” and updated it with random keys and values. Inside the body, the query is used. After that search is done based on the query which is inside the body. The advantage of using a query is that we can search our element even just with its first or first few letters but that is not the case for the match.

      


Now, we will see how to search for an element using a match. Comment or delete the lines of code (i.e., body part) used for search using query and replace it with the code given below.

Let us get the element to be searched by the user and search it.   

                 


       

As we use a match to search, the results are shown when the element is searched with all of its letters. It will not show results if we enter a few letters of that element. This is explained in the above illustration’s terminal.

So, here we have come to an end by learning how to use elastic search and visualize it through kibana.

Thank you.

Comments

Popular posts from this blog

Getting started with WSL

I recently started learning Linux operating system. I have a windows machine. I searched for what are the possible ways available to work with Linux. I ended up with more ways to install Linux and work with it. WSL is one of the simplest ways to get started with Linux on windows. So, I'll be sharing my learnings about WSL. It's obvious that to learn Linux one should have a Linux-based machine to work with and execute commands. There are four possible ways to get started with Linux. Possible ways to work with Linux Building a Linux-based computer which is quite expensive. I myself don't prefer to build a machine when I am a Student. Installing and using Linux in dual boot mode with your existing Windows using a virtual box. This is one of my preferred ways but it consumes more storage. So, this too fails. And the next way is to create instances in cloud platforms like AWS, GCP, and Azure and using remotely. Also, there is no cost associated with it. Because major cloud platf...

Why you need Linux Right Now!

  Introduction Linux is an open-source operating system created in 1991 by Linus Torvalds. It's an alternative to the more popular Windows and Mac operating systems, and it comes with a wide range of features that make it a great choice for everyone, from beginners to seasoned professionals. In this blog, we'll discuss what Linux is, what it can offer you, and why you should start using it today. What Is Linux? Linux is used today by millions of people worldwide, including large corporations, small businesses, government organizations, and individuals. There are many reasons why you should use Linux. Here are a few: Linux is stable and secure. It is less likely to crash or become infected with a virus than Windows. Linux is customizable. You can change the look and feel of Linux to suit your needs, and you can add or remove software to create the perfect operating system for you. Linux is free. There are no licensing fees required to use Linux, and there are many free applicati...