Sunday, 3 October 2021

Reading And Writing Documents With Single Document APIs In Elastic Search

 Topics

  1. Creating New document
  2. Getting Document
  3. Searching document (querying)
  4. Updating Document
  5. Deleting Document

Elastic search

What is Index?

 An index is like a “Database” in a relational database. Each Index may contain various Types in it.

What is a type?                                                                                                                                            

A type is like a “tables” in a relational database. Each type has various fields in it.

What is Document?

The document is a JSON document which is stored in elastic search. It is like a row in a table. Each document is stored in the index and has a type and document it.

What is a field?           

A document contains a list of fields in it. Fields are similar to a column in the relational database, which stores a single value. Like value types.

Definitions are referred from the link https://www.slideshare.net/ABCTalks/elastic-search-overview     

Let’s create a document --  first we need REST CLIENT.

There are various Rest clients available but for this demo, I am going to use Postman.

Elastic search

 

Comparing Query and JSON object

But if you see in the below query  I am getting data from more than one table by joining multiple tables but over the period of time this query will take time to get data but the solution to this is we are going to move this data return by the query to elastic search which will be easy to search.

Elastic search

 

Creating a New Document

To create a document in elastic search we are going to use restful APIs service provided by elastic search.

The URL of elastic search is divided into segments.

  1. Server Address is Elastic search address
  2. The index is Database Name (Database)
  3. Type is Table Name (type)
Elastic search

For creating the first document we are going to use HTTP post Request.

Steps

