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
Post a Comment