目录
一、参考资料
参考文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
这里建议入门之后,看看官方文档,因版本号不同,有些地方与自己学习的资料有所出入。
本文内容,基于ElasticSearch7.11版本。
二、索引(Index)
1. 创建索引
(1)文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
(2)请求方式
PUT /<index>
(3)参数解析
- 路径参数
参数名称 | 参数类型 | 参数说明 |
<index> | string | 1. 只允许小写字符 2. 不允许包括字符('\', '/', '*', '?', '''', '', '|', ' ', ',', '#') 3. 7.0+版本不支持冒号':' 4. 不能以字符('-', '_', '+')开头 5. 不能只有'.'或'..' 6. 不能超过255个字节 7. 不推荐以'.'作为开头,除非是插件管理的隐藏索引和内部索引 |
- 查询参数
参数名称 | 参数类型 | 参数说明 |
include_type_name | boolean | 是否包含type名称,默认false |
wait_for_active_shards | string | 设置分片数量,默认1 |
master_timeout | time units | 连接到主节点的超时时长,超过时返回错误,默认30秒 |
timeout | time units | 等待回应的时长,超过时返回错误,默认30s |
- 请求体参数
参数名称 | 参数类型 | 参数说明 |
aliases | alias object | 索引别名 |
mappings | mapping object | 索引映射字段,可指定字段: 1. 字段名称 2. 字段数据类型 3. mapping 参数 |
settings | index setting object | 索引配置 |
(4)返回值解析
{
"acknowledged": true, // 索引是否在集群中成功创建
"shards_acknowledged": true, // 超时之前是否已经为索引的每个分片启动了分片副本
"index": "test" // 操作的索引库名称
}
(5)创建索引例子
- 创建一个名称为“stu-class”,分片5,副本1的索引
- 设置type名称“student”
- 映射字段“id”、“name”、“sex”、“birthday”
【请求路径 POST】
127.0.0.1:9200/stu-class?include_type_name=true
【请求体参数】
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"student": {
"properties": {
"id": {
"type": "long",
"store": true
},
"name": {
"type": "text",
"store": true
},
"sex": {
"type": "text",
"store": true
},
"birthday": {
"type": "text",
"store": true
}
}
}
}
}
【返回值】
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "stu-class"
}
创建结果:
2. 获取索引
(1)文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html
(2)请求方式
GET /<target>
(3)参数解析
- 路径参数
参数名称 | 参数类型 | 参数说明 |
<target> | string | 索引名称,需要查询所有索引时,填写"_all"或"*" |
- 查询参数
参数名称 | 参数类型 | 参数说明 |
allow_no_indices | boolean | 当使用通配符查询时,如果索引不存在是否返回查询失败,默认true |
expand_wildcards | string | 通配符可以匹配的索引类型: 1. all 2. open 3. closed 4. hidden 5. none 默认open |
flat_settings | boolean | 是否扁平化格式返回,默认false |
include_defaults | boolean | 是否返回集群所有默认设置,默认 |
include_type_name | boolean | 是否包含类型名称,默认false |
ignore_unavailable | boolean | 是否忽略不可用的索引,默认false |
local | boolean | 请求是否仅从本地节点检索信息,默认false,表示从主节点检索信息 |
master_timeout | time units | 等待连接到主节点的时间,默认30秒 |
(4)获取索引例子
- 查询名称为“stu-class”的索引
【请求路径 GET】
127.0.0.1:9200/stu-class
【返回值】
{
"stu-class": {
"aliases": {},
"mappings": {
"properties": {
"birthday": {
"type": "text",
"store": true
},
"id": {
"type": "long",
"store": true
},
"name": {
"type": "text",
"store": true
},
"sex": {
"type": "text",
"store": true
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "5",
"provided_name": "stu-class",
"creation_date": "1615525440480",
"number_of_replicas": "1",
"uuid": "XimbAp1SRM-NrMvgxhNcHw",
"version": {
"created": "7110199"
}
}
}
}
}
3. 删除索引
(1)文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
(2)请求方式
DELETE /<index>
(3)参数解析
- 路径参数
参数名称 | 参数类型 | 参数说明 |
<index> | string | 索引名称,如果需要删除全部索引,可以使用"_all"或者"*" |
- 查询参数
参数名称 | 参数类型 | 参数说明 |
allow_no_indices | boolean | 当使用通配符查询时,如果索引不存在是否返回查询失败,默认true |
expand_wildcards | string | 通配符可以匹配的索引类型: 1. all 2. open 3. closed 4. hidden 5. none 默认open |
ignore_unavailable | boolean | 是否忽略不可用的索引,默认false |
mster_timeout | time units | 等待连接到主节点的时间,默认30秒 |
timeout | time units | 等待响应的时长,默认30秒 |
(4)删除索引列子
- 删除名称为“stu-class”的索引
【请求路径 DELETE】
127.0.0.1:9200/stu-class
【返回值】
{
"acknowledged": true
}
三、映射(Mappings)
1. 设置映射
(1)文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
(2)请求方式
PUT /<target>/_mapping
PUT /_mapping
(3)参数解析
- 路径参数
参数名称 | 参数类型 | 参数说明 |
<target> | string | 索引名称,匹配全部可以用"_all"或者"*" |
- 查询参数
参数名称 | 参数类型 | 参数说明 |
allow_no_indices | boolean | 当使用通配符查询时,如果索引不存在是否返回查询失败,默认true |
expand_wildcards | string | 通配符可以匹配的索引类型: 1. all 2. open 3. closed 4. hidden 5. none 默认open |
include_type_name | boolean | 是否包含类型名称,默认false |
ignore_unavailable | boolean | 是否忽略不可用的索引,默认false |
master_timeout | time units | 等待连接到主节点的时间,默认30秒 |
timeout | time units | 等待响应的时长,默认30秒 |
write_index_only | boolean | 映射是否仅应用于目标的当前索引,默认false |
- 请求体参数
参数名称 | 参数类型 | 参数说明 |
properties | mapping object | 索引映射字段,可指定字段: 1. 字段名称 2. 字段数据类型 3. mapping 参数 |
(4)设置映射例子
- 将索引“stu-class”的映射“birthday”改成“birth”
【请求路径 PUT】
127.0.0.1:9200/stu-class/_mapping
【请求参数】
{
"properties": {
"birth": {
"type": "text",
"store": true
},
"id": {
"type": "long",
"store": true
},
"name": {
"type": "text",
"store": true
},
"sex": {
"type": "text",
"store": true
}
}
}
【返回值】
{
"acknowledged": true
}
2. 获取映射
(1)文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
(2)请求方式
GET /_mapping
GET /<target>/_mapping
(3)参数解析
- 路径参数
参数名称 | 参数类型 | 参数说明 |
<target> | string | 索引名称,匹配全部可以用"_all"或者"*" |
- 查询参数
参数名称 | 参数类型 | 参数说明 |
allow_no_indices | boolean | 当使用通配符查询时,如果索引不存在是否返回查询失败,默认true |
expand_wildcards | string | 通配符可以匹配的索引类型: 1. all 2. open 3. closed 4. hidden 5. none 默认open |
include_type_name | boolean | 是否包含类型名称,默认false |
ignore_unavailable | boolean | 是否忽略不可用的索引,默认false |
local | boolean | 请求是否仅从本地节点检索信息,默认false,表示从主节点检索信息 |
master_timeout | time units | 等待连接到主节点的时间,默认30秒 |
(4)设置映射例子
- 获取名称为“stu-class”的索引映射
【请求路径 GET】
127.0.0.1:9200/stu-class/_mapping
【返回值】
{
"stu-class": {
"mappings": {
"properties": {
"birth": {
"type": "text",
"store": true
},
"birthday": {
"type": "text",
"store": true
},
"id": {
"type": "long",
"store": true
},
"name": {
"type": "text",
"store": true
},
"sex": {
"type": "text",
"store": true
}
}
}
}
}