博客 ES学习看这一篇文章就够了2

ES学习看这一篇文章就够了2

   数栈君   发表于 2023-07-21 10:21  445  0

第四章 基本用法
第1节 ES 的 RESTFul API
API的基本格式
1
http://<ip>:<port>/<索引>/<类型>/<文档id&gt;
常用的HTTP动作
1
GET/POST/PUT/DELETE
第2节 创建索引
2.1 使用Head插件创建索引

创建索引的步骤
1
2
3
4
5
1. 登陆Head页面
2. 找到索引标签
3. 新建索引
4. 添加索引名称(英文,全部都是小写字母,不能用下划线开头)
5. 索引创建成功之后回到概览页面就会在上面发现我们新建的索引
Head页面展示




2.2 结构化索引/非结构化索引

结构化和非结构化介绍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

进入我们的Head界面,进入概览,点击索引名称下面的信息,点击索引信息,查看数据里面有一个mappings属性,内容为空,说明我们此索引为非结构化索引

{
"state": "open",
"settings": {
"index": {
"creation_date": "1597547149325",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "UvYCPtp0TZeCiPexCT517g",
"version": {
"created": "6030299"
},
"provided_name": "book"
}
},
"mappings": {},
"aliases": [],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"BHBNGrGCSiOl-wuKptvGtQ"
],
"1": [
"yMyL9GsMR9SziVr9OK_Uxw"
],
"2": [
"b8EGEtGlTySAqvlKPtW_EA"
],
"3": [
"0azU6MwTSCqMdzVXyuPVrw"
],
"4": [
"DY2oxeUuSNaxva5dgKTSAg"
]
}
}

结构化和非结构化转化
1
2
1、在当前的head页面中点击【复合查询】
2、调用ES的API构建一个请求url
1
我这里给book索引添加一个author类型,使用_mappings关键字(ES给索引、类型、文档等起名字时不能以下划线开头,因为ES中很多关键字都是以下划线开头的)




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

3、给上面的非结构化索引添加一些结构化的数据,让他变成结构化索引

点击易读,点击验证json,防止自己写的json数据有问题,点击提交请求

properties: 关键字,设置属性
type: 关键字,设置属性类型
{
"author": {
"properties": {
"author_name": {
"type": "text"
}
}
}
}

4、添加成功标志

{
"acknowledged": true
}

5、在进入概览点击索引信息查看数据(mappings中就有了机构化数据)

{
"state": "open",
"settings": {
"index": {
"creation_date": "1597547149325",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "UvYCPtp0TZeCiPexCT517g",
"version": {
"created": "6030299"
},
"provided_name": "book"
}
},
"mappings": {
"author": {
"properties": {
"author_name": {
"type": "text"
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"BHBNGrGCSiOl-wuKptvGtQ"
],
"1": [
"yMyL9GsMR9SziVr9OK_Uxw"
],
"2": [
"b8EGEtGlTySAqvlKPtW_EA"
],
"3": [
"0azU6MwTSCqMdzVXyuPVrw"
],
"4": [
"DY2oxeUuSNaxva5dgKTSAg"
]
}
}

2.3 使用postman创建索引

1
Head在进行结构化数据创建的时候,验证json数据格式比较麻烦,我们可以使用postman进行结构化数据创建
使用postman创建索引




postman创建索引的请求数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

{
"settings":{
"number_of_shards":5,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
}
}

-----------------------------参数变量的简单介绍----------------------------------

settings : 设置当前索引的基本信息
mappings : 结构化索引配置
format : 时间格式
epoch_millis : 时间戳(毫秒数)

head页面查看创建结果




第3节 数据插入(postman)
3.1 指定id插入

请求地址
1
http://localhost:9200/people/man/1
请求图例




添加数据
1
2
3
4
5
6
{
"name":"1990",
"country":"中国",
"age":30,
"date":"1990-05-28"
}
返回结果数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
head查看








3.2 自动生成id插入

请求地址
1
http://localhost:9200/people/man/
请求图例




head查看




第4节 数据修改(postman)
4.1 直接修改文档

请求地址
1
http://localhost:9200/people/man/1/_update
请求参数
1
2
3
4
5
{
"doc":{
"name":"1990"
}
}
请求图例




响应数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
head查看




4.2 使用脚本修改文档(了解)

4.2.1 Painless简介

1
2
1. Painless 是默认情况下 Elasticsearch 中提供的一种简单,安全的脚本语言
2. 它专门设计用于 Elasticsearch,可以安全地使用内联和存储的脚本,默认情况下启用
4.2.2 Painless使用

请求地址
1
http://localhost:9200/people/man/1/_update
请求参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

方式一:

{
"script":{
"lang":"painless",
"inline":"ctx._source.age -=10"
}
}


方式二:
{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age",
"params":{
"age":21
}
}
}

请求图例




第5节 数据删除(postman)
5.1 删除文档(postman)

请求地址
1
http://localhost:9200/people/man/1
请求图例




5.2 删除索引(postman)

请求地址
1
http://localhost:9200/people
请求图例




5.3 使用head删除索引

图例演示


免责申明:

本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!

《数据治理行业实践白皮书》下载地址:https://fs80.cn/4w2atu

《数栈V6.0产品白皮书》下载地址:
https://fs80.cn/cw0iw1

想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:
https://www.dtstack.com/?src=bbs

同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:
https://github.com/DTStack

0条评论
社区公告
  • 大数据领域最专业的产品&技术交流社区,专注于探讨与分享大数据领域有趣又火热的信息,专业又专注的数据人园地

最新活动更多
微信扫码获取数字化转型资料
钉钉扫码加入技术交流群