目前为了快速的学习ES我们可以在window环境下面搭建ES和kibana
es提供了一套api,叫做cat api,可以查看es中各种各样的数据
健康检查
GET /_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1536768182 00:03:02 elasticsearch green 1 1 1 1 0 0 0 0 - 100.0%
green:每个索引的primary shard和replica shard都是active状态的
yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了
快速查看集群中有哪些索引
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana XtwBcBhyQxKo8eaDeggqPA 1 0 1 0 4kb 4kb
简单的索引操作
PUT /test_index?pretty
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana XtwBcBhyQxKo8eaDeggqPA 1 0 1 0 4kb 4kb
yellow open test_index K2ZkCnssRx-GbBpcTQ8oAA 5 1 0 0 1.1kb 1.1kb
删除索引
DELETE /test_index?pretty
{
"acknowledged": true
}
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana XtwBcBhyQxKo8eaDeggqPA 1 0 1 0 4kb 4kb
ES的CRUD操作
1、新增用户:新增文档,建立索引
es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索
PUT /index/type/id
{
"json数据"
}
新增加3条记录
PUT /employee/user/1
{
"name" : "zhangsan",
"sez" : "F",
"china" : 80,
"addr" : "hunan",
"join" : ["beijing","shanghai","wuhan"]
}
PUT /employee/user/2
{
"name" : "lisi",
"sez" : "M",
"china" : 90,
"addr" : "beijing",
"join" : ["chengdu","shenzhen","wuhan"]
}
PUT /employee/user/3
{
"name" : "wangwu",
"sez" : "M",
"china" : 60,
"addr" : "xian",
"join" : ["xiamen","nanning","changsha"]
}
返回的结果
{
"_index": "employee",
"_type": "user",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询用户信息
GET /index/type/id
GET /employee/user/1
{
"_index": "employee",
"_type": "user",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "zhangsan",
"sez": "F",
"china": 80,
"addr": "hunan",
"join": [
"beijing",
"shanghai",
"wuhan"
]
}
}
修改用户信息
PUT /index/type/id
必须带上用户所有的field信息
PUT /employee/user/1
{
"name" : "zhangsan",
"sez" : "F",
"china" : 88,
"addr" : "hunan",
"join" : ["beijing","shanghai","wuhan"]
}
返回的结果
{
"_index": "employee",
"_type": "user",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
修改用户,更新文档
POST /ecommerce/product/1/_update
POST /employee/user/1/_update
{
"doc":{
"join" : ["beijing","shanghai","wuhan"]
}
}
{
"_index": "employee",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "zhangsan",
"sez": "F",
"china": 88,
"addr": "hunan",
"join": [
"beijing",
"shanghai",
"wuhan"
]
}
}
删除用户:
DELETE /idnex/type/id
DELETE /employee/user/1
{
"_index": "employee",
"_type": "user",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
GET /employee/user/1
{
"_index": "employee",
"_type": "user",
"_id": "1",
"found": false
}