day08-Elasticsearch
day08-Elasticsearch
目标
- 理解倒排索引原理 🍐 ❤️
- 会使用 IK 分词器 ✏️
- 理解索引库 Mapping 映射的属性含义 🍐 ❤️
- 能创建索引库及映射 ✏️
- 能实现文档的 CRUD ✏️
知识储备
- 使用过mysql的模糊查询关键字 like
- 能理解数据库中的索引的作用
- 使用百度搜索过词汇
1.初识 elasticsearch
elasticsearch
Elasticsearch 是一个基于 Apache Lucene 库实现的,Restful 风格的,分布式搜索和数据分析引擎。基于倒排索引技术,实现了高性能的全文检索和数据分析功能。官方网站如下:https://www.elastic.co/cn/
本章我们就一起来初步了解一下 Elasticsearch 的基本原理和一些基础概念。
1.1.倒排索引
倒排索引
elasticsearch 之所以有如此高性能的搜索表现,正是得益于底层的倒排索引技术。
倒排索引的概念是基于 MySQL 这样的正向索引而言的。
1.正向索引
我们先来回顾一下正向索引。
例如有一张名为 tb_goods
的表:
id | title | price |
---|---|---|
1 | 小米手机 | 3499 |
2 | 华为手机 | 4999 |
3 | 华为小米充电器 | 49 |
4 | 小米手环 | 49 |
... | ... | ... |
其中的 id
字段已经创建了索引,由于索引底层采用了 B+ 树结构,因此我们根据 id 搜索的速度会非常快。但是其他字段例如 title
,只在叶子节点上存在。
因此要根据 title
搜索的时候只能遍历树中的每一个叶子节点,判断 title 数据是否符合要求。
比如用户的 SQL 语句为:
select * from tb_goods where title like '%手机%';
那搜索的大概流程如图:

说明:
- 1)检查到搜索条件为
like '%手机%'
,需要找到title
中包含手机
的数据 - 2)逐条遍历每行数据(每个叶子节点),比如第 1 次拿到
id
为 1 的数据 - 3)判断数据中的
title
字段值是否符合条件 - 4)如果符合则放入结果集,不符合则丢弃
- 5)回到步骤 1
综上,根据 id 精确匹配时,可以走索引,查询效率较高。而当搜索条件为模糊匹配时,由于索引无法生效,导致从索引查询退化为全表扫描,效率很差。
因此,正向索引适合于根据索引字段的精确搜索,不适合基于部分词条的模糊匹配。
而倒排索引恰好解决的就是根据部分词条模糊匹配的问题。
2.倒排索引
倒排索引中有两个非常重要的概念:
- 文档(
Document
):用来搜索的数据,其中的每一条数据就是一个文档。例如一个网页、一个商品信息 - 词条(
Term
):对文档数据或用户搜索数据,利用某种算法分词,得到的具备含义的词语就是词条。例如:我是中国人,就可以分为:我、是、中国人、中国、国人这样的几个词条
创建倒排索引是对正向索引的一种特殊处理和应用,流程如下: 👇
- 将每一个文档的数据利用分词算法根据语义拆分,得到一个个词条
- 创建表,每行数据包括词条、词条所在文档 id、位置等信息
- 因为词条唯一性,可以给词条创建正向索引
此时形成的这张以词条为索引的表,就是倒排索引表,两者对比如下:
倒排索引的搜索流程如下(以搜索"华为手机"为例),如图:

流程描述:
- 1)用户输入条件
"华为手机"
进行搜索。 - 2)对用户输入条件分词,得到词条:
华为
、手机
。 - 3)拿着词条在倒排索引中查找(由于词条有索引,查询效率很高),即可得到包含词条的文档 id:
1、2、3
。 - 4)拿着文档
id
到正向索引中查找具体文档即可(由于id
也有索引,查询效率也很高)。
虽然要先查询倒排索引,再查询倒排索引,但是无论是词条、还是文档 id 都建立了索引,查询速度非常快!无需全表扫描。
总结
那么两者方式的优缺点是什么呢?
- 正向索引是最传统的,根据 id 索引的方式。但根据词条查询时,必须先逐条获取每个文档,然后判断文档中是否包含所需要的词条,是根据文档找词条的过程。
- 而倒排索引则相反,是先找到用户要搜索的词条,根据词条得到保护词条的文档的 id,然后根据 id 获取文档。是根据词条找文档的过程。
正向索引:
- 优点:
- 可以给多个字段创建索引
- 根据索引字段搜索、排序速度非常快
- 缺点:
- 根据非索引字段,或者索引字段中的部分词条查找时,只能全表扫描。
倒排索引:
- 优点:
- 根据词条搜索、模糊搜索时,速度非常快
- 缺点:
- 只能给词条创建索引,而不是字段
- 无法根据字段做排序
课堂作业
- 正向索引和倒排索引的区别?以及优缺点?🎤
1.2.基础概念
elasticsearch 中有很多独有的概念,与 mysql 中略有差别,但也有相似之处。 🎯
基础概念
1.文档和字段
elasticsearch 是面向 文档(Document) 存储的,可以是数据库中的一条商品数据,一个订单信息。文档数据会被序列化为 json
格式后存储在 elasticsearch
中:
因此,原本数据库中的一行数据就是 ES 中的一个 JSON 文档;而数据库中每行数据都包含很多列,这些列就转换为 JSON 文档中的字段(Field)。
2.索引和映射
随着业务发展,需要在 es 中存储的文档也会越来越多,比如有商品的文档、用户的文档、订单文档等等:

所有文档都散乱存放显然非常混乱,也不方便管理。
因此,我们要将类型相同的文档集中在一起管理,称为索引(Index)。例如:
- 所有用户文档,就可以组织在一起,称为用户的索引;
- 所有商品的文档,可以组织在一起,称为商品的索引;
- 所有订单的文档,可以组织在一起,称为订单的索引;
因此,我们可以把索引当做是数据库中的表 。
- 数据库的表会有约束信息,用来定义表的结构、字段的名称、类型等信息。
- 索引库中就有映射(mapping),是索引中文档的字段约束信息,类似表的结构约束。
mysql 与 elasticsearch
我们统一的把 mysql 与 elasticsearch 的概念做一下对比:
MySQL | Elasticsearch | 说明 |
---|---|---|
Table | Index | 索引(index),就是文档的集合,类似数据库的表(table) |
Row | Document | 文档(Document),就是一条条的数据,类似数据库中的行(Row),文档都是 JSON 格式 |
Column | Field | 字段(Field),就是 JSON 文档中的字段,类似数据库中的列(Column) |
Schema | Mapping | Mapping(映射)是索引中文档的约束,例如字段类型约束。类似数据库的表结构(Schema) |
SQL | DSL | DSL 是 elasticsearch 提供的 JSON 风格的请求语句,用来操作 elasticsearch,实现 CRUD |

