Master ElasticSearch: Install, Index, and Run Advanced Java Queries

This guide walks you through downloading and installing ElasticSearch, explains core concepts like indices, types, documents, and fields, demonstrates CRUD operations via RESTful APIs, shows advanced query techniques, and provides complete Java integration examples using Maven and Docker.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Master ElasticSearch: Install, Index, and Run Advanced Java Queries

First, download ElasticSearch from the official website and extract the Windows package; run elasticsearch.bat in the bin directory and verify the service by visiting http://localhost:9200/, which should display a successful startup response.

ElasticSearch is a Lucene‑based distributed search server offering full‑text search via a RESTful web interface. Its main concepts are indices (analogous to databases), types (tables, removed from version 7.x), documents (rows), and fields (columns).

Using tools like Postman, you can perform index operations through HTTP methods: PUT /user creates an index, GET /user retrieves it, and DELETE /user removes it. The _cat endpoint lists all indices.

Document handling follows similar patterns. To create a document, send a POST /user/_doc request with a JSON body; the response contains an _id indicating the operation is not idempotent. Specifying an _id yourself makes the request idempotent, allowing a PUT method. Updating can be done with POST /user/_doc/_update (partial update) or PUT /user/_doc (full update). Deleting uses DELETE /user/_doc/{id}.

Advanced queries include:

Simple match queries via GET /user/_search?q=name:zhangsan or request bodies using match.

Pagination with from (page offset) and size (page size) parameters.

Sorting by adding a sort field in the request body.

Boolean combinations using bool with must (AND) or should (OR) clauses.

Range filters with range to limit numeric fields, e.g., age between 20 and 30.

Exact phrase matching via match_phrase and result highlighting with the highlight section.

Aggregations for grouping and metrics, such as terms on age or avg to compute average age.

To integrate ElasticSearch into a Java project, create a Maven module and add dependencies for org.elasticsearch:elasticsearch, the high‑level REST client, Log4j, and Jackson. Optionally, run ElasticSearch in Docker:

docker pull elasticsearch:7.4.2
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
chmod 777 /mydata/elasticsearch/data
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
  -e "discovery.type=single-node" \
  -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
  -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
  -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
  -d elasticsearch:7.4.2

In Java, instantiate a RestHighLevelClient with the target host, then use the client to:

Create an index with CreateIndexRequest and check isAcknowledged().

Retrieve index metadata via GetIndexRequest.

Delete an index using DeleteIndexRequest.

Index documents by converting POJOs (e.g., an Employee bean) to JSON with Gson and sending an IndexRequest.

Update documents with UpdateRequest (partial) or IndexRequest (full).

Get documents using GetRequest and delete them with DeleteRequest.

Perform bulk operations via BulkRequest for batch creation or deletion.

Execute advanced searches with SearchRequest, building queries with QueryBuilders (e.g., matchAllQuery, termQuery, boolQuery, rangeQuery), and control pagination with from and size.

Sample Java test methods illustrate each operation, printing results such as index creation status, retrieved mappings, and search hits. The article concludes that these steps provide a solid foundation for using ElasticSearch in Java applications.

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.

JavaIndexingElasticsearchREST APIinstallationSearchAdvanced Queries
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.