Databases 8 min read

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.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Elasticsearch Index and Document Operations Tutorial

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/shopping

Get an index:

GET http://127.0.0.1:9200/shopping

List all indices:

GET http://127.0.0.1:9200/_cat/indices?v

Delete an index:

DELETE http://127.0.0.1:9200/shopping

Document operations

Create a document (auto‑generated ID):

POST http://127.0.0.1:9200/shopping/_doc

Create a document with a custom ID:

POST http://127.0.0.1:9200/shopping/_doc/1001

Put (upsert) a document with a specific ID (idempotent):

PUT http://127.0.0.1:9200/shopping/_doc/1001

Get a document by ID:

GET http://127.0.0.1:9200/shopping/_doc/1001

Search all documents:

GET http://127.0.0.1:9200/shopping/_search

Update 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/1001

Query 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/_mapping

Insert 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.

big dataIndexingsearch engineElasticsearchJSONREST APIQuery DSL
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login 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.