❓那是不是说,我们学习了 elasticsearch 就不再需要 mysql 了呢? ❓
并不是如此,两者各自有自己的擅长之处:
- Mysql:擅长事务类型操作,可以确保数据的安全和一致性
- Elasticsearch:擅长海量数据的搜索、分析、计算
因此在企业中,往往是两者结合使用:
- 对安全性要求较高的写操作,使用 mysql 实现
- 对查询性能要求较高的搜索需求,使用 elasticsearch 实现
- 两者再基于某种方式,实现数据的同步,保证一致性 👇

总结
课堂作业
- elasticsearch 中有哪些独有的概念? 分别什么含义?🎤
- es这么好用,是不是企业中可以弃用Mysql了?🎤 ❓
1.4.安装
安装
学习ES,要安装的内容包含 3 部分:
- elasticsearch
Elasticsearch是提供核心的数据存储、搜索、分析功能的。
- kibana
Kibana,Elasticsearch 对外提供的是 Restful 风格的 API,任何操作都可以通过发送 http 请求来完成。不过 http 请求的方式、路径、还有请求参数的格式都有严格的规范。这些规范我们肯定记不住,因此我们要借助于 Kibana 这个服务。
- ik分词器
IK分词是一款国人作者林良益开发的相对简单的中文分词器,基于java语言开发的轻量级的中文分词工具包
代码操作
1.4.1.安装 elasticsearch
通过下面的 Docker 命令即可安装单机版本的 elasticsearch:
docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
--privileged \
--network hmall \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.12.1
注意:
- 这里我们采用的是 elasticsearch 的 7.12.1 版本,由于 8 以上版本的 JavaAPI 变化很大,在企业中应用并不广泛,企业中应用较多的还是 8 以下的版本。
- 内存最小设置512M ,默认是1gb,集群需要4G,最好是8G,如果是学生的云主机内存可能跑不起来 如果拉取镜像困难,可以直接导入课前资料提供的镜像 tar 包:
# 加载镜像
docker load -i 镜像名字
# 查看镜像
docker images

安装完成后,访问 9200 端口,如:http://192.168.138.135:9200/,即可看到响应的 Elasticsearch 服务的基本信息:

2.安装 Kibana
Kibana 是 elastic 公司提供的用于操作 Elasticsearch 的可视化控制台。它的功能非常强大,包括:
- 对 Elasticsearch 数据的搜索、展示
- 对 Elasticsearch 数据的统计、聚合,并形成图形化报表、图形
- 对 Elasticsearch 的集群状态监控
- 它还提供了一个开发控制台(DevTools),在其中对 Elasticsearch 的 Restful 的 API 接口提供了语法提示
通过下面的 Docker 命令,即可部署 Kibana:
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=hmall \
-p 5601:5601 \
kibana:7.12.1
如果拉取镜像困难,可以直接导入课前资料提供的镜像 tar 包:
# 加载镜像
docker load -i 镜像名字
# 查看镜像
docker images

安装完成后,直接访问 5601 端口 如:http://192.168.138.135:5601/,即可看到控制台页面:

选择 Explore on my own
之后,进入主页面:

然后选中 Dev tools
,进入开发工具页面:

具有自动提示功能
总结
课堂作业
- 安装Elasticsearch,内存设置为64M,可以吗?
- Kibana有什么作用?🎤
1.5.IK 分词器
Elasticsearch 的关键就是倒排索引,而倒排索引依赖于对文档内容的分词,而分词则需要高效、精准的分词算法,IK 分词器就是这样一个中文分词算法。 🎯
分词器
IK分词全名为IK Analyzer
IK分词是一款国人作者林良益开发的相对简单的中文分词器,IKAnalyzer 是一个开源的,基于java语言开发的轻量级的中文分词工具包。
- 从2006年12月推出1.0版开始,IKAnalyzer已经推出 了3个大版本。
- 最初,它是以开源项目 Lucene为应用主体的,结合词典分词和文法分析算法的中文分词组件。
- 新版本的IKAnalyzer3.0则发展为 面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。
IK 这个名字是来源于暗黑破坏神2这款游戏,它是游戏中武器装备的名字。刚好林良益在做这个分词器的时候,也在玩这款游戏,而且刚好打到这个装备,很开心。想想 Java 也是开发人员在开发的过程中正好在喝咖啡,所以就叫了 Java 这个名字。所以那时候也在想,给这个分词器命名的话,我就把它命名为 Immortal King,中文名叫不朽之王,是那个装备的名称,这个名字就是这么来的。
操作
相关信息
- 安装ik
- 使用ik
- 拓展词典
1.安装 IK 分词器
方案一:在线安装
运行一个命令即可:
docker exec -it es ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
然后重启 es 容器:
docker restart es
方案二:离线安装 课堂建议
如果网速较差 ,也可以选择离线安装。
首先,查看之前安装的 Elasticsearch 容器的 plugins 数据卷目录:
docker volume inspect es-plugins
结果如下:
[
{
"CreatedAt": "2024-11-06T10:06:34+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/es-plugins/_data",
"Name": "es-plugins",
"Options": null,
"Scope": "local"
}
]
可以看到 elasticsearch 的插件挂载到了 /var/lib/docker/volumes/es-plugins/_data
这个目录。我们需要把 IK 分词器上传至这个目录。
找到课前资料提供的 ik 分词器插件,课前资料提供了 7.12.1
版本的 ik 分词器压缩文件,你需要对其解压改名为:ik文件夹:

然后上传至虚拟机的 /var/lib/docker/volumes/es-plugins/_data
这个目录:

