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...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...