# Elastic Search基础操作
# ES结构
# 索引index的分片与备份
- ES服务中可以创建多个索引
- 每个索引默认默认分为5片存储
- 每个分片都会存在至少一个备份分片
- 备份分片默认不会分担检索数据,但当检索压力过大时会参与分担检索数据
- 备份分片必须放在不同的服务器中
# Type类型
- 一个索引可以创建多个类型(版本不同,类型创建也不同)
- ES5.x版本中,一个index可以创建多个Type(学习版本)
- ES6.x版本中,一个index可以创建一个Type
- ES7.x版本中,取消了Type
# 文档Doc和属性Field
- 一个类型下,可以有多个文档。(多个文档类比Mysql表中的多行数据)
- 一个文档中可以包含多个属性。(多个属性类比Mysql表中的多个字段)
# 操作ES的RESTful语法
- GET请求
- http://cmcc.liudongyang.top:9200/index : 查询索引信息
- http://cmcc.liudongyang.top:9200/index/type/doc_id : 查询指定文档信息
- POST请求
- http://cmcc.liudongyang.top:9200/index/type/_search : 查询文档,可以在请求体中添加json字符串来代表查询条件
- http://cmcc.liudongyang.top:9200/index/doc_id/_update : 修改文档,在请求体中指定json字符串代表修改的具体信息
- PUT请求
- http://cmcc.liudongyang.top:9200/index : 创建一个索引,需要在请求体中指定索引的信息,类型,结构
- http://cmcc.liudongyang.top:9200/index/type/_mapping : 代表创建索引时指定索引文档存储的属性信息
- DELETE请求
- http://cmcc.liudongyang.top:9200/index : 删除索引
- http://cmcc.liudongyang.top:9200/index/type/doc_id : 删除指定的文档
# 索引的基础操作
- 创建一个索引 (PUT /person) 请求及响应
- 请求
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
1
2
3
4
5
6
2
3
4
5
6
- 响应
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "persion"
}
1
2
3
4
5
2
3
4
5
- 查看一个索引 (GET /person) 请求及响应
- 响应
{
"persion" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1624416095748",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "A2aW5ieDTO61iXubn40FjA",
"version" : {
"created" : "6050499"
},
"provided_name" : "persion"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 删除一个索引 (DELETE /person) 请求及响应
- 响应
{
"acknowledged" : true
}
1
2
3
2
3
# ES中Field可以指定的类型
- 字符串类型:
- text: 一般被用于全文检索,将当前Field进行分词。
- keyword: 当前Field不会被分词。
- 数值类型:
- long: -2的63次方到2的63次方减1,占用8个人字节
- Integer: -2的31次方到2的31次方减1,占用4个字节
- short: -2的15次方到2的15次方减1,占用2个字节
- byte: -2的7次方到2的7次方减1,占用1个字节
- double: e乘以10的正308次方和负324次方之间,占用8个字节
- float: e乘以10的正38次方和负45次方之间,占用4个字节
- half_float: 精度比float小一半
- scaled_float: 根据一个long和scaled来表达一个浮点型,long-345,scaled-100->3.45
- 时间类型:
- date类型: 指定具体时间格式的时间类型
- 布尔类型:
- booble类型: true/false
- 二进制类型:
- binary类型暂时支持Base64 encode string
- 范围类型:
- long_range: 赋值时无需指定具体内容,只需要存储一个范围即可,指定gt,lt,gte,lte
- integer_range: 同上
- double_range: 同上
- double_range: 同上
- float_range: 同上
- date_range: 同上
- ip_range: 同上
- 经纬度类型:
- geo_point: 用来存储经纬度,适用于快递,外卖等场景
- ip类型:
- ip: 可以存储IPv4或者IPv6信息
- 参考官方文档地址: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/mapping-types.html
- 指定数据结构创建索引 (PUT /book) 请求和响应
- 请求
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"novel": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"index": true,
"store": false
},
"author": {
"type": "keyword"
},
"count":{
"type": "long"
},
"on-sale": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"descr":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
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
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": {
# 类型(type)
"novel": {
# 文档存储的Field
"properties": {
# Field属性名
"name": {
# 类型
"type": "text",
# 指定分词器
"analyzer": "ik_max_word",
# 指定当前Field可以被作为查询的条件
"index": true,
# 是否需要额外存储
"store": false
},
"author": {
"type": "keyword"
},
"count":{
"type": "long"
},
"on-sale": {
"type": "date",
# 时间类型的格式化方式
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"descr":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
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
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
- 响应
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "book"
}
1
2
3
4
5
2
3
4
5
# 文档的操作
- 文档在ES服务器中的唯一标识依赖_index,_type,_id三个内容组合确定。
- 新增文档 (POST /book/novel) 自动生成_id 请求和响应
- 请求
{
"name": "西游记",
"author": "吴承恩",
"count": 1000000,
"on-sale": "2000-01-10",
"descr": "九九八十一难"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 响应
{
"_index" : "book",
"_type" : "novel",
"_id" : "ftqNN3oBwiPJbe8gGw2O",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 新增文档 (POST /book/novel/1) 手动生成_id 请求和响应
- 请求
{
"name": "红楼梦",
"author": "施耐庵",
"count": 1000001,
"on-sale": "2001-01-10",
"descr": "刘姥姥进大观园"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 响应
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 修改文档
覆盖方式修改 (PUT /book/novel/1) 请求和响应
请求
{
"name": "红楼梦",
"author": "曹雪芹",
"count": 123456789,
"on-sale": "2011-12-13",
"descr": "王姥姥进大观园"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
- 响应
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
doc修改方式,指定修改的字段和具体值即可 (POST /book/novel/1/_update) 请求和响应
请求
{
"doc": {
"descr": "刘姥姥进大观园"
}
}
1
2
3
4
5
2
3
4
5
- 响应
{
"_index" : "book",
"_type" : "novel",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 删除文档
- 根据id删除 (DELETE /book/novel/1)