Big Data 9 min read

Quick Elasticsearch Guide: Install, Core Concepts, and CRUD Operations

This article introduces Elasticsearch as a reliable open‑source search and analytics engine, explains its key concepts such as clusters, nodes, indexes, types and documents, walks through installation steps, and demonstrates essential CRUD operations using RESTful curl commands.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Quick Elasticsearch Guide: Install, Core Concepts, and CRUD Operations

1. Introduction

Elasticsearch is a highly reliable open‑source full‑text search and analytics engine that allows real‑time storage, querying, and analysis of massive data.

It is easy to install, stable, fast, and real‑time, making it a popular enterprise‑grade search engine.

Example use cases:

Product search in online stores, enabling fast search and autocomplete.

Data collection for trend analysis, statistics, anomaly monitoring using Logstash to ingest data into Elasticsearch.

2. Basic Concepts

Cluster

A cluster is a group of servers that together store data and provide indexing and search capabilities.

Clusters have names (default "elasticsearch"); a server can belong to only one cluster, so naming (e.g., logging‑dev, logging‑prod) should be planned.

Node

A node is a single server within a cluster, identified by a name (default a UUID).

Index

An index is a collection of documents with similar attributes, such as customer data, product categories, or order data.

Index names are lowercase identifiers.

Type

Within an index, one or more types can be defined to logically categorize documents.

For example, a blog system may store all data in one index with separate types for users, posts, and comments.

Document

A document is the basic unit that can be indexed, represented in JSON, and stored in an index or type.

Documents can contain any fields and must belong to a specific type within an index.

3. Installation

Prerequisite: Java environment installed.

Download:

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz

Extract: tar -xvf elasticsearch-5.2.2.tar.gz Start:

cd elasticsearch-5.2.2/bin
./elasticsearch

The startup logs end with started when the node is ready.

4. Operation Examples

List all indexes

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Create an index

Create an index named customer:

curl -XPUT 'localhost:9200/customer?pretty&pretty'

List indexes again to verify creation:

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Add a document to an index

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

This adds a document with ID 1 to the customer index, type external.

Document content:

{
  "name": "John Doe"
}
Elasticsearch will automatically create the index if it does not exist.

Retrieve a document by ID

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

Response:

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

Delete an index

curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

The customer index is now removed.

Replace a document

First create a document with ID 1:

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' \
-H 'Content-Type: application/json' -d'
{
  "name": "华仔"
}
'

Verify:

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

Replace it with a new document (same ID):

curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' \
-H 'Content-Type: application/json' -d'
{
  "name": "德华"
}
'

Verify replacement:

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

Update document fields

Update the name field of ID 1:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "学友" }
}
'

Update name and add an age field:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "富城", "age": 20 }
}
'

Verify the document:

curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'

Use a simple script to increment age by 5:

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'

Delete a document

curl -XDELETE 'localhost:9200/customer/external/1?pretty&pretty'

5. Summary

From the hands‑on examples, Elasticsearch proves to be very convenient, with no complex installation, REST‑style operations that are easy to understand, and concepts that are straightforward.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ElasticsearchREST APIInstallationCRUD Operations
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.