Skip to main content

Elasticsearch : Getting started with Elasticsearch & Kibana

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
}

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...