Elasticsearch 基础教程:从入门到版本演进

一、Elasticsearch 核心概念

Elasticsearch(简称ES)是一款分布式全文搜索引擎,以下是核心概念:

  • 索引(Index):类似关系数据库的“数据库”,用于存储一类相关文档。本文以threads(帖子集合)作为示例索引。
  • 文档(Document):索引中的基本数据单元,以JSON格式存储,类似数据库的“行”。
  • 字段(Field):文档中的属性,类似数据库的“列”,如title(标题)、content(内容)。
  • 分片(Shard):索引的水平拆分单元,用于扩展存储和查询能力,不可修改数量。
  • 副本(Replica):分片的冗余备份,提供高可用和查询性能,可动态调整数量。

二、环境准备

1. Java 环境要求(版本差异)

ES版本 最低Java版本 推荐版本
ES6.x Java 8 Java 8
ES7.x Java 8/11 Java 11
ES8.x Java 11+ Java 17
ES9.x Java 17+ Java 17

2. 下载与启动

三、索引操作

查看Elasticsearch状态的常用命令:

  # 查看ES服务状态
  curl http://localhost:9200/_cluster/health

  # 查看所有索引
  curl http://localhost:9200/_cat/indices?v

  # 查看threads索引详情
  curl http://localhost:9200/threads

  # 查看threads索引的文档数量
  curl http://localhost:9200/threads/_count

  # 查看threads索引的映射结构
  curl http://localhost:9200/threads/_mapping

1. 创建索引

通用逻辑:定义索引的分片、副本配置及字段映射(字段类型、分词器等)。

ES6 示例(含Type)

ES6支持在索引中定义多个type(类似“表”),示例中使用项目中的App\Thread类型:

# 创建threads索引(ES6)
curl -X PUT "localhost:9200/threads" -H "Content-Type: application/json" -d '
{
  "settings": {
    "number_of_shards": 1,    # 主分片数
    "number_of_replicas": 0   # 副本数
  },
  "mappings": {
    "App\\Thread": {  # 自定义type(ES6特有)
      "properties": {
        "title": { 
          "type": "text", 
          "analyzer": "ik_max_word",  # 中文分词器
          "fields": { "keyword": { "type": "keyword" } }  # 精确匹配子字段
        },
        "user_id": { "type": "long" },  # 数值类型
        "created_at": { "type": "date", "format": "yyyy-MM-dd HH🇲🇲ss" }  # 日期类型
      }
    }
  }
}
'

ES7+ 示例(无Type)

ES7开始弃用type,ES8/9彻底移除,索引直接定义字段映射:

# 创建threads索引(ES7/8/9)
curl -u elastic:your_password -X PUT "https://localhost:9200/threads" -k -H "Content-Type: application/json" -d '
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {  # 无type层级
      "title": { 
        "type": "text", 
        "analyzer": "ik_max_word",
        "fields": { "keyword": { "type": "keyword" } }
      },
      "user_id": { "type": "long" },
      "created_at": { "type": "date", "format": "yyyy-MM-dd HH🇲🇲ss" }
    }
  }
}
'
> 说明:ES8/9默认启用HTTPS,需添加`-u`(认证)和`-k`(跳过证书验证,测试环境用)参数。

2. 查看索引

通用操作

# 查看所有索引
curl "localhost:9200/_cat/indices?v"  # ES6/7
curl -u elastic:your_password "https://localhost:9200/_cat/indices?v" -k  # ES8/9

# 查看threads索引详情
curl "localhost:9200/threads"  # ES6/7
curl -u elastic:your_password "https://localhost:9200/threads" -k  # ES8/9

3. 删除索引

通用操作(谨慎执行!):

curl -X DELETE "localhost:9200/threads"  # ES6/7
curl -u elastic:your_password -X DELETE "https://localhost:9200/threads" -k  # ES8/9

四、文档操作

1. 添加文档

通用逻辑:向索引中插入JSON格式的文档,可指定ID或自动生成。

ES6 示例(指定Type)

# 添加文档到threads索引的App\Thread类型(ES6)
curl -X POST "localhost:9200/threads/App\Thread/1" -H "Content-Type: application/json" -d '
{
  "title": "ES基础教程学习",
  "content": "这是一篇关于ES入门的笔记",
  "user_id": 1001,
  "created_at": "2024-08-09 14:30:00"
}
'
> 说明:URL格式为`索引名/type/文档ID`,ID可选(不指定则自动生成)。

ES7+ 示例(无Type)

