1.简单管理
(1)检查ES是否启动成功:http://localhost:9200/?pretty
ES的默认端口是9200返回一下信息说明启动成功
{
"name" : "w5RX1Ra", //node节点名称,随机生成的
"cluster_name" : "elasticsearch", //集群名称,默认名称
"cluster_uuid" : "Is1NOYWFS_i7MxPijyPoDg", //集群随机生成uuid
"version" : {
"number" : "5.2.0", //es的版本号
"build_hash" : "24e05b9",
"build_date" : "2017-01-24T19:52:35.800Z",
"build_snapshot" : false,
"lucene_version" : "6.4.0" //内嵌lucene的版本
},
"tagline" : "You Know, for Search"
}
(2)快速检查集群的健康状况
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
1488006741 15:12:21 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%
green:每个索引的primary shard和replica shard都是active状态的
yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是
active状态,处于不可用的状态
red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了
(3)快速查看集群中有哪些索引
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana XL1b_ekDTtGDmSkSWkMc-A 1 1 1 0 3.1kb 3.1kb
yellow open test_index3 _tT8GoOhSVO3yDqTlwbESw 1 3 2 0 7.4kb 7.4kb
(4) 简单的索引操作
创建索引:PUT /test_index?pretty
删除索引:DELETE /test_index?pretty
(5)简单的CRUD操作
新增文档,建立索引 es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索
PUT /product/electronic/1
{
"name" : "huawei shouji",
"desc" : "gaoqing pianyi",
"price" : 3000
}
结果
{
"_index": "test_index", //索引名
"_type": "test_type", // 所属类型
"_id": "1", //id号
"_version": 1, //版本号
"result": "created", // 返回创建结果
"_shards": {
"total": 2, //shart的数量
"successful": 1, //成的个数
"failed": 0 //失败0个
},
"created": true //创建成功
}
(6)检索文档
GET /product/electronic/1
结构
{
"_index": "product",
"_type": "electronic",
"_id": "1",
"_version": 1,
"found": true, //是否查找到数据
"_source": { //文档内容
"name": "huawei shouji",
"desc": "gaoqing pianyi",
"price": 3000
}
}
(7)替换文档
如果使用PUT将会覆盖原来数据,如果更新需要将所有的字段都重新
PUT /product/electronic/1
{
"name" : "huawei shouji",
"desc" : "gaoqing pianyi",
"price" : 3000
}
(8)更新文档 只用写更新的字段就好
POST /product/electronic/1
{
"price" : 30
}
{
"_index": "product",
"_type": "electronic",
"_id": "1",
"_version": 5,
"result": "updated", //如果文档存在就更新,没有则创建
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
(9)删除文档
DELETE /product/electronic/1
结果
{
"found": true,
"_index": "product",
"_type": "electronic",
"_id": "1",
"_version": 6,
"result": "deleted", //删除
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}