es基本命令
Elasticsearch7.6 中文文档 (opens new window)
# 1、案例数据
# 1、单独添加
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/accounts/person/1' -d '{"user": "marray", "title": "doctor", "desc": "I’m a doctor"}'
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/accounts/person/2' -d '{"user": "tom", "title": "teacher", "desc": "I’m a teacher"}'
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/accounts/person/3' -d '{"user": "jack", "title": "teacher", "desc": "I’m a teacher"}'
# 2、多条记录添加并指定id,注意每个json需要换行,包括最后一行
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/accounts/person/_bulk?pretty' -d '
{"create": {"_id": 1}}
{"user": "marray", "title": "doctor", "desc": "I’m a doctor"}
{"create": {"_id": 2}}
{"user": "tom", "title": "teacher", "desc": "I’m a teacher"}
{"create": {"_id": 3}}
{"user": "jack", "title": "teacher", "desc": "I’m a teacher"}
'
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 2、基本命令
# 查看 es 基本信息
curl 'localhost:9200'
1
# 查看当前节点的所有 Index
curl -X GET 'http://localhost:9200/_cat/indices?v'
1
# 列出每个 Index 所包含的 Type
curl 'localhost:9200/_mapping?pretty=true'
1
# 新建 index
curl -X PUT 'localhost:9200/weather'
1
# 删除 index
curl -X DELETE 'localhost:9200/weather'
1
# 新增一条数据
# 在没有创建 index 的时候新增数据会自动创建 index
curl -H 'Content-Type: application/json' -X POST 'localhost:9200/accounts/person' -d '
{"user": "tom", "title": "teacher"}'
1
2
3
2
3
# 使用 PUT 新增/更新数据
# 需要指定 id,id 不一定是数字,可以是随意的字符串
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/accounts/person/1' -d '
{"user": "tom", "title": "teacher"}'
1
2
3
2
3
# 使用 id 查看记录
curl 'localhost:9200/accounts/person/1?pretty=true'
1
# 删除记录
curl -X DELETE 'localhost:9200/accounts/person/1'
1
# 查询所有记录
curl 'localhost:9200/accounts/person/_search'
1
# 排序
# 该案例可能报错,但是语法没问题
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty' -d '
{"query": {"match_all": {}}, "sort": [{"user": "desc"}]}'
1
2
3
2
3
# 使用 json 文件插入数据
# 注意 json 文件最后一行需要数据需要换行(最后留一行空行),@后面是绝对路径
curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@/Users/yangxu/Desktop/es/accounts.json"
1
2
2
# 查看数据条数
curl 'localhost:9200/[index]/_count'
1
# 查看 es 健康状况
curl 'localhost:9200/_cluster/health?pretty'
1
上次更新: 2023/08/08, 20:00:46