最后,重启 es 容器:
docker restart es
1.5.2.使用 IK 分词器
IK 分词器包含两种模式:
ik_smart
:智能语义切分ik_max_word
:最细粒度切分
我们在 Kibana 的 DevTools 上来测试分词器,首先测试 Elasticsearch 官方提供的标准分词器:
POST /_analyze
{
"analyzer": "standard",
"text": "长沙校区Java84期,学习java太棒了,大家都不想润了"
}
结果如下:
{
"tokens" : [
{
"token" : "黑",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "马",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "程",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "序",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "员",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "长",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
},
{
"token" : "沙",
"start_offset" : 6,
"end_offset" : 7,
"type" : "<IDEOGRAPHIC>",
"position" : 6
},
{
"token" : "校",
"start_offset" : 7,
"end_offset" : 8,
"type" : "<IDEOGRAPHIC>",
"position" : 7
},
{
"token" : "区",
"start_offset" : 8,
"end_offset" : 9,
"type" : "<IDEOGRAPHIC>",
"position" : 8
},
{
"token" : "java84",
"start_offset" : 9,
"end_offset" : 15,
"type" : "<ALPHANUM>",
"position" : 9
},
{
"token" : "期",
"start_offset" : 15,
"end_offset" : 16,
"type" : "<IDEOGRAPHIC>",
"position" : 10
},
{
"token" : "学",
"start_offset" : 17,
"end_offset" : 18,
"type" : "<IDEOGRAPHIC>",
"position" : 11
},
{
"token" : "习",
"start_offset" : 18,
"end_offset" : 19,
"type" : "<IDEOGRAPHIC>",
"position" : 12
},
{
"token" : "java",
"start_offset" : 19,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 13
},
{
"token" : "太",
"start_offset" : 23,
"end_offset" : 24,
"type" : "<IDEOGRAPHIC>",
"position" : 14
},
{
"token" : "棒",
"start_offset" : 24,
"end_offset" : 25,
"type" : "<IDEOGRAPHIC>",
"position" : 15
},
{
"token" : "了",
"start_offset" : 25,
"end_offset" : 26,
"type" : "<IDEOGRAPHIC>",
"position" : 16
},
{
"token" : "大",
"start_offset" : 27,
"end_offset" : 28,
"type" : "<IDEOGRAPHIC>",
"position" : 17
},
{
"token" : "家",
"start_offset" : 28,
"end_offset" : 29,
"type" : "<IDEOGRAPHIC>",
"position" : 18
},
{
"token" : "都",
"start_offset" : 29,
"end_offset" : 30,
"type" : "<IDEOGRAPHIC>",
"position" : 19
},
{
"token" : "不",
"start_offset" : 30,
"end_offset" : 31,
"type" : "<IDEOGRAPHIC>",
"position" : 20
},
{
"token" : "想",
"start_offset" : 31,
"end_offset" : 32,
"type" : "<IDEOGRAPHIC>",
"position" : 21
},
{
"token" : "润",
"start_offset" : 32,
"end_offset" : 33,
"type" : "<IDEOGRAPHIC>",
"position" : 22
},
{
"token" : "了",
"start_offset" : 33,
"end_offset" : 34,
"type" : "<IDEOGRAPHIC>",
"position" : 23
}
]
}
可以看到,标准分词器智能 1 字 1 词条,无法正确对中文做分词。
我们再测试 IK 分词器的ik_smart智能分词:
POST /_analyze
{
"analyzer": "ik_smart",
"text": "长沙校区Java84期,学习java太棒了,大家都不想润了"
}
执行结果如下:
{
"tokens" : [
{
"token" : "黑马",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "程序员",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "长沙",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "校区",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "java84",
"start_offset" : 9,
"end_offset" : 15,
"type" : "LETTER",
"position" : 4
},
{
"token" : "期",
"start_offset" : 15,
"end_offset" : 16,
"type" : "COUNT",
"position" : 5
},
{
"token" : "学习",
"start_offset" : 17,
"end_offset" : 19,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "java",
"start_offset" : 19,
"end_offset" : 23,
"type" : "ENGLISH",
"position" : 7
},
{
"token" : "太棒了",
"start_offset" : 23,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "大家",
"start_offset" : 27,
"end_offset" : 29,
"type" : "CN_WORD",
"position" : 9
},
{
"token" : "都",
"start_offset" : 29,
"end_offset" : 30,
"type" : "CN_CHAR",
"position" : 10
},
{
"token" : "不想",
"start_offset" : 30,
"end_offset" : 32,
"type" : "CN_WORD",
"position" : 11
},
{
"token" : "润",
"start_offset" : 32,
"end_offset" : 33,
"type" : "CN_CHAR",
"position" : 12
},
{
"token" : "了",
"start_offset" : 33,
"end_offset" : 34,
"type" : "CN_CHAR",
"position" : 13
}
]
}
再测试 IK 分词器的ik_max_word分词:
POST /_analyze
{
"analyzer": "ik_max_word",
"text": "长沙校区Java84期,学习java太棒了,大家都不想润了"
}
{
"tokens" : [
{
"token" : "黑马",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "程序员",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "程序",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "员",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "长沙",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "校区",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "java84",
"start_offset" : 9,
"end_offset" : 15,
"type" : "LETTER",
"position" : 6
},
{
"token" : "java",
"start_offset" : 9,
"end_offset" : 13,
"type" : "ENGLISH",
"position" : 7
},
{
"token" : "84",
"start_offset" : 13,
"end_offset" : 15,
"type" : "ARABIC",
"position" : 8
},
{
"token" : "期",
"start_offset" : 15,
"end_offset" : 16,
"type" : "COUNT",
"position" : 9
},
{
"token" : "学习",
"start_offset" : 17,
"end_offset" : 19,
"type" : "CN_WORD",
"position" : 10
},
{
"token" : "java",
"start_offset" : 19,
"end_offset" : 23,
"type" : "ENGLISH",
"position" : 11
},
{
"token" : "太棒了",
"start_offset" : 23,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 12
},
{
"token" : "太棒",
"start_offset" : 23,
"end_offset" : 25,
"type" : "CN_WORD",
"position" : 13
},
{
"token" : "了",
"start_offset" : 25,
"end_offset" : 26,
"type" : "CN_CHAR",
"position" : 14
},
{
"token" : "大家",
"start_offset" : 27,
"end_offset" : 29,
"type" : "CN_WORD",
"position" : 15
},
{
"token" : "都不",
"start_offset" : 29,
"end_offset" : 31,
"type" : "CN_WORD",
"position" : 16
},
{
"token" : "不想",
"start_offset" : 30,
"end_offset" : 32,
"type" : "CN_WORD",
"position" : 17
},
{
"token" : "润",
"start_offset" : 32,
"end_offset" : 33,
"type" : "CN_CHAR",
"position" : 18
},
{
"token" : "了",
"start_offset" : 33,
"end_offset" : 34,
"type" : "CN_CHAR",
"position" : 19
}
]
}
3.拓展词典
随着互联网的发展,“造词运动”也越发的频繁。出现了很多新的词语,在原有的词汇列表中并不存在。比如:“黑马程序员”,“传智播客”,"润了","焱哥" 等。
IK 分词器无法对这些词汇分词,测试一下:
POST /_analyze
{
"analyzer": "ik_max_word",
"text": "东方红大厦黑马程序员,真的泰裤辣!下课都不润了"
}
结果:
{
"tokens" : [
{
"token" : "东方红",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "东方",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "红",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "大厦",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "黑马",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "程序员",
"start_offset" : 7,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "程序",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "员",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_CHAR",
"position" : 7
},
{
"token" : "真的",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "泰裤辣",
"start_offset" : 13,
"end_offset" : 16,
"type" : "CN_WORD",
"position" : 9
},
{
"token" : "下课",
"start_offset" : 17,
"end_offset" : 19,
"type" : "CN_WORD",
"position" : 10
},
{
"token" : "都不",
"start_offset" : 19,
"end_offset" : 21,
"type" : "CN_WORD",
"position" : 11
},
{
"token" : "润",
"start_offset" : 21,
"end_offset" : 22,
"type" : "CN_CHAR",
"position" : 12
},
{
"token" : "了",
"start_offset" : 22,
"end_offset" : 23,
"type" : "CN_CHAR",
"position" : 13
}
]
}
可以看到,东方红大厦
和 黑马程序员
都无法正确分词。
所以要想正确分词,IK 分词器的词库也需要不断的更新,IK 分词器提供了扩展词汇的功能。
1)打开 IK 分词器 config 目录:

