Elasticsearch实战——索引管理
文章目录
1. 索引创建
PUT index
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"title":{
"type":"text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
},
"tag":{
"type":"keyword"
}
}
}
}
2. 添加字段
PUT index/_mapping
{
"properties":{
"indexAt":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
}
}
}
修改索引刷新时间
PUT index/_settings
{
"refresh_interval": "120s"
}
3. 删除索引
3.1 删除索引
DELETE index
3.2 查询删除
POST index/_delete_by_query
{
"query":{
}
}
4. 打开/关闭索引
4.1 打开索引
POST index/_open
4.2 关闭索引
POST index/_close
5. 重建索引
POST /_reindex
{
"source":{
"index":"sourceIndexName",
"query":{
},
"sort":{
"fieldName":"asc/desc"
},
"size":5000,
"_source":[
"fieldName1",
"fieldName2",
"fieldName3",
]
},
"dest":{
"index":"destIndexName",
"version_type":"internal(default)/external",
"op_type":"create"
},
"script": {
"inline":"ctx._routing = ctx._source.province_id",
"lang": "painless"
}
}
说明:
version_type
internal
:表示迁移全部数据,且完全覆盖冲突文档(即使目标文档版本新于源索引文档)。external
:表示迁移全部数据,且更新旧版本冲突文档。
op_type
:create
:表示仅创建目标索引不存在的文档。
script
:脚本,指定routing
6. 别名管理
6.1 添加别名
POST /_aliases
{
"actions":[
{
"add":{
"index":"indexName1",
"alias":"aliasName"
}
},
{
"add":{
"index":"indexName2",
"alias":"aliasName"
}
}
]
}
6.2 删除别名
POST /_aliases
{
"actions":[
{
"remove":{
"index":"indexName",
"alias":"aliasName"
}
}
]
}
6.3 重命名
POST /_aliases
{
"actions":[
{
"remove":{
"index":"indexName",
"alias":"aliasName1"
}
},
{
"add":{
"index":"indexName",
"alias":"aliasName2"
}
}
]
}
7. 合并索引
POST index/_forcemerge?max_num_segments=1
8. 缩小索引
8.1 先把索引设置为只读
PUT source_index/_setting
{
"settings":{
"index.routing.allocation.require._name":"node-1",
"index.blocks.write":true
}
}
8.2 执行shrink
POST source_index/_shrink/target_index
{
"settings":{
"index.number_of_replicas":1,
"index.number_of_shards":1,
"index.codec":"best_compression"
}
}
9. 关注我
搜索微信公众号:java架构强者之路