Enter URL - http://127.0.0.1:9200/timesheet/projectmaster 

    Elastic search

     

    After setting Request URL next we are going to set request headers.

    Set Header “Content-Type: application/json”

    Elastic search

      After setting headers next, we are going to enter request body.

      Enter Request Body

        We are going to insert  the below JSON data into elastic search.

        A snippet of Request JSON

        1. {  
        2.     "ProjectID""1",  
        3.     "ProjectName""ABC Bearings Ltd",  
        4.     "NatureofIndustry""Bearings",  
        5.     "ProjectCode""A001"  
        6. }  

        Elastic search

        After entering request URL, request headers, and Request body, next, we are going to Post Data.

         Posting data for creating a new document


        Elastic search

          After posting data to elastic search, we got a response.

          In response, you will get index and type name which you have set and a unique “_id” along with result as “created”.

          Elastic search

           

          After entering a single document, in a similar way, we can create other new documents in the elastic search.

          Note
          In a similar way, I have inserted 4 more document into index “timesheet”.

          After completing with inserting all documents next we get all documents from Elasticsearch.

          The five project which I have created for a demo.

          • "ProjectName": "Alok Industries Ltd"
          •  "ProjectName": "Ansa Pack (Simplex Group)"
          •  "ProjectName": "ABC Bearings Ltd"
          • "ProjectName": "Anil Bioplus Ltd (Anil Group Ahmedabad)
          • "ProjectName": "Alok Industries Ltd"

           

          Get Document

          The get API allows getting a typed JSON document from the index based on its id.

          The below snapshot contains a document id (“_id”) in which we are going to use for search

          A snippet of Response JSON

          1. {  
          2.     "_index""timesheet",  
          3.     "_type""projectmaster",  
          4.     "_id""B_K8nGMBcox2VuUwbivo",  
          5.     "_score": 1,  
          6.     "_source": {  
          7.         "ProjectID""2",  
          8.         "ProjectName""Alok Industries Ltd",  
          9.         "NatureofIndustry""Textile",  
          10.         "ProjectCode""A002"  
          11.     }  
          12. }  

          URL - http://127.0.0.1:9200/timesheet/projectmaster/B_K8nGMBcox2VuUwbivo

          Elastic search

           

          After setting parameter just send a request to get a response.

          Below is a response which we have received to a request which we have sent.

          Elastic search

           But in response, if we only want to view “_source” part is it possible?

          Yes, it is possible here is the answer to that.

          Just append “_source” keyword to your existing request --  that’s it.

          URL - http://127.0.0.1:9200/timesheet/projectmaster/B_K8nGMBcox2VuUwbivo/_source

          Elastic search

          Next we are going to learn how to search document.

          Querying Document (Search)

          Get all Data from Elastic search (match_all Query)

          Now let’s query data and get all data which we have inserted so far.

          1. To search data, we need to enter “_search” to URL.

          URL - http://127.0.0.1:9200/timesheet/projectmaster/_search      

          Elastic search

           

          1. Setting header

            Elastic search

          2. Setting Query for getting all documents in timesheet index and project master

          A snippet of Request JSON

          1. {  
          2.     "query": {  
          3.         "match_all": {}  
          4.     }  
          5. }  

          Elastic search

          After posting query next we get all responses to a document which we have entered.

          Elastic search

           

          In response, you can see we got a “total” of 5 documents, it is correct because we have inserted a total of 5 document into timesheet “index” and project master type.

          Below are the complete Response parameter Details

          • took – time in milliseconds for Elasticsearch to execute the search
          • timed_out – tells us if the search timed out or not
          • _shards – tells us how many shards were searched, as well as a count of the successful/failed searched shards
          • hits – search results
          • hits.total – total number of documents matching our search criteria
          • hits.hits – an actual array of search results (defaults to first 10 documents)
          • hits.sort - sort key for results (missing if sorting by score)
          • hits._score and max_score - ignore these fields for now

           

          Referenced from - https://www.elastic.co/guide/en/elasticsearch/reference/current/_the_search_api.html

          What is Shard?

          Elasticsearch provides the ability to subdivide your index into multiple pieces called shards. When you create an index, you can simply define the number of shards that you want. Each shard is in itself a fully-functional and independent "index" that can be hosted on any node in the cluster.

          Sharding is important for two primary reasons:

          It allows you to horizontally split/scale your content volume

          It allows you to distribute and parallelize operations across shards (potentially on multiple nodes) thus increasing performance/throughput

          Referenced from - https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html

          Elastic search

          If we expand hits we are going to see data which we have posted.

          Elastic search

           

          Now we have written a query to get all data, next let’s write a query to get specific data.

          Note
          What is Query DSL?

          The query DSL is a flexible, expressive search language that Elasticsearch uses to expose most of the power of Lucene through a simple JSON interface. It is what you should be using to write your queries in production. It makes your queries more flexible, more precise, easier to read, and easier to debug.

          Referenced from - https://www.elastic.co/guide/en/elasticsearch/guide/current/query-dsl-intro.html

          Get specific Data from Elastic search (match Query)

          In this part, we are going to search data with project name; for doing that we need to write a different query.

          You want to query for a full-text or exact value in almost any field then you can use (Match Query).

          1. To search data, we need to enter “_search” to URL.

          URL - http://127.0.0.1:9200/timesheet/projectmaster/_search          

          Elastic search

           

          1. Setting header

            Elastic search

          2. Query for getting all data that matches project name.

            Elastic search

          Snippet of JSON

          1. {  
          2.     "query": {  
          3.         "match": {  
          4.             "ProjectName""Ambuja Cement Ltd"  
          5.         }  
          6.     }  
          7. }  

          Elastic search

          After sending a request it returns 4 documents as output because it searches all data for a match.

          We have five project names but in that, only four project names have the word “Ltd”.

          Elastic search

           

          If I remove “Ltd” then it will only show one project name.

          Elastic search

           

          A snippet of Request Json

          1. {  
          2.     "query": {  
          3.         "match": {  
          4.             "ProjectName""Ambuja Cement "  
          5.         }  
          6.     }  
          7. }  

          Now if we send this above query request then we are going to get only one project name as output as shown below.

          Elastic search

           

          There are a  lot of ways to query elastic search data; you can learn more  here.

          After getting an idea of how to query data next let’s have a look at how to update the document.

          Updating Document (Modifying Your Data)

          To update the document, we need document id -- every document has document id (“_id”) which is auto-generated.

          A snippet of Response Json

          1. {  
          2.     "_index""timesheet",  
          3.     "_type""projectmaster",  
          4.     "_id""CPK8nGMBcox2VuUw5Ss2",  
          5.     "_score": 1,  
          6.     "_source": {  
          7.         "ProjectID""3",  
          8.         "ProjectName""Ambuja Cement Ltd",  
          9.         "NatureofIndustry""Cement”, ProjectCode""A003"  
          10.     }  
          11. }  

          Elastic search

          This is a parameter which we are going to set for updating the document.

           

          For the demo, we are going to update project name “Ambuja Cement Ltd” to “Ambuja Cement”.

          Elastic search

           

          Now we have set all parameter to update data.

          After updating data below is a response which we get.                     

          Snippet of Request Json

          1. {  
          2.     "doc": {  
          3.         "ProjectName""Ambuja Cement"  
          4.     }  
          5. }  

          Elastic search

          Now that we have updated data we must again call (match all Query).

           

          In the below snapshot, we got updated data,

          Elastic search

           

          A snippet of Request Json

          1. {  
          2.     "query": {  
          3.         "match_all": {}  
          4.     }  

           

          Now we are going to work on delete document.

          Delete Document (Deleting Your Data)

          To delete a document, we need document id. Every document has document id (“_id”) which is auto-generated.

          For deleting a document, we are going to set HTTP request to Delete.

          After that, we are going to set URL for delete request which contains an index, type, and document id.

          URL - http://127.0.0.1:9200/timesheet/projectmaster/CPK8nGMBcox2VuUw5Ss2

          Elastic search

           

          After setting parameter just send request to delete document.

          Below is the response which we receive after sending delete request.

          Elastic search

           

          Finally, we have completed performing CRUD operations with the elastic search.

          How To Configure Kibana

           What is Kibana?

          Kibana is an open source analytics and visualization platform designed to work with Elasticsearch. You use Kibana to search, view, and interact with data stored in Elasticsearch indices. You can easily perform advanced data analysis and visualize your data in a variety of charts, tables, and maps.

          Kibana makes it easy to understand large volumes of data. It’s simple, the browser-based interface enables you to quickly create and share dynamic dashboards that display changes to Elasticsearch queries in real time.

          Setting up Kibana is a snap. You can install Kibana and start exploring your Elasticsearch indices in minutes — no code, no additional infrastructure required.

          The definition is referenced from here.

          Kibana

          Prerequisites

          1. Node js (Because Kibana runs on Node js)
          2. Kibana

          Download Node js

          Link to download Node.js here.

          Kibana

           

          Download Kibana    

          Link to download Kibana, click here.

          Kibana

          After downloading the tool, we first thing need to install Node.js.

          Installing Node js

          Kibana

           

          After installing node.js, just check if it is running. Open windows command prompt and type “node” command in it.

          Kibana

           If the installation has succeeded, you are now in the command line mode of node.js.

          Configure Kibana

          Below is the view of unzipped Kibana.

          Kibana

           Now, we are going to run Kibana through command prompt.

          To run Kibana, go to to bin folder and type command “cmd” in search header.

          Kibana

          Kibana

           After entering the command, it will open path in command prompt.

          Kibana

           Next, enter “Kibana” and enter a command to run Kibana and press enter.

          Kibana

          Note

          Please check your Elastic Search instance should be running; after that only run Kibana server.

          Kibana

          The Kibana server is running now. Just copy URL: - http://localhost:5601

          Open this URL in the browser.

          If you get the same screen, then your Kibana server is running successfully.

          Kibana

           

          Next, click on Discover tab to “Create index pattern”.

          Discover tab

          What is “Create index pattern”.?

          Kibana uses index patterns to retrieve data from Elasticsearch indices for things like visualizations.

          After view appears it will ask for Index pattern here name Index which we have created in the previous tutorial.

          Index Name: - timesheet.

          As you will type index name it will tell you “Success!  Your index pattern matches # index”.

          Kibana

           

          After entering “Index patterns” next click on Next Step button.

          Kibana

           

          It will show you a message that “Index pattern does not contain any time field” that’s true because we do not have any time field in timesheet index.

          Click on Create Index pattern button to Create Index pattern.

          After creating an index, it will show you timesheet index with all fields in it as shown below.

          Kibana

           

          This is the same index which we have created in tutorial 1.

          Now after configuring index if you go to “Discover” tab again then it will display all data of timesheet index from elastic search.

          Kibana

           

          Visualize tab

          You can create your own Visualize data (index and type).

          Kibana

           

          Click on create a visualization button after clicking you will see various charts as shown below.

          Kibana

           

          Choose any chart to create your visualization.

          I have chosen pie chart, you can customize according to your requirements.

          Kibana

          Similarly you can create your dashboard and timelion.           

          Note
          You can also save your visualization via the button.

          Dashboard tab

          In this part, you can add your created visualization to the dashboard. But one thing you need to do when you create visualization is to save it, to save your visualization there is a save button at the right top on the page just click it to save, then you can use saved visualization on the dashboard.

          Kibana

          Timelion tab

          What is timelion?

          Timelion is a time series data visualizer that enables you to combine totally independent data sources within a single visualization. It’s driven by a simple expression language you use to retrieve time series data, perform calculations to tease out the answers to complex questions, and visualize the results.

          For example, Timelion enables you to easily get the answers to questions like,

          • How many pages does each unique user view over time?
          • What’s the difference in traffic volume between this Friday and last Friday?
          • What percent of Japan’s population came to my site today?
          • What’s the 10-day moving average of the S&P 500?
          • What’s the cumulative sum of all search requests received in the last 2 years?

          Referenced from the site check here.

          Kibana

          Next, we are going have a look at Dev Tools.

          Dev Tools tab

          This is a kind of rest client which is built in into Kibana for Searching data by querying it.

          Kibana

           

          Management tab

          The Management application is where you perform your runtime configuration of Kibana, including both the initial setup and ongoing configuration of index patterns, advanced settings that tweak the behaviors of Kibana itself, and the various "objects" that you can save throughout Kibana such as searches, visualizations, and dashboards.

          This section is plugin-able, so in addition to the out of the box capabilities, packs such as X-Pack can add additional management capabilities to Kibana.

          Reference link - https://www.elastic.co/guide/en/kibana/current/management.html

          Kibana

           

          Finally, in this article, we got to learn what Kibana is and how it works and for what purpose we should use it.

          How To Configure Elasticsearch On Windows

           Why Elasticsearch?

          Elasticsearch can be used as a powerful search tool for your application. Because if you work with the relational database, it won’t allow a search for a large database which has millions of data sets. Then,  you need to fine-tune it to achieve it, but Elasticsearch is readily available to search. You just need to Create an index, type, and Document and push the data into Elasticsearch and you are ready to search.

          As a developer, we mostly use a relational database for storing the data which helps us to easily join tables and pull this data from the database which we require. Over time, as these databases and tables become fat, we are not able to do an operation on these databases and tables.

          On the other hand, Elasticsearch tool can easily perform a search on millions of documents in a few seconds. The two unique and significant features of Elasticsearch are -
          1. Horizontal scale
          2. High availability

          Various Elasticsearch Clients

          • Java REST Client [6.2] — other versions
          • Java API [6.2] — other versions
          • JavaScript API
          • Groovy API [2.4] — other versions
          • .NET API [6.x] — other versions
          • PHP API [6.0] — other versions
          • Perl API
          • Python API
          • Ruby API
          • Community Contributed Clients

          For details of Elasticsearch Clients, click here.

          We are going to focus on .NET API in the entire series.

          Now, let’s start with setting up Elasticsearch. For doing that, first, we require to download the latest JDK.

          Downloading java development kit

          Since many of us are from .NET background, we may not know how to use Java in our programming. For that purpose, I am going to show it in simple steps so that a novice developer can also start using it in their application.

          For downloading JDK, click here.

          Elastic Search

           

          For this tutorial, I am going to use “Version 8 update 162” “(build 1.8.0_162-b12)”.

          If you want to work with the same version which I am using, then this is the link to download.

          1. http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html
          2. https://www.filehorse.com/download-java-development-kit-64/33594/

          After downloading Java JDK, next, we are going to install it.

          Setting Java Home Variable

          Once you have the JDK installation path, follow the below-mentioned process.

          • Right-click the My Computer icon on your desktop and select Properties.
          • Click the Advanced tab, then click the Environment Variables button.
          • Under System Variables, click New.
          • Enter the variable name as JAVA_HOME.
          • Enter the variable value as the installation path for the Java Development Kit.
          • Click OK.
          • Click Apply Changes.

          If you still do not get how to set up, then refer to this link here.

          After installing, just open Windows command prompt and type the following command -

          javac

          If you get the error “javac: command not found” that indicates javac path is not set properly. Still, try to restart the computer and once again check the command.

          If you get all Java options, then you have set the path properly, as shown below.

          Elastic Search

           

          After completing the setting of Java Home Variable, next, we are going to download Elasticsearch.

          Downloading Elasticsearch

          Elastic Search

           

          After clicking on Download button, it will take you to another page where you will have various download options. In that, we are going to choose Zip options.

          Elastic Search

           Given below is the view of Elasticsearch files after unzipping the folder.

          Elastic Search

          Next, run the Elasticsearch tool.

          Run Elastic search

          Go to the bin folder of Elasticsearch.

          Elastic Search

          Then, in header, type “cmd”. This will open the command prompt on the folder path you have set.

          Elastic Search

          After coming to this path, next, enter “elasticsearch” keyword to start its instance, as shown below.

          Elastic Search

          It will run on “127.0.0.0” address with port no “9200”.   

          Elastic Search      

          Now, open your browser and enter localhost address “127.0.0.1” and with port no “9200”.

          Elastic Search

          If you get this page, then you have successfully started Elasticsearch instance.

          Now, we have completed the configuration of Elasticsearch on Windows. In the next part, we are going to learn how to create Index and document in Elasticsearch.