注意 ,如果采用在线安装的通过,默认是没有 config 目录的,需要把课前资料提供的 ik 下的 config 上传至对应目录。
2)在 IKAnalyzer.cfg.xml 配置文件内容添加:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 *** 添加扩展词典-->
<entry key="ext_dict">ext.dic</entry>
</properties>
3)在 IK 分词器的 config 目录新建一个 ext.dic
,可以参考 config 目录下复制一个配置文件进行修改
进入数据卷
cd /var/lib/docker/volumes/es-plugins/_data/ik/config/
复制一个现成的词库为ex
cp suffix.dic ./ext.dic
修改ext.dic内容
vim ext.dic
增加:
东方红大厦
程序员
润了
泰裤辣
:wq
保存退出
4)重启 elasticsearch
docker restart es
# 查看 日志 使用管道符 查询
docker logs -f es | grep ext.dic

再次测试,可以发现 东方红大厦
和 泰裤辣
都正确分词了:
{
"tokens" : [
{
"token" : "东方红大厦",
"start_offset" : 0,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "东方红",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "东方",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "红",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "大厦",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "程序员",
"start_offset" : 5,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "程序员",
"start_offset" : 7,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "程序",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "员",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_CHAR",
"position" : 9
},
{
"token" : "真的",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 10
},
{
"token" : "泰裤辣",
"start_offset" : 13,
"end_offset" : 16,
"type" : "CN_WORD",
"position" : 11
},
{
"token" : "下课",
"start_offset" : 17,
"end_offset" : 19,
"type" : "CN_WORD",
"position" : 12
},
{
"token" : "都不",
"start_offset" : 19,
"end_offset" : 21,
"type" : "CN_WORD",
"position" : 13
},
{
"token" : "润了",
"start_offset" : 21,
"end_offset" : 23,
"type" : "CN_WORD",
"position" : 14
}
]
}
总结
分词器的作用是什么?
- 创建倒排索引时,对文档分词
- 用户搜索时,对输入的内容分词
IK 分词器有几种模式?
ik_smart
:智能切分,粗粒度,ik_max_word
:最细切分,细粒度
IK 分词器如何拓展词条?如何停用词条?
- 利用 config 目录的
IkAnalyzer.cfg.xml
文件添加拓展词典和停用词典 - 在词典中添加拓展词条或者停用词条
课堂作业
- 分词器的作用是什么??🎤
- IK 分词器有几种模式? 有什么区别??🎤
- IK 分词器如何拓展词条?如何停用词条??🎤
2.索引库操作
Index 就类似数据库表,Mapping 映射就类似表的结构。我们要向 es 中存储数据,必须先创建 Index 和 Mapping 🎯
2.1.Mapping 映射属性
Mapping映射属性
Mapping 是对索引库中文档的约束,常见的 Mapping 属性包括:
type
:字段数据类型,常见的简单类型有:- 字符串:
text
(可分词的文本)、keyword
(精确值,例如:品牌、国家、ip 地址) - 数值:
long
、integer
、short
、byte
、double
、float
、 - 布尔:
boolean
- 日期:
date
- 对象:
object
- 字符串:
index
:是否创建索引,默认为true
analyzer
:使用哪种分词器properties
:该字段的子字段
例如下面的 json 文档:
{
"age": 21,
"weight": 52.1,
"isMarried": false,
"info": "Java讲师",
"email": "zy@itcast.cn",
"score": [99.1, 99.5, 98.9],
"name": {
"firstName": "云",
"lastName": "赵"
}
}
对应的每个字段映射(Mapping):
字段名 | 字段类型 | 类型说明 | 是否参与搜索 | 是否参与分词 | 分词器 | |
---|---|---|---|---|---|---|
age | integer | 整数 | - [x] | - [ ] | —— | |
weight | float | 浮点数 | - [x] | - [ ] | —— | |
isMarried | boolean | 布尔 | - [x] | - [ ] | —— | |
info | text | 字符串,但需要分词 | - [x] | - [x] | IK | |
keyword | 字符串,但是不分词 | - [ ] | - [ ] | —— | ||
score | float | 只看数组中元素类型 | - [x] | - [ ] | —— | |
name | firstName | keyword | 字符串,但是不分词 | - [x] | - [ ] | —— |
lastName | keyword | 字符串,但是不分词 | - [x] | - [ ] | —— |
总结
课堂作业
- Mapping是什么?🎤
- type中的keyword是什么意思?有什么应用场景🎤
2.2.索引库的 CRUD
索引库的CRUD
由于 Elasticsearch 采用的是 Restful 风格的 API,因此其请求方式和路径相对都比较规范,而且请求参数也都采用 JSON 风格。
我们直接基于 Kibana 的 DevTools 来编写请求做测试,由于有语法提示,会非常方便。
操作
步骤
- 创建索引库和映射
- 查询索引库
- 修改索引库
- 删除索引库
1.创建索引库和映射
基本语法:
- 请求方式:
PUT
- 请求路径:
/索引库名
,可以自定义 - 请求参数:
mapping
映射
格式:
PUT /索引库名称
{
"mappings": {
"properties": {
"字段名":{
"type": "text",
"analyzer": "ik_smart"
},
"字段名2":{
"type": "keyword",
"index": "false"
},
"字段名3":{
"properties": {
"子字段": {
"type": "keyword"
}
}
},
// ...略
}
}
}
ik_smart分词器
示例:
PUT /heima
{
"mappings": {
"properties": {
"info":{
"type": "text",
"analyzer": "ik_smart"
},
"email":{
"type": "keyword",
"index": "false"
},
"name":{
"properties": {
"firstName": {
"type": "keyword"
}
}
}
}
}
}
heima:索引库名称
输入:GET /heima 查看

