Home Post BigData

Elasticsearch : Getting started with Elasticsearch & Kibana

Mar 31, 2024

In this article we will take a quick look at downloading, installing and performing CRUD operations on Elasticsearch using Kibana.

1) Download and Install Elasticsearch

Download the latest Elasticsearch release and extract it:

% tar -zxvf elasticsearch-8.2.2-darwin-x86_64.tar.gz
% cd elasticsearch-8.2.2

Run the following command in order to start Elasticsearch:

% bin/elasticsearch

Look into the logs and note down content between two solid black lines, we need this information to connect/configure Kibana to the ES:

2) Download and Install Kibana

Download the latest Kibana release and extract it:

% tar -zxvf kibana-8.2.2-darwin-x86_64.tar.gz
% cd kibana-8.2.2

Run the following command in order to start Kibana:

% bin/kibana

In Kibana logs, you should see something like this:

Kibana has not been configured.
Go to http://localhost:5601/?code=393018 to get started.

Let's configure Kibana with "Elasticsearch" information:

Put "Enrollment token" copied from previous step in the text area and click "Configure Elastic".

Enter username/password in the form and login, (default username/password are present in elasticsearch logs copied in previous step.).

If everything went right, you should see welcome screen - choose "Explore on my own":

From "top-left" nav select "Dev Tools" under "Management" section:

3) Basic Queries

Let's perform some basic cluster/health related queries to make sure everting is working fine:

3.1) Cluster health

GET _cluster/health

3.2) Nodes in a cluster

GET _nodes/stats

4) Elasticsearch CRUS operations with Kibana

Let's perform "create, read, update and delete" operations on Elasticsearch with the help of Kibana:

4.1) Create an index

PUT codeburps-demo
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "codeburps-demo"
}

4.2) Index a document

When indexing a document, both HTTP verbs POST or PUT can be used.

POST is used when we want Elasticsearch to autogenerate an id for the document.

POST codeburps-demo/_doc
{
  "first_name": "lily",
  "last_name": "parmer"
}
{
  "_index" : "codeburps-demo",
  "_id" : "KJQnYYEBp6bDSI4ZnX6o",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

PUT is used when we want Elasticsearch to assign a specific id for the document.

PUT codeburps-demo/_doc/1
{
  "first_name": "Lily",
  "last_name": "James"
}
{
  "_index" : "codeburps-demo",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

Note: When indexing/adding a document using an existing id, the existing document is overwritten by the new document. We can use the _create endpoint to avoid this situation!

With the _create Endpoint, no indexing/addition/updation will occur if a document with provided id already exists in ES.

PUT codeburps-demo/_create/1
{
  "first_name": "Update_Lily",
  "last_name": "Updated_James"
}
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, document already exists (current version [1])",
        "index_uuid" : "00ZsLAuYQ9ipn93sUeUyDw",
        "shard" : "0",
        "index" : "codeburps-demo"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[1]: version conflict, document already exists (current version [1])",
    "index_uuid" : "00ZsLAuYQ9ipn93sUeUyDw",
    "shard" : "0",
    "index" : "codeburps-demo"
  },
  "status" : 409
}

4.3) Read a document

GET codeburps-demo/_doc/1
{
  "_index" : "codeburps-demo",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "Lily",
    "last_name" : "James"
  }
}

4.4) Update a document

POST codeburps-demo/_update/1
{
  "doc": {
    "last_name": "Updated_James"
  }
}
{
  "_index" : "codeburps-demo",
  "_id" : "1",
  "_version" : 3,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

4.5) Delete a document

DELETE codeburps-demo/_doc/1
{
  "_index" : "codeburps-demo",
  "_id" : "1",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

avatar

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Categories
Spring Framework
Microservices
BigData
Core Java
Java Concurrency