目录结构
- 前言
- 数据库初始化
- 索引操作
- 创建索引
- 获取索引
- 获取所有索引
- 删除索引
- 数据操作
- 新增
- POST方式
- PUT方式
- 查询
- 主键查询
- 全量查询search
- 修改
- 全量覆盖
- 部分修改
- 删除
前言
- Elasticsearch安装成功情况下;
- 使用Postman请求操作数据库;
- 浏览器插件实现Elasticsearch可视化;
- 文章中的表格是Elasticsearch元素和mysql元素的对应关系,以便理解;
数据库初始化
索引操作
创建索引
- 请求方式:PUT
- URL:http://localhost:9200/daocaoren
- 回执数据
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "1111"
}
- Postman请求截图如下:
- 浏览器可视化截图如下,为方便后续测试创建了另外两个索引(duijiaoxiang和fudimo)
获取索引
- 请求方法:GET
- URL:http://localhost:9200/daocaoren
- 回执数据
{
"daocaoren": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1686127099199",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "DvvrASslSyG7YdUByApZAQ",
"version": {
"created": "6010099"
},
"provided_name": "daocaoren"
}
}
}
}
- Postman请求截图如下:
获取所有索引
- 请求方法:GET
- URL:http://localhost:9200/_cat/indices?v
- “?v”:可将回执数据格式化,也可不带;
- 回执数据
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open duijiaoxiang VDlwp5RGQi23G_Jj0J5pIQ 5 1 0 0 1.1kb 1.1kb
yellow open daocaoren DvvrASslSyG7YdUByApZAQ 5 1 0 0 1.1kb 1.1kb
yellow open fudimo 14rW7rhATa2H0OTRUrdElQ 5 1 0 0 1.1kb 1.1kb
- Postman请求截图如下:
- 携带“?v”
- 不携带“?v”
- 携带“?v”
删除索引
- 请求方法:DELETE
- URL:http://localhost:9200/fudimo
- 回执数据
{
"acknowledged": true
}
- Postman请求截图如下:
- 浏览器可视化截图如下,“fudimo”索引已经删除;
数据操作
新增
POST方式
- URL
- 数据id自生成:http://localhost:9200/daocaoren/doc
- 自定义数据id:http://localhost:9200/daocaoren/doc/100
- 请求参数:
{
"name":"哈利波特",
"age":16
}
- 回执参数
{
"_index": "daocaoren",
"_type": "doc",
"_id": "Xj0elYgB-DN7ym1vn-5f",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
- Postman请求截图如下:
- 浏览器可视化截图如下
PUT方式
PUT请求方式后面必须跟id才能新增成功,并且新增多次都会成功!
- URL:http://localhost:9200/daocaoren/doc/200
- 请求参数:
{
"name":"邓布利多",
"age":16
}
- 回执参数
{
"_index": "daocaoren",
"_type": "doc",
"_id": "200",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
- Postman请求截图如下:
- 浏览器可视化截图如下
查询
主键查询
- 请求方法:GET
- URL:http://localhost:9200/daocaoren/doc/400
- 回执数据
{
"_index": "daocaoren",
"_type": "doc",
"_id": "400",
"_version": 2,
"found": true,
"_source": {
"name": "邓布利多",
"age": 16
}
}
- Postman请求截图如下:
全量查询search
- 请求方法:GET
- URL:http://localhost:9200/daocaoren/doc/_search
- 回执数据
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.0,
"hits": [
{
"_index": "daocaoren",
"_type": "doc",
"_id": "400",
"_score": 1.0,
"_source": {
"name": "邓布利多",
"age": 16
}
},
{
"_index": "daocaoren",
"_type": "doc",
"_id": "100",
"_score": 1.0,
"_source": {
"name": "哈利波特",
"age": 16
}
},
{
"_index": "daocaoren",
"_type": "doc",
"_id": "200",
"_score": 1.0,
"_source": {
"name": "邓布利多",
"age": 16
}
},
{
"_index": "daocaoren",
"_type": "doc",
"_id": "Xj0elYgB-DN7ym1vn-5f",
"_score": 1.0,
"_source": {
"name": "哈利波特",
"age": 16
}
}
]
}
}
- Postman请求截图如下:
修改
全量覆盖
- 请求方法:PUT
- URL:http://localhost:9200/fudimo
** 其实和新增那里的put一样,类似mysql里的
部分修改
- 请求方法:POST
- URL:http://localhost:9200/daocaoren/doc/400
- 请求数据
{
"name":"邓布利多666",
"age":88
}
- 回执数据
{
"_index": "daocaoren",
"_type": "doc",
"_id": "400",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
- Postman请求截图如下:
- 浏览器可视化截图如下
删除
- 请求方法:DELETE
- URL:http://localhost:9200/daocaoren/doc/400
- 回执数据
{
"_index": "daocaoren",
"_type": "doc",
"_id": "400",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
- Postman请求截图如下:
- 浏览器可视化截图如下