Elasticsearch Essentials: Quick Start, Index Management, Mapping, and Advanced Operations
The article offers a thorough, step‑by‑step guide to Elasticsearch, explaining how to check cluster health, create and manage indices, define mappings and field types, use dynamic mapping, and perform maintenance tasks such as shrink, split, rollover, and cache management, all illustrated with concrete API examples.
This article provides a comprehensive introduction to Elasticsearch, covering basic concepts, quick‑start commands, index management, mapping definitions, field types, dynamic mapping, and various maintenance operations.
Quick Start
Check cluster health: http://localhost:9200/_cat/health?v . View nodes: http://localhost:9200/_cat/?v . List indices: http://localhost:9200/_cat/indices?v .
Create an index named customer with pretty output:
PUT /customer?pretty
Index a document:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
Retrieve the document:
curl -X GET "localhost:9200/customer/_doc/1?pretty"
Search all documents:
GET /customer/_search?q=*&sort=name:asc&pretty
Or using JSON body:
GET /customer/_search { "query": { "match_all": {} }, "sort": [ {"name": "asc"} ] }
Index Management
Create an index twitter with 3 primary shards and 2 replicas:
PUT twitter { "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 2 } } }
Define a mapping for the index:
PUT twitter { "mappings": { "_doc": { "properties": { "type": {"type": "keyword"}, "name": {"type": "text"}, "user_name": {"type": "keyword"}, "email": {"type": "keyword"}, "content": {"type": "text"}, "tweeted_at": {"type": "date"} } } } }
Alias definition, index templates, and other advanced settings are demonstrated with examples such as:
PUT _template/template_1 { "index_patterns": ["te*", "bar*"], "settings": {"number_of_shards": 1}, "mappings": {"type1": {"_source": {"enabled": false}, "properties": {"host_name": {"type": "keyword"}, "created_at": {"type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY"}}} }
Mapping Details
Mapping defines field names, types, and attributes. Core datatypes include text , keyword , numeric types, date , and boolean. Complex types such as object and nested are also covered.
Example of a multi‑field definition:
PUT my_index { "mappings": {"_doc": {"properties": {"city": {"type": "text", "fields": {"raw": {"type": "keyword"}}}}}} }
Dynamic mapping automatically creates fields based on incoming JSON. Date detection and numeric detection can be enabled or disabled with settings like "date_detection": false or "numeric_detection": true .
Maintenance Operations
Cache clearing: POST /twitter/_cache/clear . Refresh: POST /_refresh . Flush: POST twitter/_flush . Force merge: POST /kimchy/_forcemerge?max_num_segments=1 .
Index shrink, split, rollover, and monitoring APIs are illustrated with the corresponding POST and GET requests.
Overall, the article serves as a practical reference for developers and engineers working with Elasticsearch, providing ready‑to‑use commands and explanations for everyday tasks.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.