一.知识回顾
二.ElasticSearch入门
2.1ElasticSearch中_cat接口
_cat接口 | 说明 |
_cat/nodes | 查看所有节点 |
/_cat/health | 查看ES健康状况 |
/_cat/master | 查看主节点 |
/_cat/indices | 查看所有索引信息 |
通过postman来测试查看索引信息—》http://ip:9200/_cat/indices?v 查看所有的索引信息
es 中会默认提供上面的几个索引,表头的含义为:
字段名 | 含义说明 |
health | green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 是否能使用 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主节点几个 |
rep | 从节点几个 |
docs.count | 文档数 |
docs.deleted | 文档被删了多少 |
store.size | 整体占空间大小 |
pri.store.size | 主节点占 |
2.2 索引操作
索引就相当于我们讲的关系型数据库MySQL中的 database
2.2.1 创建索引
PUT /索引名
参数需要设置为Json数据格式,可选:指定分片及副本,默认分片为3,副本为2。
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
2.2.2 查看索引
查看指定索引
GET /索引名
查询所有索引具体信息
2.2.2 删除索引
DELETE /索引名称
删除后查询看是否存在
2.3文档操作
文档相当于数据库中的表结构中的Row记录
2.3.1 创建文档
PUT /索引名称/类型名/编号
保存的json数据
{
"name" : "ljw",
"age" :18
}
提交方式 | 描述 |
PUT | 提交的id如果不存在就是新增操作,如果存在就是更新操作,id不能为空 |
POST | 如果不提供id会自动生成一个id,如果id存在就更新,如果id不存在就新增 |
POST /索引名称/类型名/编号
如果当前文档的id之前是存在的,那么当前文档的状态就是updated
2.3.2 查询文档
GET /索引/类型/id
返回字段的含义
字段 | 含义 |
_index | 索引名称 |
_type | 类型名称 |
_id | 记录id |
_version | 版本号 |
_seq_no | 并发控制字段,每次更新都会+1,用来实现乐观锁 |
_primary_term | 同上,主分片重新分配,如重启,就会发生变化 |
found | 找到结果 |
_source | 真正的数据内容 |
接下里模拟测试一个乐观锁实现场景:
先来查询一下上一次增加完数据乐观锁维护的值到哪里了
通过查询我们知道此时 ?if_seq_no=5&if_primary_term=1
此时我们通过两个相同的乐观锁请求模拟测试
测试1:
测试2:
2.3.3 更新文档
更新文档数据的俩个方式:
- POST和PUT添加数据的时候,如果id存在就会执行更新文档的操作;
- 我们可以通过POST方式提交,然后显示的跟上_update来实现更新
POST /索引/类型/id/_update
{
"doc":{
"name":"shoufenhewei"
}
}
这种方式来更新,只是这种方式的更新如果数据没有变化则不会操作。
如果更新的数据和文档中的数据是一样的,那么POST方式提交是不会有任何操作的
2.3.4 删除文档
DELETE /索引/类型/id
DELETE /索引
三.Kibanan导入elasticsearch官方测试数据
3.1 Kibanan中执行批量操作1
_bulk批量操作,语法格式
{action:{metadata}}\n
{request body }\n
{action:{metadata}}\n
{request body }\n
案例
POST /ljw/system/_bulk
{"index":{"_id":"1"}}
{"name":"ljw1"}
{"index":{"_id":"2"}}
{"name":"ljw2"}
执行批量插入成功
3.2 Kibanan中导入官方测试数据
先去官方下载数据:
官方测试数据:https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json
然后在Kibanan中执行
好了,关于【3-ElasticSearch入门-索引的创建删除更新查询-文档的创建删除更新查询-Kibanan导入elasticsearch官方测试数据-postman测试】就先学习到这里,更多的内容,后续持续创作更新中。