Elasticsearch Basics: Overview, Operations, and Integration Guide
This article introduces Elasticsearch as a scalable distributed search engine, compares it with Solr, walks through local, Docker, and Kubernetes installation, explains its document‑oriented data model, demonstrates CRUD operations via REST API, and shows how to integrate it with Spring Boot using the official Java client.
1. Overview
Elasticsearch (ES) is an open‑source, highly scalable distributed full‑text search engine built on Apache Lucene. It stores and retrieves data in near‑real‑time, can scale to hundreds of nodes and petabyte‑level data, and exposes a simple RESTful API that hides Lucene’s complexity.
1.1 Elasticsearch vs. Solr
Both ES and Solr are built on Lucene and provide similar core capabilities, but ES is generally easier to install, JSON‑centric, and better suited for log‑management and metric collection, while Solr has a larger, more mature community and richer XML configuration.
Typical large‑scale adopters include GitHub (20 TB, 1.3 billion files), Wikipedia, SoundCloud, Baidu, Sina, Alibaba, and Stack Overflow.
2. Installation
2.1 Local binary
Download the appropriate package from the official site, unzip, and run bin/elasticsearch. After startup, http://127.0.0.1:9200 returns node information.
{
"name": "node-1",
"cluster_name": "es-cluster",
"version": {
"number": "7.15.2",
"lucene_version": "8.9.0"
},
"tagline": "You Know, for Search"
}For a multi‑node cluster on a single machine, start each node with a unique node.name and path.data:
bin/elasticsearch -E node.name=node-1 -E cluster.name=es-cluster -E path.data=node1_data
bin/elasticsearch -E node.name=node-2 -E cluster.name=es-cluster -E path.data=node2_data
bin/elasticsearch -E node.name=node-3 -E cluster.name=es-cluster -E path.data=node3_dataPort 9200 handles HTTP requests; port 9300 is used for inter‑node communication.
2.2 Docker
Pull the official image and run a single‑node container:
docker pull elasticsearch:7.4.2
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-d elasticsearch:7.4.22.3 Kubernetes (brief)
Deploying ES on Kubernetes requires a more complex configuration and is omitted here.
3. Data Model
ES stores documents in an index (analogous to a database). Types have been deprecated; each index now contains a single type. A document is a JSON object.
4. CRUD Operations via REST API
Creating an index with 3 primary shards and 2 replicas:
PUT product
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}Adding a document (POST generates an ID, PUT requires an explicit ID):
POST product/_doc/1
{
"name":"Apple iPhone 13 Pro",
"category":"phone",
"price":6599,
"stock":100,
"brand":"Apple",
"desc":"Excellent smartphone"
}Updating a field without overwriting the whole document:
POST product/_doc/1/_update
{
"doc": {
"name":"Red Fuji Apple"
}
}Bulk insertion, range queries, sorting, pagination, field filtering, boolean combinations, fuzzy search, highlighting, aggregations, and term grouping are demonstrated with the corresponding DSL snippets in the source.
5. Spring Boot Integration
Two common approaches are the low‑level elasticsearch‑rest‑high‑level‑client and Spring Data Elasticsearch. Adding the following Maven dependencies enables the REST client:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch‑rest‑high‑level‑client</artifactId>
</dependency>
... (logging and Jackson dependencies omitted for brevity) ...Sample Java code creates a RestHighLevelClient, builds IndexRequest, UpdateRequest, DeleteRequest, and executes searches using the DSL shown earlier.
These steps provide a complete foundation for using Elasticsearch in Java applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Shepherd Advanced Notes
Dedicated to sharing advanced Java technical insights, daily work snippets, and the power of persistent effort.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
