当前位置: 首页 > news >正文

Elasticsearch 常用操作命令整合 (cURL 版本)

Elasticsearch 常用操作命令整合 (cURL 版本)

集群管理

查看集群健康状态

curl -X GET "localhost:9200/_cluster/health?pretty"

查看节点信息

curl -X GET "localhost:9200/_cat/nodes?v"

查看集群统计信息

curl -X GET "localhost:9200/_cluster/stats?human&pretty"

索引操作

创建索引

curl -X PUT "localhost:9200/index_name" -H 'Content-Type: application/json' -d'
{"settings": {"number_of_shards": 3,"number_of_replicas": 1},"mappings": {"properties": {"field1": { "type": "text" },"field2": { "type": "keyword" }}}
}
'

查看所有索引

curl -X GET "localhost:9200/_cat/indices?v"

查看特定索引信息

curl -X GET "localhost:9200/index_name?pretty"

删除索引

curl -X DELETE "localhost:9200/index_name"

关闭/打开索引

curl -X POST "localhost:9200/index_name/_close"
curl -X POST "localhost:9200/index_name/_open"

文档操作

索引/创建文档

curl -X POST "localhost:9200/index_name/_doc" -H 'Content-Type: application/json' -d'
{"field1": "value1","field2": "value2"
}
'curl -X PUT "localhost:9200/index_name/_doc/1" -H 'Content-Type: application/json' -d'
{"field1": "value1","field2": "value2"
}
'

获取文档

curl -X GET "localhost:9200/index_name/_doc/1?pretty"

更新文档

curl -X POST "localhost:9200/index_name/_update/1" -H 'Content-Type: application/json' -d'
{"doc": {"field1": "new_value"}
}
'

删除文档

curl -X DELETE "localhost:9200/index_name/_doc/1"

批量操作

curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "index_name", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "index_name", "_id" : "2" } }
{ "create" : { "_index" : "index_name", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "index_name"} }
{ "doc" : {"field2" : "value2"} }
'

搜索操作

简单搜索

curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{"query": {"match": {"field1": "search_term"}}
}
'

复合查询

curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{"query": {"bool": {"must": [{ "match": { "field1": "value1" } },{ "range": { "field2": { "gte": 10 } } }]}}
}
'

聚合查询

curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{"aggs": {"agg_name": {"terms": { "field": "field2" }}}
}
'

索引别名

创建别名

curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{"actions": [{"add": {"index": "index_name","alias": "alias_name"}}]
}
'

切换别名

curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{"actions": [{ "remove": { "index": "old_index", "alias": "alias_name" } },{ "add": { "index": "new_index", "alias": "alias_name" } }]
}
'

索引模板

创建索引模板

curl -X PUT "localhost:9200/_template/template_name" -H 'Content-Type: application/json' -d'
{"index_patterns": ["pattern*"],"settings": {"number_of_shards": 1},"mappings": {"properties": {"field1": { "type": "text" }}}
}
'

数据迁移

使用_reindex API

curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{"source": {"index": "old_index"},"dest": {"index": "new_index"}
}
'

常用_cat API

查看分片分配

curl -X GET "localhost:9200/_cat/shards?v"

查看任务

curl -X GET "localhost:9200/_cat/tasks?v"

查看插件

curl -X GET "localhost:9200/_cat/plugins?v"

查看线程池

curl -X GET "localhost:9200/_cat/thread_pool?v"

认证相关 (如果启用了安全功能)

带基本认证的请求

curl -u username:password -X GET "localhost:9200/_cluster/health?pretty"

带API密钥的请求

curl -H "Authorization: ApiKey your_api_key" -X GET "localhost:9200/_cluster/health?pretty"

注意:

  1. 默认使用localhost:9200,如果ES运行在其他主机或端口,请相应修改
  2. 如果启用了安全功能,需要添加认证信息
  3. 对于生产环境,建议使用HTTPS协议

相关文章:

  • 鸿蒙API自翻译
  • 从碳基羊驼到硅基LLaMA:开源大模型家族的生物隐喻与技术进化全景
  • Ollama部署下载Qwen3-Embedding(含0.6B、4B、8B等)向量模型和Qwen3-Reranker(含0.6B、4B、8B等)重排模型的方法
  • 【RAG召回】BM25算法示例
  • 智慧城市项目总体建设方案(Word700页+)
  • 楠溪江诗意传承:李文照笔下的山水印记
  • leetcode_56 合并区间
  • 十一.C++ 类 -- 面向对象思想
  • day50 随机函数与广播机制
  • 【西门子杯工业嵌入式-6-ADC采样基础】
  • CMake基础:gcc/g++编译选项详解
  • 结合三维基因建模与智能体技术打造工业软件无码平台
  • 2025-06-08-深度学习网络介绍(语义分割,实例分割,目标检测)
  • 什么是 Ansible 主机和组变量
  • 【数据结构】顺序表和链表详解(下)
  • C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
  • FBRT-YOLO:面向实时航拍图像检测的轻量高效目标检测框架
  • 股指期货技术分析与短线操作方法介绍
  • 从C到C++语法过度1
  • windows安装Nexus3.6
  • 网站首页图片大小/网站创建的流程是什么
  • 泰和网站制作/seo提升排名
  • 自己做一个网站一年的费用/推广方案如何写
  • 微信公众号网站建设/银川seo
  • 82端口做网站/个人网上卖货的平台
  • 有帮忙做儿童房设计的网站吗/今日网站收录查询