ES命令行操作

一、集群管理

集群状态查询

# 无密码访问
$ curl 'localhost:9200/_cat/health?v'
$ curl 'localhost:9200/_cat/nodes?v'
$ curl 'localhost:9200/_cat/nodes?v&h=hc,hm,rc,rm'
# 有密码访问
$ curl 'localhost:9200/_cat/health?v' --user elastic:admin@123
$ curl 'localhost:9200/_cat/nodes?v' --user elastic:admin@123
$ curl 'localhost:9200/_cat/nodes?v&h=hc,hm,rc,rm' --user elastic:admin@123

启动节点

$ bin/elasticsearch -d

关闭节点

$ curl -XPOST 'http://localhost:9200/_cluster/nodes/n2/_shutdown'
$ curl -XPOST 'http://localhost:9200/_shutdown'
$ curl -XPOST 'http://localhost:9200/_cluster/nodes/_local/_shutdown?delay=10s'

二、索引管理

$ curl 'localhost:9200/_cat/indices?v'
# 查询索引状态
$ curl -XDELETE http://localhost:9200/索引名称
{"acknowledged":true}
# 删除指定索引
$ curl -XDELETE http://localhost:9200/索引名称1,索引名称2
# 删除多个索引,中间用逗号隔开
$ curl -XDELETE -u elastic:changeme http://localhost:9200/索引名前缀*
# 模糊匹配删除
# 使用通配符,删除所有索引
$ curl -XDELETE http://localhost:9200/_all
$ curl -XDELETE http://localhost:9200/*
# 二选一都可以
# _all ,* 通配所有的索引,通常不建议使用通配符,误删了后果就很严重了,所有的index都被删除了,禁止通配符为了安全起见,可以在elasticsearch.yml配置文件中设置禁用_all和*通配符`action.destructive_requires_name = true`这样就不能使用_all和*了!

创建别名

$ curl -XPOST localhost:9200/_aliases -d '
{
"actions": [
{ "add": {
"alias": "goods",
"index": "goods_v1"
}}
]
}'

删除并更新别名

$ curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "goods_v2", "alias" : "goods" } },
{ "add" : { "index" : "goods_v1", "alias" : "goods" } }
]
}'
$ curl -XGET 'localhost:9200/_cat/aliases'
# 查看已有别名
$ curl -XGET 'localhost:9200/_alias/help'
# 查看别名对应的索引

创建mapping

curl -XPOST http://localhost:9200/goods_v1/fulltext/_mapping -d'
{
"fulltext": {
"_all": {
"indexAnalyzer": "ik",
"searchAnalyzer": "ik_syno",
"term_vector": "no",
"store": "false"
},
"properties": {
"sku_id" :{
"type": "string"
},
"product_id":{
"type": "string"
},
"product_name":{
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"indexAnalyzer": "ik",
"searchAnalyzer": "ik_syno",
"include_in_all": "true",
"boost": 8
}
}
}
}'
$ curl -XGET 'http://localhost:9200/help/_mapping/fulltext?pretty'
# 获取mapping
$ curl 'localhost:9200/_cat/shards?v'
$ curl -XGET 'http://localhost:9200/_cat/shards' | grep INIT
# 查看分区

三、文档管理

$ curl -XGET 'localhost:9200/goods/fulltext/S201406251699?pretty'
# 获取文档
$ curl -XDELETE 'localhost:9200/goods/fulltext/1?pretty'
# 删除文档

获取索引中前10个文档

curl -XPOST 'localhost:9200/goods/_search?pretty' -d '
{
"query": { "match_all": {} }
}'

四、缓存管理

创建时显式开启缓存

curl -XPUT localhost:9200/my_index -d'
{
"settings": {
"index.cache.query.enable": true
}
}'

更新设置开启缓存

curl -XPUT localhost:9200/goods/_settings -d'
{ "index.cache.query.enable": true }'
$ curl 'localhost:9200/_nodes/stats/indices/query_cache?pretty&human'
# 查询各节点缓存状态

五、索引副本

$ curl -XPUT -H 'Content-Type: application/json' 'http://192.168.100.134:9200/entbaseinfo_ceb20200619/_settings' -d'
{
"number_of_replicas": 1
}'
# 指定索引entbaseinfo_ceb20200619副本数为1

六、Es平衡机制

$ curl -XPUT -H 'Content-Type: application/json' '192.168.100.134:9200/_cluster/settings' -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
# 开启es平衡机制
$ curl -XPUT -H 'Content-Type: application/json' '192.168.100.134:9200/_cluster/settings' -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}'
# 关闭es平衡机制
posted @   吕振江  阅读(3476)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示

目录导航