索引别名类似代理一样
GET _cat/aliases?v
单索引单别名绑定解绑
案例:假定公司基本信息索引需要变更
PUT ckiss-company-002
{
"aliases": {
"ckiss-company": {}
},
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"companyName": {
"type": "keyword"
}
}
}
}
PUT ckiss-company-002/_doc/1
{
"companyId": 1,
"companyName": "美团集团"
}
PUT ckiss-company-002/_doc/2
{
"companyId": 2,
"companyName": "美团集团"
}
# 删除
DELETE ckiss-company-002/_alias/ckiss-company
#设置
PUT ckiss-company-002/_alias/ckiss-company
# 查看
GET ckiss-company-002/_alias
# 修改
POST _aliases
{
"actions": [
{
"remove": {
"index": "ckiss-company-002",
"alias": "ckiss-company"
}
},
{
"add": {
"index": "ckiss-company-002",
"alias": "ckiss-company-fianl"
}
}
]
}
索引别名不支持直接删除
filter别名查询过滤
案例:假设公司员工信息很多,按公司编号去做一些隔离查询
DELETE company-staff-001
PUT company-staff-001
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"userId": {
"type": "keyword"
},
"userName":{
"type": "keyword"
}
}
}
}
PUT company-staff-001/_doc/1
{
"companyId": 1,
"userId": "111",
"userName": "lisi"
}
PUT company-staff-001/_doc/2
{
"companyId": 1,
"userId": "121",
"userName": "zhangs"
}
PUT company-staff-001/_alias/company-staff
{
"filter": {
"term": {
"userId": "121"
}
}
}
GET company-staff/_search
{}
"hits" : [
{
"_index" : "company-staff-001",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"companyId" : 1,
"userId" : "121",
"userName" : "zhangs"
}
}
]
routing 路由控制
假设公司员工信息很多,按照公司编号做一些隔离查询
DELETE company-staff-001
PUT company-staff-001
{
"settings": {
"number_of_shards": 3
},
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"userId": {
"type": "keyword"
},
"userName":{
"type": "keyword"
}
}
}
}
#指定路由
PUT company-staff-001/_doc/1?routing=1
{
"companyId": 1,
"userId": "111",
"userName": "员工111"
}
PUT company-staff-001/_doc/2
{
"companyId": 2,
"userId": "121",
"userName": "员工121"
}
PUT company-staff-001/_doc/3
{
"companyId": 3,
"userId": "123",
"userName": "员工123"
}
GET company-staff-001/_search
{}
# 建立别名时候指定路由过滤数据
PUT company-staff-001/_alias/company-staff
{
"routing": "1"
}
is_write_index 读写控制
is_write_index
读写关键字- 创建一个索引读别名
- 创建一个索引写别名
is_write_index
设置了此索引是可以通过此别名被写入的索引
# 创建索引
PUT company-staff-002
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"userId": {
"type": "keyword"
},
"userName":{
"type": "keyword"
}
}
}
}
# 获取别名
GET company-staff-001/_alias
# 删除已经建立的别名
DELETE company-staff-001/_alias/company-staff
#设置读写属性
POST /_aliases
{
"actions": [
{
"add": {
"index": "company-staff-001",
"alias": "company-staff",
"is_write_index": false
}
},
{
"add": {
"index": "company-staff-001",
"alias": "company-staff",
"is_write_index": true
}
}
]
}
alias 批量操作api
- actions : 别名操作关键字
- add:新增别名操作
- remove:删除别名操作
- is_write_index: 读写分离机设置
- search_routing: 查询路由字段,路由值随意设置
- index_routing: 写入路由字段
索引模板
索引模板创建
- _template: 模板关键字,遗留版本
- _index_template:模板关键字,新版本
- index_patterns:索引模板匹配关键字,索引匹配可指定前缀和后缀
案例:公司基本信息定期创建新索引,按照年份月份创建,限制动态增加索引字段,新增的字段仅仅保留元数据,设置索引别名,设置字段数量与属性,设置索引分片与副本。
dynamic
- true:遇到陌生字段,就进行dynamic mapping
- false:遇到陌生字段,就忽略
- strict:遇到陌生字段,就报错 ```json GET _template GET _cat/templates?v
DELETE _template/ckiss-company-tp01
新建模板
PUT _template/ckiss-company-tp01 { “index_patterns”: [ “ckiss-company-2022“, “company-“ ], “settings”: { “number_of_shards”: 3, “number_of_replicas”: 1, “refresh_interval”: “15s” }, “mappings”: { “_source”: { “enabled”: true }, “properties”: { “companyId”: { “type”: “integer” }, “userId”: { “type”: “keyword” }, “userName”: { “type”: “keyword” } }, “dynamic”: false }, “aliases”: { “ckiss-company”: {} } }
PUT ckiss-company-2022-01/_create/1 { “companyId”: 1, “userId”: “111”, “userName”: “员工111” }
PUT ckiss-company-2022-01/_create/2 { “companyId”: 2, “userId”: “112”, “userName”: “员工112”, “area”: { “province”: “HB” } }
检索
GET ckiss-company-2022-01/_mapping GET ckiss-company/_search GET _template/ckiss-company-tp01
<a name="qPVH7"></a>
#### 组件式索引模板:`_component_template`
- `_component_template`:组件模板关键字
- 与索引模板创建语法类似,不能指定匹配规则,需要与索引模板一起使用
- `composed_of`: 索引模板组合多个组件模板参数
> 假定公司基本信息包括 区域信息;员工信息也包括区域信息,可以将区域信息独立 出来设计,需要的时候组合在 一起
```json
GET _component_template
PUT _component_template/area_tp01
{
"template": {
"mappings": {
"properties": {
"area":{
"properties": {
"province":{
"type": "keyword"
},
"city":{
"type": "keyword"
},
"country": {
"type": "keyword"
}
}
}
}
}
}
}
GET _index_template
GET _cat/templates
DELETE _index_template/company_tp30
PUT _index_template/company_tp30
{
"index_patterns": [
"company-20*",
"company-*"
],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"refresh_interval": "1s"
}
},
"priority": 30,
"composed_of": [
"area_tp01"
]
}
# 数据
DELETE company-2022
PUT company-2022/_create/1
{
"companyId": 1,
"companyName": "华住集团",
"area": {
"province": "HB",
"city": "WH",
"county": "HS"
}
}
GET company-2022
根据别名索引不停机重建
# 1.index_old取一个别名index_service,index_service对外提供服务
POST /_aliases
{
"actions": [
{
"add": {
"index": "index_old",
"alias": "index_service"
}
}
]
}
# 2.新创建一个索引index_new,数据结构与index_old一样,但是name字段的类型为keyword
PUT index_new
{
"mappings": {
"_doc": {
"properties": {
"id": {"type": "long"},
"name": {"type": "keyword"}
}
}
}
}
# 3.将index_old的数据同步到index_new
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "index_old"
},
"dest": {
"index": "index_new"
}
}
# wait_for_completion=true,同步执行,会一直等待直到同步完成或者报错,默认
# wait_for_completion=false,异步执行,返回taskId,然后后台进行同步,数据量大的情况下建议使用
# 4.将index_old的别名删除,index_new的别名设置为index_service
POST /_aliases
{
"actions": [
{
"remove": {
"index": "index_old",
"alias": "index_service"
}
},
{
"add": {
"index": "index_new",
"alias": "index_service"
}
}
]
}
# 5.删除index_old
DELETE /index_old