# 添加文档到threads索引(ES7/8/9)
curl -u elastic:your_password -X POST "https://localhost:9200/threads/_doc/1" -k -H "Content-Type: application/json" -d '
{
  "title": "ES9新特性探索",
  "content": "ES9的向量搜索功能很强大",
  "user_id": 1001,
  "created_at": "2024-08-09 15:00:00"
}
'
> 说明:URL格式为`索引名/_doc/文档ID`,`_doc`是默认类型标识(可省略)。

2. 查询文档

按ID查询

# ES6
curl "localhost:9200/threads/App\Thread/1"

# ES7+
curl -u elastic:your_password "https://localhost:9200/threads/_doc/1" -k

全文搜索

通用逻辑:使用_search接口和查询DSL(领域特定语言)。

# 搜索标题包含“ES教程”的文档(ES6)
curl "localhost:9200/threads/App\Thread/_search" -H "Content-Type: application/json" -d '
{
  "query": {
    "match": { "title": "ES教程" }  # match查询:全文分词匹配
  }
}
'

# 搜索标题包含“ES9”的文档(ES7+)
curl -u elastic:your_password "https://localhost:9200/threads/_search" -k -H "Content-Type: application/json" -d '
{
  "query": {
    "match": { "title": "ES9" }
  }
}
'

3. 更新文档

# ES6:更新文档(指定Type)
curl -X POST "localhost:9200/threads/App\Thread/1/_update" -H "Content-Type: application/json" -d '
{
  "doc": { "content": "更新后的ES学习笔记" }  # 仅更新指定字段
}
'

# ES7+:更新文档
curl -u elastic:your_password -X POST "https://localhost:9200/threads/_doc/1/_update" -k -H "Content-Type: application/json" -d '
{
  "doc": { "content": "更新后的ES9特性笔记" }
}
'

4. 删除文档

# ES6
curl -X DELETE "localhost:9200/threads/App\Thread/1"

# ES7+
curl -u elastic:your_password -X DELETE "https://localhost:9200/threads/_doc/1" -k

五、高级查询

1. 条件过滤

查询user_id=1001且创建时间在2024年之后的文档:

# 通用语法(仅URL差异)
curl -X GET "localhost:9200/threads/App\Thread/_search" -H "Content-Type: application/json" -d '  # ES6
# curl -u elastic:your_password -X GET "https://localhost:9200/threads/_search" -k -H "Content-Type: application/json" -d '  # ES7+
{
  "query": {
    "bool": {  # 布尔查询:组合多个条件
      "must": [  # 必须满足
        { "term": { "user_id": 1001 } }  # term:精确匹配
      ],
      "filter": [  # 过滤(不影响评分)
        { "range": { "created_at": { "gte": "2024-01-01" } } }  # range:范围查询
      ]
    }
  }
}
'

2. 聚合分析

统计每个用户的发帖数量(Top 5):

# 通用语法(仅URL差异)
curl -X GET "localhost:9200/threads/App\Thread/_search" -H "Content-Type: application/json" -d '  # ES6
# curl -u elastic:your_password -X GET "https://localhost:9200/threads/_search" -k -H "Content-Type: application/json" -d '  # ES7+
{
  "size": 0,  # 不返回原始文档
  "aggs": {  # 聚合配置
    "user_post_count": {
      "terms": { "field": "user_id", "size": 5 }  # 按user_id分组统计
    }
  }
}
'

六、版本核心差异对比

特性 ES6.x ES7.x ES8.x ES9.x
Type支持 多Type(如App\Thread 仅支持_doc(兼容旧Type语法) 彻底移除Type 无Type
安全功能 需单独安装X-Pack 基础安全集成(需手动启用) 默认启用HTTPS和身份验证 强化安全策略,支持更多认证方式
Java依赖 Java 8 Java 8/11 Java 11+ Java 17+
新功能 基础全文检索 引入索引生命周期管理 内置机器学习、向量搜索(实验) 稳定向量搜索、AI集成
核心API变化 支持_type参数 弃用_type,推荐_doc 移除_all字段 优化批量操作API

七、总结

本教程以threads索引为例,覆盖了ES的基础操作(索引管理、文档CRUD、查询分析),并标注了不同版本的关键差异:

  • ES6:需关注Type概念(如项目中的App\Thread),适合维护旧系统。
  • ES7+:核心变化是移除Type,语法更简洁;ES8/9新增安全特性和AI功能,是学习重点。

通过对比学习,既能运维ES6旧项目,也能快速掌握最新版用法。实际使用中,建议优先选择ES8/9(生产环境稳定且功能全面),并参考官方文档深入学习。

京ICP备13031296号-4