ES索引库的维护
# 30.ES索引库的维护
接下来我们就开始使用ES,来演示如何创建和维护索引。
实际开发中,主要有三种方式可以作为 ES 服务的客户端:
- 使用ES提供的Restful接口直接访问
- elasticsearch-head插件
- 使用ES提供的API进行访问
# 使用Restful接口直接访问
例如,我们可以在命令行里直接发送HTTP请求,假设使用curl工具:
curl ‐X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' ‐d '<BODY>'
其中:
参数 | 解释 |
---|---|
VERB | 适当的 HTTP 方法 或 谓词 : GET 、POST 、PUT 、HEAD 或者 DELETE 。 |
PROTOCOL | http 或者 https (如果你在 Elasticsearch 前面有一个 https 代理) |
HOST | Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。 |
PORT | 运行 Elasticsearch HTTP 服务的端口号,默认是 9200 |
PATH | API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例 如: _cluster/stats 和 _nodes/stats/jvm |
QUERY_STRING | 任意可选的查询字符串参数,例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读) |
BODY | 一个 JSON 格式的请求体 (如果请求需要的话) |
由于使用命令行挺麻烦的,要敲的命令挺长,这里不演示
# 使用head插件
我们点击到索引的选项卡,然后点击新建,填写索引相关信息:
(https://image.peterjxl.com/blog/image-20230516221831-p8rf7qi.png)
点击OK,会有弹框提示我们创建成功了:
(https://image.peterjxl.com/blog/image-20230516221945-pn806cg.png)
在概览也能看到有5个分片,1个复制
(https://image.peterjxl.com/blog/image-20230516222024-3kbj1nj.png)
也可以删除某个索引库:
(https://image.peterjxl.com/blog/image-20230517074315-1j8kxfj.png)
使用该插件,一些简单的操作是可以用,但是对于稍微复杂的就不太合适了,我们一般是使用postman。
# 安装postman
postman是一款强大的网页调试工具。并且带有客户端,提供功能强大的Web API 和 HTTP 请求调试。软件功能非常强大,免费,界面简洁明晰、操作方便快捷,设计得很人性化。
postman能够发送任何类型的HTTP 请求 (GET, HEAD, POST, PUT..),且可以附带任何数量的参数,在开发中经常能用到。
官网:Postman (opens new window),我们选择对应的平台下载:
(https://image.peterjxl.com/blog/image-20230516222611-1uixiuf.png)
或去我的百度云网盘 (opens new window)下载,路径为编程资料/其他开发工具/Postman-win64-Setup.exe
,安装后需要注册和登录才能使用。
我们先创建一个Collections(可以理解为一个项目)
(https://image.peterjxl.com/blog/image-20230516223113-ug35u9d.png)
然后添加一个请求(可以理解为创建一个类)
(https://image.peterjxl.com/blog/image-20230516223025-ro13d1x.png)
然后我们就可以在输入框里输入网址,发送一个GET请求了:
(https://image.peterjxl.com/blog/image-20230516223043-g4m6jyp.png)
注意:随着postman的更新,界面可能有点不同,具体以官网文档为准。
# 新建索引库
# 直接创建
假设我们新建一个索引库,只需选择PUT方式,在URL后面加上索引库名字即可,例如:
(https://image.peterjxl.com/blog/image-20230517070808-lyhlv8s.png)
使用head插件也能看到创建好的索引库:
(https://image.peterjxl.com/blog/image-20230517070949-ejeg8t9.png)
我们可以查看下索引的信息:
(https://image.peterjxl.com/blog/image-20230517071038-96i5g8a.png)
可以看到mappings是空的。
(https://image.peterjxl.com/blog/image-20230517071105-vl0owg6.png)
# 指定mappings创建索引库
我们可以在创建索引库的时候就带上mappings信息,或者创建好后补上也行。我们演示下创建时定义mappings。首先是定义JSON:
{
"mappings": {
"article": {
"properties": {
"id": {
"type": "long",
"store": true,
"index": "not_analyzed"
},
"title": {
"type": "text",
"store": true,
"analyzer": "standard",
"index": "analyzed"
},
"content": {
"type": "text",
"store": true,
"analyzer": "standard",
"index": "analyzed"
}
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上面我们相当于定义了一个表article,里面有3个字段:id,title,content,每个字段的类型、是否存储、使用什么分析器和是否索引,都指定了。
回到postman,我们输入上述的JSON,然后点击发送:
(https://image.peterjxl.com/blog/image-20230517072337-rmj5ku0.png)
响应结果:创建成功了
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "blog1"
}
2
3
4
5
访问head插件,查看blog1索引库的索引信息,也能看到mappings:
(https://image.peterjxl.com/blog/image-20230517072430-zl15rvo.png)
# 创建索引后设置Mapping
假设我们给blog这个索引库添加mappings。首先是请求的URL:
POST http://127.0.0.1:9200/blog/hello/_mappings
使用post方式,然后后面跟着索引库名,然后是要新建的表名,然后是要修改的信息mappings
JSON信息:
{
"hello": {
"properties": {
"id": {
"type": "long",
"store": true,
"index": "not_analyzed"
},
"title": {
"type": "text",
"store": true,
"analyzer": "standard",
"index": "analyzed"
},
"content": {
"type": "text",
"store": true,
"analyzer": "standard",
"index": "analyzed"
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
然后我们点击发送:
(https://image.peterjxl.com/blog/image-20230517073005-v27rd89.png)
返回结果:
{
"acknowledged": true
}
2
3
在head插件中也能看到mappings信息:
(https://image.peterjxl.com/blog/image-20230517073042-i143vf0.png)
# 删除索引库
请求url:
DELETE localhost:9200/索引库名
在postman中,我们选择删除,然后发送
(https://image.peterjxl.com/blog/image-20230517074554-rxjct2g.png)
响应结果:
{
"acknowledged": true
}
2
3
同时在head插件中也看不到该索引库了
# 创建document
请求url:索引库/type/文档的id,注意该id是真正的id,不是JSON中的id属性,JSON中的数据只是碰巧同名
POST localhost:9200/blog1/article/1
请求体:
{
"id": 1,
"title": "ElasticSearch是一个基于Lucene的搜索服务器",
"content": "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java 开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时 搜索,稳定,可靠,快速,安装使用方便。"
}
2
3
4
5
postman截图:
(https://image.peterjxl.com/blog/image-20230517075925-1buito7.png)
响应结果:创建成功了
{
"_index": "blog",
"_type": "hello",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
2
3
4
5
6
7
8
9
10
11
12
13
在head插件也能查看到数据:
(https://image.peterjxl.com/blog/image-20230517075531-botg4zp.png)
注意 _id是ES帮我们给每个document生成的,而我们自己也刚好定义了一个id属性。一般两者保持一致
我们在创建document时也可以不指定ID,也能正常生成:
(https://image.peterjxl.com/blog/image-20230517075813-f8j63zb.png)
响应结果:此时的_id类似UUID,随机生成的,没什么规律
{
"_index": "blog",
"_type": "hello",
"_id": "AYgm_-pEoyq3JE_fvM1J",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
2
3
4
5
6
7
8
9
10
11
12
13
也可以在head插件中看到:
(https://image.peterjxl.com/blog/image-20230517080410-qrg8snz.png)
# 删除document
请求url:
DELETE localhost:9200/blog/hello/1
postman截图:选择Delete,然后是索引库/type/_id,注意是 _id,这个才是每条记录的ID
(https://image.peterjxl.com/blog/image-20230517081215-unzsf0i.png)
响应结果:
{
"found": true,
"_index": "blog",
"_type": "hello",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
2
3
4
5
6
7
8
9
10
11
12
13
在head插件中,也没看到这个数据了:
(https://image.peterjxl.com/blog/image-20230517195601-51omaj0.png)
# 修改document
和Lucene一样,ES修改文档的原理也是先删除然后添加。请求url举例:
POST localhost:9200/blog/hello/1
可以看到和创建索引的URL是一样的。如果没有这个文档,就会新建;有的话就删除后创建。
我们先创建下 id为1的文档:
{
"id": 1,
"title": "新添加的文档1",
"content": "新添加的文档的内容"
}
2
3
4
5
postman截图:
(https://image.peterjxl.com/blog/image-20230517200817-ennck9y.png)
能看到创建成功了:
(https://image.peterjxl.com/blog/image-20230517200838-46fgzs2.png)
然后我们更新:
{
"id": 123,
"title": "修改之后的文档1",
"content": "新添加的文档的内容"
}
2
3
4
5
(https://image.peterjxl.com/blog/image-20230517201010-ddlvyyj.png)
能看到数据确实被更新了:
(https://image.peterjxl.com/blog/image-20230517201019-t8ioljm.png)
# head插件中发送HTTP请求
head插件也提供了类似postman的功能:这里就不一一演示了
(https://image.peterjxl.com/blog/image-20230517074736-mnqqlk6.png)
# 总结
本文我们介绍了如何维护索引库,接下来我们学习下如何查询数据。