@nalan90
2016-12-09T23:14:17.000000Z
字数 3977
阅读 518
ElasticSearch
Cluster Health
#Request
GET /_cat/health?v
#Response
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1481276500 09:41:40 elasticsearch yellow 1 1 11 11 0 0 11 0 - 50.0%
#Request
GET /_cat/nodes?v
#Response
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.17.0.2 7 89 0 0.08 0.04 0.01 mdi * kx4nTUI
#Request
GET /_cat/indices?v
#Response
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana OKHkcJ1HSUylJM5MSCX3lA 1 1 1 0 3.1kb 3.1kb
yellow open bank TPSuBFZVRlKW61o5JMuk8A 5 1 1000 0 640.8kb 640.8kb
Create an Index
#Request
PUT /customer?pretty
#Response
{
"acknowledged": true,
"shards_acknowledged": true
}
Index and Query a Document
# Request
PUT /customer/external/1?pretty
{
"name": "John Doe"
}
#Response
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
#Request
GET /customer/external/1?pretty
#Response
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "John Doe"
}
}
#Request
GET /customer/external/2?pretty
#Response 未查询到任何数据
{
"_index": "customer",
"_type": "external",
"_id": "2",
"found": false
}
Delete an Index
#Request
DELETE /customer?pretty
#Response
{
"acknowledged": true
}
PUT /customer
PUT /customer/external/1
{
"name": "John Doe"
}
GET /customer/external/1
DELETE /customer
<REST Verb> /<Index>/<Type>/<ID
Modifying Your Data
GET /customer/external/1?pretty
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "John Doe"
}
}
#Request
PUT /customer/external/1?pretty
{
"name": "John Doe"
}
#Response
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
版本号变更
#Request
POST /customer/external?pretty
{
"name": "Jane Doe"
}
#Response
{
"_index": "customer",
"_type": "external",
"_id": "AVjkGopJkGmB7tHuUrBy",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
Updating Documents
POST /customer/external/1/_update?pretty
{
"doc": { "name": "Jane Doe" }
}
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 6,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
POST /customer/external/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
GET /customer/external/1?pretty
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 7,
"found": true,
"_source": {
"name": "Jane Doe",
"age": 20
}
}
#年龄+5
POST /customer/external/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
GET /customer/external/1?pretty
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"found": true,
"_source": {
"name": "Jane Doe",
"age": 25
}
}
Deleting Documents
#Request
DELETE /customer/external/1?pretty
#Response
{
"found": true,
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
Batch Processing
#Request
POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
#Response
{
"took": 40,
"errors": false,
"items": [
{
"index": {
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
},
{
"index": {
"_index": "customer",
"_type": "external",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
}
]
}
#Request
POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
#Response
{
"took": 33,
"errors": false,
"items": [
{
"update": {
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
},
{
"delete": {
"found": true,
"_index": "customer",
"_type": "external",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}