Elasticsearch(v8.5) 常用操作大全

我直接给你一套 Elasticsearch 8.5.0 最常用、最实用、你项目 100% 会用到的命令大全

全部可以在 Kibana → Dev Tools 里直接运行!

🚀 Elasticsearch 常用操作大全


1️⃣ 查看集群/索引信息

http 复制代码
# 查看所有索引
GET /_cat/indices?v

# 查看集群健康状态
GET /_cluster/health

# 查看节点信息
GET /_cat/nodes?v

# 查看所有插件(看IK分词器)
GET /_cat/plugins?v

2️⃣ 分词测试(IK 中文分词)

http 复制代码
# 标准分词(英文分词,不适合中文)
POST /_analyze
{
  "analyzer": "standard",
  "text": "我是中国人"
}

# IK智能分词(推荐)
POST /_analyze
{
  "analyzer": "ik_smart",
  "text": "我是中国人"
}

# IK最大分词(细粒度,适合搜索)
POST /_analyze
{
  "analyzer": "ik_max_word",
  "text": "我是中国人"
}

3️⃣ 创建索引(带中文分词)

http 复制代码
PUT /album_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "id": { "type": "long" },
      "title": { 
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "author": { 
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "intro": { 
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "createTime": { "type": "date" }
    }
  }
}

4️⃣ 新增/修改文档

http 复制代码
# 新增文档(自动生成ID)
POST /album_index/_doc
{
  "title": "三体",
  "author": "刘慈欣",
  "intro": "科幻神作",
  "createTime": "2025-01-01"
}

# 新增/覆盖文档(指定ID)
PUT /album_index/_doc/1
{
  "title": "三体",
  "author": "刘慈欣",
  "intro": "科幻神作"
}

5️⃣ 查询数据(最常用)

http 复制代码
# 根据ID查询
GET /album_index/_doc/1

# 查询所有数据
GET /album_index/_search
{
  "query": {
    "match_all": {}
  }
}

# 中文关键词搜索(核心!)
GET /album_index/_search
{
  "query": {
    "match": {
      "title": "三体"
    }
  }
}

# 多字段搜索(标题+简介)
GET /album_index/_search
{
  "query": {
    "multi_match": {
      "query": "三体",
      "fields": ["title","intro"]
    }
  }
}

# 精准匹配(不分词)
GET /album_index/_search
{
  "query": {
    "term": {
      "author.keyword": "刘慈欣"
    }
  }
}

6️⃣ 修改/删除数据

http 复制代码
# 局部更新
POST /album_index/_update/1
{
  "doc": {
    "intro": "最新简介"
  }
}

# 删除文档
DELETE /album_index/_doc/1

# 删除整个索引
DELETE /album_index

7️⃣ 高级搜索(排序、分页、高亮)

http 复制代码
GET /album_index/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "match": { "title": "三体" }
  },
  "sort": [
    { "createTime": { "order": "desc" } }
  ],
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

8️⃣ 查看索引结构/配置

http 复制代码
# 查看索引结构
GET /album_index/_mapping

# 查看索引设置
GET /album_index/_settings

索引核心操作

http 复制代码
PUT /album_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "id": { "type": "long" },
      "title": { 
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "author": { 
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "intro": { 
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "createTime": { "type": "date" }
    }
  }
}

🚀 创建完索引后,一共就 3 个核心用法

1️⃣ 往里面添加数据(存专辑)

http 复制代码
POST /album_index/_doc/1
{
  "id": 1,
  "title": "三体",
  "author": "刘慈欣",
  "intro": "科幻神作,讲述人类和外星文明的故事",
  "createTime": "2025-01-01"
}
http 复制代码
POST /album_index/_doc/2
{
  "id": 2,
  "title": "雪中悍刀行",
  "author": "烽火戏诸侯",
  "intro": "经典玄幻武侠,有声书超火",
  "createTime": "2025-01-02"
}

2️⃣ 搜索数据

搜标题里包含 "三体"

http 复制代码
GET /album_index/_search
{
  "query": {
    "match": {
      "title": "三体"
    }
  }
}

搜标题 + 简介都包含 "科幻"

http 复制代码
GET /album_index/_search
{
  "query": {
    "multi_match": {
      "query": "科幻",
      "fields": ["title", "intro"]
    }
  }
}

搜作者 "刘慈欣"

http 复制代码
GET /album_index/_search
{
  "query": {
    "match": {
      "author": "刘慈欣"
    }
  }
}

3️⃣ 修改 / 删除数据

修改专辑简介

http 复制代码
POST /album_index/_update/1
{
  "doc": {
    "intro": "这是修改后的简介"
  }
}

删除一条数据

http 复制代码
DELETE /album_index/_doc/1

相关推荐
Databend19 分钟前
Jev 爆火之后,我们把它集成进了数据湖仓
大数据·数据库·agent
拾光师22 分钟前
MapReduce Shuffle 深度解析:数据从 Map 到 Reduce 的完整旅程
大数据
小小程序猴123 分钟前
中小企业AI转型课程模块的工程化设计:从能力矩阵到课程编排
大数据·人工智能
Aloudata25 分钟前
语义层和SQL Copilot:一个降低写SQL门槛,一个统一业务逻辑
大数据·人工智能·数据分析·data agent·语义层
不吃香菜kkk、26 分钟前
CI/CD(GitOps)学习与部署手册
运维·云原生·容器·kubernetes·云计算·jenkins·argocd
IT研究室1 小时前
最新大数据毕业设计选题推荐-基于大数据的信用等级数据分析与可视化-大数据-Spark-Hadoop-Bigdata
大数据·信息可视化·数据分析·spark·课程设计
不会写代码的女程序猿1 小时前
商用 AI 四诊仪技术拆解|明理 AI 四诊仪多模态采集方案解析
大数据·人工智能·科技·ai·健康医疗
cspttty2 小时前
需求预测校招入门:SQL、Excel、统计模型和业务指标怎么学
大数据·数据库
YangYang9YangYan2 小时前
财务 BP 校招面试案例整理|业务场景题 + 数据案例,2027 届求职
大数据·数据库
Rosanci11 小时前
谷歌浏览器插件开发实战指南:从 Hello World 到上架发布
大数据·人工智能·chrome·程序人生