Elasticsearch Index and Document Operations Tutorial
This tutorial explains how to create, query, update, and delete Elasticsearch indices and documents using RESTful HTTP requests, covering basic CRUD operations, various query types, pagination, sorting, aggregations, highlighting, and mapping definitions with practical JSON examples.
This article provides a step‑by‑step guide to using Elasticsearch for index and document management via its REST API.
Index operations
Create an index:
PUT http://127.0.0.1:9200/shoppingGet an index:
GET http://127.0.0.1:9200/shoppingList all indices:
GET http://127.0.0.1:9200/_cat/indices?vDelete an index:
DELETE http://127.0.0.1:9200/shoppingDocument operations
Create a document (auto‑generated ID):
POST http://127.0.0.1:9200/shopping/_docCreate a document with a custom ID:
POST http://127.0.0.1:9200/shopping/_doc/1001Put (upsert) a document with a specific ID (idempotent):
PUT http://127.0.0.1:9200/shopping/_doc/1001Get a document by ID:
GET http://127.0.0.1:9200/shopping/_doc/1001Search all documents:
GET http://127.0.0.1:9200/shopping/_searchUpdate a document completely:
PUT http://127.0.0.1:9200/shopping/_doc/1001
{ "name": "商品" }Partial update (scripted or doc):
POST http://127.0.0.1:9200/shopping/_update/1001
{ "doc": { "name": "局部修改商品" } }Delete a document:
DELETE http://127.0.0.1:9200/shopping/_doc/1001Query examples
Simple term query:
GET http://127.0.0.1:9200/shopping/_search?q=category:小米Match query (recommended):
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match": { "category": "小米" } }
}Match‑all query:
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_all": {} }
}Pagination (from, size):
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_all": {} },
"from": 0,
"size": 10
}Field‑source filtering:
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_all": {} },
"from": 0,
"size": 10,
"_source": ["title"]
}Sorting by price descending:
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_all": {} },
"from": 0,
"size": 10,
"_source": ["title"],
"sort": { "price": { "order": "desc" } }
}Boolean must (AND) query:
GET http://127.0.0.1:9200/shopping/_search {
"query": {
"bool": {
"must": [
{ "match": { "category": "小米" } },
{ "match": { "price": 1999.00 } }
]
}
}
}Boolean should (OR) query:
GET http://127.0.0.1:9200/shopping/_search {
"query": {
"bool": {
"should": [
{ "match": { "category": "小米" } },
{ "match": { "price": 1999.00 } }
]
}
}
}Range filter (price > 5000) combined with should:
GET http://127.0.0.1:9200/shopping/_search {
"query": {
"bool": {
"should": [
{ "match": { "category": "小米" } },
{ "match": { "price": 1999.00 } }
],
"filter": {
"range": { "price": { "gt": 5000 } }
}
}
}
}Match phrase (exact phrase) query:
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_phrase": { "category": "小华" } }
}Highlighting results:
GET http://127.0.0.1:9200/shopping/_search {
"query": { "match_phrase": { "category": "小华" } },
"highlight": { "fields": { "category": {} } }
}Aggregations – terms aggregation on price:
GET http://127.0.0.1:9200/shopping/_search {
"aggs": { "price_group": { "terms": { "field": "price" } } }
}Aggregations with size 0 to return only stats:
GET http://127.0.0.1:9200/shopping/_search {
"aggs": { "price_group": { "terms": { "field": "price" } } },
"size": 0
}Average price aggregation:
GET http://127.0.0.1:9200/shopping/_search {
"aggs": { "price_avg": { "avg": { "field": "price" } } },
"size": 0
}Mapping operations
Create a mapping for a user index:
PUT http://127.0.0.1:9200/user/_mapping {
"properties": {
"name": { "type": "text", "index": true },
"sex": { "type": "keyword", "index": true },
"tel": { "type": "keyword", "index": false }
}
}Retrieve the mapping:
GET http://127.0.0.1:9200/user/_mappingInsert a document into the user index:
PUT http://127.0.0.1:9200/user/_create/1001 {
"name": "小米",
"sex": "男的",
"tel": "10010"
}Search the user index by name (partial match):
GET http://127.0.0.1:9200/user/_search {
"query": { "match": { "name": "小" } }
}Search by exact sex value (must match full keyword):
GET http://127.0.0.1:9200/user/_search {
"query": { "match": { "sex": "男的" } }
}Note: fields indexed as keyword require exact matches; tel is not indexed and cannot be searched.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.