Master Elasticsearch Index & Document Operations with RESTful APIs
This guide walks you through creating, viewing, updating, and deleting Elasticsearch indices, documents, and mappings using RESTful HTTP methods, complete with example requests, JSON bodies, and visual illustrations for each operation.
Environment: Elasticsearch 7.8.0 + Postman
Elasticsearch is a Lucene‑based search server offering a distributed, multi‑user full‑text search engine via RESTful web interfaces. Developed in Java and released under the Apache license, it is a popular enterprise search solution used in cloud computing for real‑time, reliable, and fast search. Official clients exist for Java, .NET (C#), PHP, Python, Groovy, Ruby, and many other languages.
Index Operations
1. Create Index
Method: PUT
URL: http://localhost:9200/users
Optional body (settings):
<code>{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}</code>number_of_shards: number of primary shards
number_of_replicas: number of replica shards
2. View All Indices
Method: GET
URL: http://localhost:9200/_cat/indices?v
3. View Single Index
Method: GET
URL: http://localhost:9200/users
4. Delete Index
Method: DELETE
URL: http://localhost:9200/users
Document Operations
1. Create Document
Method: POST
URL: http://localhost:9200/users/_doc
<code>{"name":"张三", "sex":"男", "age":30}</code>2. Create Document with Specified ID
Method: POST
URL: http://localhost:9200/student/_doc/唯一I
<code>{"name":"莉莉", "sex":"女", "age":30}</code>3. View Document
Method: GET
URL: http://localhost:9200/users/_doc/唯一ID
4. Update Document (Replace)
Method: POST
URL: http://localhost:9200/users/_doc/唯一I
<code>{"name":"莉莉007", "sex":"女", "age":60}</code>5. Update Document (Partial)
Method: POST
URL: http://localhost:9200/users/_update/唯一ID
<code>{
"doc": {
"age": 50
}
}</code>6. Delete Document
Method: DELETE
URL: http://localhost:9200/users/_doc/唯一ID
7. Delete Documents by Query
Method: POST
URL: http://localhost:9200/users/_delete_by_query
<code>{
"query": {
"match": {
"age": 50
}
}
}</code>Create Mapping
1. Define Mapping
Method: PUT
URL: http://localhost:9200/users/_mapping
<code>{
"properties": {
"name": {"type": "text", "index": true},
"sex": {"type": "text", "index": true},
"age": {"type": "long", "index": true}
}
}</code>2. View Mapping
Method: GET
URL: http://localhost:9200/users/_mapping
Finished!
Next article will cover advanced Elasticsearch query RESTful APIs.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.