2.查询索引库
基本语法:
- 请求方式:GET
- 请求路径:/索引库名
- 请求参数:无
格式:
GET /索引库名
示例:
GET /heima
3.修改索引库
倒排索引结构虽然不复杂,但是一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改 mapping。
虽然无法修改 mapping 中已有的字段,但是却允许添加新的字段到 mapping 中,因为不会对倒排索引产生影响。因此修改索引库能做的就是向索引库中添加新字段,或者更新索引库的基础属性。
语法说明:
PUT /索引库名/_mapping
{
"properties": {
"新字段名":{
"type": "integer"
}
}
}
示例:
PUT /heima/_mapping
{
"properties": {
"age":{
"type": "integer"
}
}
}

查询修改结果:👇

2.2.4.删除索引库
语法:
- 请求方式:DELETE
- 请求路径:/索引库名
- 请求参数:无
格式:
DELETE /索引库名
示例:
DELETE /heima
删除后查询:
总结
索引库操作有哪些?
- 创建索引库:PUT /索引库名
- 查询索引库:GET /索引库名
- 删除索引库:DELETE /索引库名
- 修改索引库,添加字段:PUT /索引库名/_mapping
可以看到,对索引库的操作基本遵循的 Restful 的风格,因此 API 接口非常统一,方便记忆。
课堂作业
- 索引库操作有哪些?🎤
- 索引库一旦创建,无法修改 mapping,为什么?
3.文档操作
3.1文档的CURD
文档操作
有了索引库,接下来就可以向索引库中添加数据了。
Elasticsearch 中的数据其实就是 JSON 风格的文档。操作文档自然保护 增
、删
、改
、查
等几种常见操作,我们分别来学习。🎯
操作
步骤
- 新增文档
- 查询文档
- 删除文档
- 修改文档
- 批处理
3.1.新增文档
语法:
POST /索引库名/_doc/文档id
{
"字段1": "值1",
"字段2": "值2",
"字段3": {
"子属性1": "值3",
"子属性2": "值4"
},
}
示例:
POST /heima/_doc/1
{
"info": "Java讲师",
"email": "huyan@itcast.cn",
"name": {
"firstName": "焱",
"lastName": "哥"
}
}
响应:

3.2.查询文档
根据 rest 风格,新增是 post,查询应该是 get,不过查询一般都需要条件,这里我们把文档 id 带上。
语法:
GET /{索引库名称}/_doc/{id}
示例:
GET /heima/_doc/1
查看结果:

3.3.删除文档
删除使用 DELETE 请求,同样,需要根据 id 进行删除:
语法:
DELETE /{索引库名}/_doc/id值
示例:
DELETE /heima/_doc/1
结果:


3.4.修改文档
修改有两种方式:
- 全量修改:直接覆盖原来的文档
- 局部修改:修改文档中的部分字段
3.4.1.全量修改
全量修改是覆盖原来的文档,其本质是两步操作:
- 根据指定的 id 删除文档
- 新增一个相同 id 的文档
注意:如果根据 id 删除时,id 不存在,第二步的新增也会执行,也就从修改变成了新增操作了。
语法:
PUT /{索引库名}/_doc/文档id
{
"字段1": "值1",
"字段2": "值2",
// ... 略
}
示例:
PUT /heima/_doc/1
{
"info": "Java讲师",
"email": "huyan@itcast.cn",
"name": {
"firstName": "焱",
"lastName": "哥"
}
}
由于 id
为 1
的文档已经被删除,所以第一次执行时,得到的反馈是 created
:

所以如果执行第 2 次时,得到的反馈则是 updated
:

3.4.2.局部修改
局部修改是只修改指定 id 匹配的文档中的部分字段。
语法:
POST /{索引库名}/_update/文档id
{
"doc": {
"字段名": "新的值",
}
}
示例:
POST /heima/_update/1
{
"doc": {
"email": "yangeit@itcast.cn"
}
}
执行结果:

查询结果👇

3.5.批处理
批处理采用 POST 请求,基本语法如下:
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
其中:
index
代表新增操作_index
:指定索引库名_id
指定要操作的文档 id{ "field1" : "value1" }
:则是要新增的文档内容
delete
代表删除操作_index
:指定索引库名_id
指定要操作的文档 id
update
代表更新操作_index
:指定索引库名_id
指定要操作的文档 id{ "doc" : {"field2" : "value2"} }
:要更新的文档字段
示例,批量新增:
POST /_bulk
{"index": {"_index":"hmall", "_id": "3"}}
{"info": "C++讲师", "email": "ww@itcast.cn", "name":{"firstName": "五", "lastName":"赵"}}
{"index": {"_index":"hmall", "_id": "4"}}
{"info": "前端讲师", "email": "zhangsan@itcast.cn", "name":{"firstName": "三", "lastName":"张"}}


