快速检查集群的健康状态
GET _cat/health?v ----(v)显示列头
>>>epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1550196914 10:15:14 elasticsearch yellow 1 1 6 6 0 0 6 0 - 50.0%
green: 每个索引的primary shard和replica shard都是active状态
yellow: 每个索引的primary shard都是active状态,但是部分replica shard不是active状态,处于不可用的状态
red: 不是所有索引的primary shard都是active状态,部分索引有数据丢失
为什么现在处于yellow状态?
现在我就用一台电脑,就启动了一个ES进程,相当于就只有一个node,现在ES中只有一个index,就是kibana自己内置建立的index。由于默认的配置是给每个index分配5个primary shard和5个replice shard,而且primary shard和replice shard
不能在同一台机器上(为了容错)。现在kibana自己建立的index是1个primary shard和1个replice shard。当前就1个node,所有1个primary shard被分配了和启动,但是replice shard没有第二台机器去启动。
做个实验:此时只要启动第二个ES进程,就会在ES集群的中有2个node,然后1个replice shard就会自动分配过去,然后cluster status 为green
快速查询集群中有哪些索引
GET _cat/indices?v
>>>health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana KmDQFAVQTUKCGVI32yKSGQ 1 1 1 0 3.2kb 3.2kb health status---当前健康状态 index---索引
pri---primary shard
rep---replica shard
docs.count---document文档 条数
store.size---存储大小
创建索引
创建名为test_index索引
PUT /test_index?pretty
比如新增商品
PUT /index/type/id
PUT /goods/produce/1
{
"name": "高露洁牙膏",
"desc": "高露洁美白,口气清新,防蛀牙",
"price": 30,
"producer": "高露洁产品",
"tags": ["美白", "防蛀"],
}
PUT /goods/produce/2
{
"name": "佳洁士牙膏",
"desc": "佳洁士,防蛀牙",
"price": 30,
"producer": "佳洁士产品",
"tags": ["防蛀"],
}
>>>
{
"_index": "goods", # 索引
"_type": "produce", # 类型
"_id": "1",
"_version": 1, # 版本号(涉及乐观锁)
"result": "created", #创建
"_shards": {
"total": 2, --pri、rep要写2个
"successful": 1, 只有pri成功1个
"failed": 0
},
"created": true
}
ES会自动建立index和type,不需要提前创建,而且ES会默认对document每个field都建立倒排索引,让其可以被搜索
查询
GET /index/type/id # 查询商品信息 GET /goods/produce/1
>>>
{
"_index": "goods",
"_type": "produce",
"_id": "1",
"_version": 2,
"found": true, # 是否找到
"_source": {
"name": "高露洁牙膏",
"desc": "高露洁美白,口气清新,防蛀牙",
"price": 30,
"producer": "高露洁产品",
"tags": [
"美白",
"防蛀"
]
}
}
更新
# 修改name
方法1: 替换
PUT /goods/produce/1 { "name": "加强版高露洁牙膏", "desc": "高露洁美白,口气清新,防蛀牙", "price": 30, "producer": "高露洁产品", "tags": ["美白", "防蛀"], }
>>>
{
"_index": "goods",
"_type": "produce",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
替换方法有1个不好,就是要带上所有field,才能去进行信息的修改
方法2:
POST /goods/produce/1/_update
{
"doc": {
"name": "高露洁牙膏"
}
}
>>>
{
"_index": "goods",
"_type": "produce",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
删除索引
删除test_index索引
DELETE /test_index?pretty
删除商品
DELETE /goods/produce/1
>>>
{
"found": true,
"_index": "goods",
"_type": "produce",
"_id": "4",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}