Build Your Own Full-Text Search Engine with Elasticsearch: Step‑by‑Step Guide

This tutorial walks you through installing Elasticsearch, configuring Java and network settings, understanding core concepts like nodes, clusters, indices and documents, setting up Chinese analyzers, performing CRUD operations, and executing powerful full‑text queries using the Elasticsearch REST API.

21CTO
21CTO
21CTO
Build Your Own Full-Text Search Engine with Elasticsearch: Step‑by‑Step Guide

Installation

Elasticsearch requires a Java 8 runtime. After installing Java and setting JAVA_HOME, download the zip package, unzip it, and start the server with ./bin/elasticsearch. If the virtual memory limit is too low, run sudo sysctl -w vm.max_map_count=262144. Verify the installation with curl localhost:9200, which returns a JSON object describing the node and cluster. To allow remote access, edit config/elasticsearch.yml and set network.host: 0.0.0.0, then restart.

Basic Concepts

Node and Cluster

A node is a single Elasticsearch instance; a cluster is a group of nodes working together.

Index

An index is the top‑level logical container for data, implemented as an inverted index. Use curl -X GET 'localhost:9200/_cat/indices?v' to list all indices.

Document

Each document is a JSON record stored in an index. Documents in the same index do not need identical structures, though a consistent schema improves search performance.

Type

Types were logical groupings of documents within an index. Starting with Elasticsearch 6.x each index can contain only one type, and types are removed entirely in 7.x.

Creating and Deleting an Index

Create an index with a PUT request, e.g. curl -X PUT 'localhost:9200/weather'. The response contains an acknowledged flag. Delete the index with curl -X DELETE 'localhost:9200/weather'.

Chinese Analyzer Configuration

Install the IK analyzer plugin:

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip

Restart Elasticsearch, then create an index with fields that use the ik_max_word analyzer for both indexing and searching.

$ curl -X PUT 'localhost:9200/accounts' -d '{
  "mappings": {
    "person": {
      "properties": {
        "user": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word"},
        "title": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word"},
        "desc": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word"}
      }
    }
  }
}'

Data Operations

Add a Document

Use PUT with a specific ID:

$ curl -X PUT 'localhost:9200/accounts/person/1' -d '{"user":"张三","title":"工程师","desc":"数据库管理"}'

Or let Elasticsearch generate an ID with POST:

$ curl -X POST 'localhost:9200/accounts/person' -d '{"user":"李四","title":"工程师","desc":"系统管理"}'

Retrieve a Document

$ curl 'localhost:9200/accounts/person/1?pretty=true'

The response includes found:true and the original _source.

Update a Document

$ curl -X PUT 'localhost:9200/accounts/person/1' -d '{"user":"张三","title":"工程师","desc":"数据库管理,软件开发"}'

The response shows _version incremented and result:"updated".

Delete a Document

$ curl -X DELETE 'localhost:9200/accounts/person/1'

Data Query

Retrieve All Documents

$ curl 'localhost:9200/accounts/person/_search'

The hits.total field indicates the number of matching records.

Full‑Text Search

Match query on the desc field:

$ curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"desc":"软件"}}}'

Control result size with the size parameter and pagination with from.

$ curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"desc":"管理"}},"size":1}'
$ curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"desc":"管理"}},"from":1,"size":1}'

Boolean AND Query

$ curl 'localhost:9200/accounts/person/_search' -d '{"query":{"bool":{"must":[{"match":{"desc":"软件"}},{"match":{"desc":"系统"}}]}}}'

References

Elasticsearch official guide: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html

A Practical Introduction to Elasticsearch: https://www.elastic.co/blog/a-practical-introduction-to-elasticsearch

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.

Backend DevelopmentElasticsearchREST APIQuery DSLfull-text searchdata indexingChinese Analyzer
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

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.