批量删除:
POST /_bulk
{"delete":{"_index":"hmall", "_id": "3"}}
{"delete":{"_index":"hmall", "_id": "4"}}


查不到数据
总结
文档操作有哪些?
- 创建文档:
POST /{索引库名}/_doc/文档id { json文档 }
- 查询文档:
GET /{索引库名}/_doc/文档id
- 删除文档:
DELETE /{索引库名}/_doc/文档id
- 修改文档:
- 全量修改:
PUT /{索引库名}/_doc/文档id { json文档 }
- 局部修改:
POST /{索引库名}/_update/文档id { "doc": {字段}}
- 全量修改:
课堂作业
- 文档操作有哪些?🎤
- 全量修改和局部修改有区别吗?
4.RestAPI
4.0.初始化 RestClient
初始化RestClient
ES 官方提供了各种不同语言的客户端,用来操作 ES。这些客户端的本质就是组装 DSL 语句,通过 http 请求发送给 ES。
官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
由于 ES 目前最新版本是 8.8,提供了全新版本的客户端,老版本的客户端已经被标记为过时。而我们采用的是 7.12 版本,因此只能使用老版本客户端:

然后选择 7.12 版本,HighLevelRestClient 版本:

代码操作
流程
在 elasticsearch 提供的 API 中,与 elasticsearch 一切交互都封装在一个名为 RestHighLevelClient
的类中,必须先完成这个对象的初始化,建立与 elasticsearch 的连接。

1)在 item-service
模块中引入 es
的 RestHighLevelClient
依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2)因为 SpringBoot 默认的 ES 版本是 7.17.10
,所以我们需要覆盖默认的 ES 版本:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
3)初始化 RestHighLevelClient:
初始化的代码如下:
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")
));
这里为了单元测试方便,我们创建一个测试类 IndexTest
,然后将初始化的代码编写在 @BeforeEach
方法中:
和spring环境无关,因此需要测试前创还能链接,测试后释放资源
package com.hmall.item.es;
public class IndexTest {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.138.135:9200")
));
}
@Test
void testConnect() {
System.out.println(client);
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}

4.1.创建索引库
创建索引库
由于要实现对商品搜索,所以我们需要将商品添加到 Elasticsearch 中,不过需要根据搜索业务的需求来设定索引库结构,而不是一股脑的把 MySQL 数据写入 Elasticsearch.
代码操作
核心步骤
- 分析Mapping映射
- 编写代码
1.Mapping映射
搜索页面的效果如图所示:

实现搜索功能需要的字段包括三大部分:
搜索过滤字段
- 分类
- 品牌
- 价格
排序字段
- 默认:按照更新时间降序排序
- 销量
- 价格
展示字段
- 商品 id:用于点击后跳转
- 图片地址
- 是否是广告推广商品
- 名称
- 价格
- 评价数量
- 销量
对应的商品表结构如下,索引库无关字段已经划掉:

结合数据库表结构,以上字段对应的 mapping 映射属性如下:
字段名 | 字段类型 | 类型说明 | 是否参与搜索 | 是否参与分词 | 分词器 | |
---|---|---|---|---|---|---|
id | long | 长整数 | - [x] | - [ ] | —— | |
name | text | 字符串,参与分词搜索 | - [x] | - [x] | IK | |
price | integer | 以分为单位,所以是整数 | - [x] | - [ ] | —— | |
stock | integer | 字符串,但需要分词 | - [x] | - [ ] | —— | |
image | keyword | 字符串,但是不分词 | - [ ] | - [ ] | —— | |
category | keyword | 字符串,但是不分词 | - [x] | - [ ] | —— | |
brand | keyword | 字符串,但是不分词 | - [x] | - [ ] | —— | |
sold | integer | 销量,整数 | - [x] | - [ ] | —— | |
commentCount | integer | 销量,整数 | - [x] | - [ ] | —— | |
isAD | boolean | 布尔类型 | - [x] | - [ ] | —— | |
updateTime | Date | 更新时间 | - [x] | - [ ] | —— |
因此,最终我们的索引库文档结构应该是这样:
PUT /items
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"price":{
"type": "integer"
},
"stock":{
"type": "integer"
},
"image":{
"type": "keyword",
"index": false
},
"category":{
"type": "keyword"
},
"brand":{
"type": "keyword"
},
"sold":{
"type": "integer"
},
"commentCount":{
"type": "integer"
},
"isAD":{
"type": "boolean"
},
"updateTime":{
"type": "date"
}
}
}
}
4.1.2.创建索引
创建索引库的 API 如下:

代码分为三步: 👇
- 1)创建 Request 对象。
- 因为是创建索引库的操作,因此 Request 是
CreateIndexRequest
。
- 因为是创建索引库的操作,因此 Request 是
- 2)添加请求参数
- 其实就是 Json 格式的 Mapping 映射参数。因为 json 字符串很长,这里是定义了静态字符串常量
MAPPING_TEMPLATE
,让代码看起来更加优雅。
- 其实就是 Json 格式的 Mapping 映射参数。因为 json 字符串很长,这里是定义了静态字符串常量
- 3)发送请求
client.indices()
方法的返回值是IndicesClient
类型,封装了所有与索引库操作有关的方法。例如创建索引、删除索引、判断索引是否存在等
在 item-service
中的 IndexTest
测试类中,具体代码如下:
private RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.138.135:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
@Test
void testCreateIndex() throws IOException {
// 1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("items");
// 2.准备请求参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"stock\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"image\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"category\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"sold\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"commentCount\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"isAD\":{\n" +
" \"type\": \"boolean\"\n" +
" },\n" +
" \"updateTime\":{\n" +
" \"type\": \"date\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
可以在kabana中查看索引库是否创建成功

4.2.删除和判断索引库是否存在
删除和判断索引库是否存在
删除索引库的请求非常简单:
DELETE /items
与创建索引库相比:
- 请求方式从 PUT 变为 DELTE
- 请求路径不变
- 无请求参数
所以代码的差异,注意体现在 Request 对象上。流程如下:👇
- 1)创建 Request 对象。这次是 DeleteIndexRequest 对象
- 2)准备参数。这里是无参,因此省略
- 3)发送请求。改用 delete 方法
在 item-service
中的 IndexTest
测试类中,编写单元测试,实现删除索引:
@Test
void testDeleteIndex() throws IOException {
// 1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("items");
// 2.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}

判断索引库是否存在
判断索引库是否存在,本质就是查询,对应的请求语句是:
GET /items
因此与删除的 Java 代码流程是类似的,流程如下:
- 1)创建 Request 对象。这次是 GetIndexRequest 对象
- 2)准备参数。这里是无参,直接省略
- 3)发送请求。改用 exists 方法
@Test
void testExistsIndex() throws IOException {
// 1.创建Request对象
GetIndexRequest request = new GetIndexRequest("items");
// 2.发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 3.输出
System.err.println(exists ? "索引库已经存在!" : "索引库不存在!");
}

