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

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

集群管理

查看集群健康状态

bash 复制代码
curl -X GET "localhost:9200/_cluster/health?pretty"

查看节点信息

bash 复制代码
curl -X GET "localhost:9200/_cat/nodes?v"

查看集群统计信息

bash 复制代码
curl -X GET "localhost:9200/_cluster/stats?human&pretty"

索引操作

创建索引

bash 复制代码
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" }
    }
  }
}
'

查看所有索引

bash 复制代码
curl -X GET "localhost:9200/_cat/indices?v"

查看特定索引信息

bash 复制代码
curl -X GET "localhost:9200/index_name?pretty"

删除索引

bash 复制代码
curl -X DELETE "localhost:9200/index_name"

关闭/打开索引

bash 复制代码
curl -X POST "localhost:9200/index_name/_close"
curl -X POST "localhost:9200/index_name/_open"

文档操作

索引/创建文档

bash 复制代码
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"
}
'

获取文档

bash 复制代码
curl -X GET "localhost:9200/index_name/_doc/1?pretty"

更新文档

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

删除文档

bash 复制代码
curl -X DELETE "localhost:9200/index_name/_doc/1"

批量操作

bash 复制代码
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"} }
'

搜索操作

简单搜索

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

复合查询

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

聚合查询

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

索引别名

创建别名

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

切换别名

bash 复制代码
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" } }
  ]
}
'

索引模板

创建索引模板

bash 复制代码
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

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

常用_cat API

查看分片分配

bash 复制代码
curl -X GET "localhost:9200/_cat/shards?v"

查看任务

bash 复制代码
curl -X GET "localhost:9200/_cat/tasks?v"

查看插件

bash 复制代码
curl -X GET "localhost:9200/_cat/plugins?v"

查看线程池

bash 复制代码
curl -X GET "localhost:9200/_cat/thread_pool?v"

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

带基本认证的请求

bash 复制代码
curl -u username:password -X GET "localhost:9200/_cluster/health?pretty"

带API密钥的请求

bash 复制代码
curl -H "Authorization: ApiKey your_api_key" -X GET "localhost:9200/_cluster/health?pretty"

注意:

  1. 默认使用localhost:9200,如果ES运行在其他主机或端口,请相应修改
  2. 如果启用了安全功能,需要添加认证信息
  3. 对于生产环境,建议使用HTTPS协议
相关推荐
byte轻骑兵30 分钟前
时序数据库选型指南:从大数据视角看IoTDB的核心优势
大数据·时序数据库·iotdb
Databend35 分钟前
如何打造AI时代的数据基石 | Databend Meetup 上海站
数据库
老华带你飞1 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·海鲜商城购物系统
Leo.yuan1 小时前
制造业都在说BOM,为什么BOM这么重要?
大数据·bom·企业数字化·数字赋能
能鈺CMS1 小时前
内容付费系统全面解析:构建知识变现体系的最强工具(2025 SEO 深度专题)
大数据·人工智能·html
合作小小程序员小小店1 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
SelectDB1 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
IvorySQL2 小时前
PostgreSQL 18 - 时间约束 (Temporal Constraints)
数据库·postgresql·开源
q***61412 小时前
从MySQL迁移到PostgreSQL的完整指南
数据库·mysql·postgresql
N***73852 小时前
SQL锁机制
java·数据库·sql