总结
JavaRestClient 操作 elasticsearch 的流程基本类似。核心是 client.indices()
方法来获取索引库的操作对象。
索引库操作的基本步骤:
- 初始化
RestHighLevelClient
- 创建 XxxIndexRequest。XXX 是
Create
、Get
、Delete
- 准备请求参数(
Create
时需要,其它是无参,可以省略) - 发送请求。调用
RestHighLevelClient#indices().xxx()
方法,xxx 是create
、exists
、delete
课堂作业
- 参考上述步骤,完成练习 ✏️
5.RestClient 操作文档
5.1.新增文档
新增文档
我们需要将数据库中的商品信息导入 elasticsearch 中,而不是造假数据了。🎯
代码操作
索引库准备好以后,就可以操作文档了。为了与索引库操作分离,我们再次创建一个测试类,做两件事情:
- 初始化 RestHighLevelClient
- 我们的商品数据在数据库,需要利用 IHotelService 去查询,所以注入这个接口
package com.hmall.item.es;
import java.io.IOException;
@SpringBootTest(properties = "spring.profiles.active=local")
public class DocumentTest {
private RestHighLevelClient client;
@Autowired
private IItemService itemService;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.138.135:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
1.实体类
索引库结构与数据库结构还存在一些差异 ,因此我们要定义一个索引库结构对应的实体。
在 hm-service
模块的 com.hmall.item.domain.dto
包中定义一个新的 DTO:
package com.hmall.item.domain.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@ApiModel(description = "索引库实体")
public class ItemDTOES{
@ApiModelProperty("商品id")
private String id;
@ApiModelProperty("商品名称")
private String name;
@ApiModelProperty("价格(分)")
private Integer price;
@ApiModelProperty("库存数量")
private Integer stock;
@ApiModelProperty("商品图片")
private String image;
@ApiModelProperty("类目名称")
private String category;
@ApiModelProperty("品牌名称")
private String brand;
@ApiModelProperty("销量")
private Integer sold;
@ApiModelProperty("评论数")
private Integer commentCount;
@ApiModelProperty("是否是推广广告,true/false")
private Boolean isAD;
@ApiModelProperty("更新时间")
private LocalDateTime updateTime;
}
2.API 语法
新增文档的请求语法如下:
POST /{索引库名}/_doc/1
{
"name": "Jack",
"age": 21
}
对应的 JavaAPI 如下:

可以看到与索引库操作的 API 非常类似,同样是三步走:
- 1)创建 Request 对象,这里是
IndexRequest
,因为添加文档就是创建倒排索引的过程 - 2)准备请求参数,本例中就是 Json 文档
- 3)发送请求
变化的地方在于,这里直接使用 client.xxx()
的 API,不再需要 client.indices()
索引操作了。
@Test
void testExistsIndex() throws IOException {
Item item = itemService.getById(584394);
// 1.创建Request对象
IndexRequest request = new IndexRequest("items").id(item.getId().toString());
// 2.准备请求参数
request.source(JSONUtil.toJsonStr(BeanUtil.copyProperties(item, ItemDTOES.class)), XContentType.JSON);
//3. 发送请求
client.index(request,RequestOptions.DEFAULT);
}

3.完整代码
我们导入商品数据,除了参考 API 模板三步走
以外,还需要做几点准备工作:
- 商品数据来自于数据库,我们需要先查询出来,得到
Item
对象 Item
对象需要转为ItemDTO
对象ItemDTO
需要序列化为json
格式
因此,代码整体步骤如下:
- 1)根据 id 查询商品数据
Item
- 2)将
Item
封装为ItemDTO
- 3)将
ItemDTO
序列化为 JSON - 4)创建 IndexRequest,指定索引库名和 id
- 5)准备请求参数,也就是 JSON 文档
- 6)发送请求
在 item-service
的 DocumentTest
测试类中,编写单元测试:
@Test
void testAddDocument() throws IOException {
// 1.根据id查询商品数据
Item item = itemService.getById(100002644680L);
// 2.转换为文档类型
ItemDTO itemDTO = BeanUtil.copyProperties(item, ItemDTO.class);
// 3.将ItemDTO转json
String doc = JSONUtil.toJsonStr(itemDTO);
// 1.准备Request对象
IndexRequest request = new IndexRequest("items").id(itemDTO.getId());
// 2.准备Json文档
request.source(doc, XContentType.JSON);
// 3.发送请求
client.index(request, RequestOptions.DEFAULT);
}
5.2.查询文档
查询文档
我们以根据 id 查询文档为例
语法说明
查询的请求语句如下:
GET /{索引库名}/_doc/{id}
与之前的流程类似,代码大概分 2 步:
- 创建 Request 对象
准备请求参数,这里是无参,直接省略- 发送请求
不过查询的目的是得到结果,解析为 ItemDTO,还要再加一步对结果的解析。示例代码如下:

可以看到,响应结果是一个 JSON,其中文档放在一个 _source
属性中,因此解析就是拿到 _source
,反序列化为 Java 对象即可。
其它代码与之前类似,流程如下:
- 1)准备 Request 对象。这次是查询,所以是
GetRequest
- 2)发送请求,得到结果。因为是查询,这里调用
client.get()
方法 - 3)解析结果,就是对 JSON 做反序列化
代码操作
在 item-service
的 DocumentTest
测试类中,编写单元测试:
@Test
void testGetDocumentById() throws IOException {
// 1.准备Request对象
GetRequest request = new GetRequest("items").id("584394");
// 2.发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 3.获取响应结果中的source
String json = response.getSourceAsString();
ItemDTOES itemDTO = JSONUtil.toBean(json, ItemDTOES.class);
System.out.println("itemDTO = " + itemDTO);
}

5.3.删除文档和修改文档
删除文档和修改文档
删除文档
删除的请求语句如下:
DELETE /hotel/_doc/{id}
与查询相比,仅仅是请求方式从 DELETE
变成 GET
,可以想象 Java 代码应该依然是 2 步走:
- 1)准备 Request 对象,因为是删除,这次是
DeleteRequest
对象。要指定索引库名和 id - 2)
准备参数,无参,直接省略 - 3)发送请求。因为是删除,所以是
client.delete()
方法
在 item-service
的 DocumentTest
测试类中,编写单元测试:
@Test
void testDeleteDocument() throws IOException {
// 1.准备Request,两个参数,第一个是索引库名,第二个是文档id
DeleteRequest request = new DeleteRequest("items", "584394");
// 2.发送请求
client.delete(request, RequestOptions.DEFAULT);
}

修改文档
修改我们讲过两种方式:
- 全量修改:本质是先根据 id 删除,再新增
- 局部修改:修改文档中的指定字段值
在 RestClient 的 API 中,全量修改与新增的 API 完全一致,判断依据是 ID:
- 如果新增时,ID 已经存在,则修改
- 如果新增时,ID 不存在,则新增
这里不再赘述,我们主要关注局部修改的 API 即可。
局部修改的请求语法如下:
POST /{索引库名}/_update/{id}
{
"doc": {
"字段名": "字段值",
"字段名": "字段值"
}
}
代码示例如图:

与之前类似,也是三步走:
- 1)准备
Request
对象。这次是修改,所以是UpdateRequest
- 2)准备参数。也就是 JSON 文档,里面包含要修改的字段
- 3)更新文档。这里调用
client.update()
方法
在 item-service
的 DocumentTest
测试类中,编写单元测试:
@Test
void testUpdateDocument() throws IOException {
// 1.准备Request
UpdateRequest request = new UpdateRequest("items", "100002644680");
// 2.准备请求参数
request.doc(
"price", 58800,
"commentCount", 1
);
// 3.发送请求
client.update(request, RequestOptions.DEFAULT);
}
总结
课堂作业
- 参考上述代码,练习练习🎤
5.5.批量导入文档
批量导入文档
在之前的案例中,我们都是操作单个文档。而数据库中的商品数据实际会达到数十万条,某些项目中可能达到数百万条。
我们如果要将这些数据导入索引库,肯定不能逐条导入,而是采用批处理方案。常见的方案有:
利用 Logstash 批量导入
- 需要安装 Logstash
- 对数据的再加工能力较弱
- 无需编码,但要学习编写 Logstash 导入配置
利用 JavaAPI 批量导入
- 需要编码,但基于 JavaAPI,学习成本低
- 更加灵活,可以任意对数据做再加工处理后写入索引库
接下来,我们就学习下如何利用 JavaAPI 实现批量文档导入。
代码操作
1.语法说明
批处理与前面讲的文档的 CRUD 步骤基本一致:
- 创建 Request,但这次用的是
BulkRequest
- 准备请求参数
- 发送请求,这次要用到
client.bulk()
方法
BulkRequest
本身其实并没有请求参数,其本质就是将多个普通的 CRUD 请求组合在一起发送。例如:
- 批量新增文档,就是给每个文档创建一个
IndexRequest
请求,然后封装到BulkRequest
中,一起发出。 - 批量删除,就是创建 N 个
DeleteRequest
请求,然后封装到BulkRequest
,一起发出
因此 BulkRequest
中提供了 add
方法,用以添加其它 CRUD 的请求:

可以看到,能添加的请求有:
IndexRequest
,也就是新增UpdateRequest
,也就是修改DeleteRequest
,也就是删除
因此 Bulk 中添加了多个 IndexRequest
,就是批量新增功能了。示例:
@Test
void testBulk() throws IOException {
// 1.创建Request
BulkRequest request = new BulkRequest();
// 2.准备请求参数
request.add(new IndexRequest("items").id("1").source("json doc1", XContentType.JSON));
request.add(new IndexRequest("items").id("2").source("json doc2", XContentType.JSON));
// 3.发送请求
client.bulk(request, RequestOptions.DEFAULT);
}
2.完整代码
当我们要导入商品数据时,由于商品数量达到数十万,因此不可能一次性全部导入。建议采用循环遍历方式,每次导入 1000 条左右的数据。
item-service
的 DocumentTest
测试类中,编写单元测试:
@Test
void testLoadItemDocs() throws IOException {
// 分页查询商品数据
int pageNo = 1;
int size = 1000;
while (true) {
Page<Item> page = itemService.lambdaQuery().eq(Item::getStatus, 1).page(new Page<Item>(pageNo, size));
// 非空校验
List<Item> items = page.getRecords();
if (CollUtils.isEmpty(items)) {
return;
}
log.info("加载第{}页数据,共{}条", pageNo, items.size());
// 1.创建Request
BulkRequest request = new BulkRequest("items");
// 2.准备参数,添加多个新增的Request
for (Item item : items) {
// 2.1.转换为文档类型ItemDTO
ItemDTO itemDTO = BeanUtil.copyProperties(item, ItemDTO.class);
// 2.2.创建新增文档的Request对象
request.add(new IndexRequest()
.id(itemDTO.getId())
.source(JSONUtil.toJsonStr(itemDTO), XContentType.JSON));
}
// 3.发送请求
client.bulk(request, RequestOptions.DEFAULT);
// 翻页
pageNo++;
}
}
总结
文档操作的基本步骤:
初始化
RestHighLevelClient
创建 XxxRequest。
- XXX 是
Index
、Get
、Update
、Delete
、Bulk
- XXX 是
准备参数(
Index
、Update
、Bulk
时需要)发送请求。
- 调用
RestHighLevelClient#.xxx()
方法,xxx 是index
、get
、update
、delete
、bulk
- 调用
解析结果(
Get
时需要)
课堂作业
- 参考上述步骤练习练习🎤
6.作业
6.1.服务拆分
搜索业务并发压力可能会比较高,目前与商品服务在一起,不方便后期优化。
需求:创建一个新的微服务,命名为 search-service
,将搜索相关功能抽取到这个微服务中
6.2.商品查询接口
在 item-service
服务中提供一个根据 id 查询商品的功能,并编写对应的 FeignClient