索引别名——API会自动将别名转换为实际索引名称。
别名可以映射到多个索引
别名还可以与过滤器关联,该过滤器在搜索和路由值时自动应用
别名不能与索引同名
1.创建一个索引的别名
2.删除一个索引的别名
3.重命名一个索引的别名
4.多个索引添加别名
5.创建过滤器别名(带有过滤器的别名,提供了一种简单的方法来创建系统个索引的不同视图。)
6.查询别名
7.滚动索引(当现有的索引太大或太老时,滚动索引将别名滚动到新的索引)
1. 创建一个索引的别POST /_aliases{
"actions": [
{
"add": {
"index": "test1",
"alias": "alias1"
}
}
]
}
GET _alias
{
"my_index" : {
"aliases" : {
"alias1" : { }
}
},
"test1" : {
"aliases" : {
"alias1" : { }
}
}
2.删除一个索引的别名
POST /_aliases
{
"actions": [
{
"remove": {
"index": "test1",
"alias": "alias1"
}
}
]
}
3.重命名一个索引的别名
#新建索引,没有别名
"test2" : {
"aliases" : { }
}
POST /_aliases
{
"actions": [
{
"remove": {
"index": "test1",
"alias": "alias1"
}},
{ "add": {
"index": "test2",
"alias": "alias1"
}
}
]
}
GET _alias
{
"my_index" : {
"aliases" : {
"alias1" : { }
}
},
"test2" : {
"aliases" : {
"alias1" : { }
}
},
".kibana_1" : {
"aliases" : {
".kibana" : { }
}
},
"test1" : {
"aliases" : { }
},
".kibana_task_manager" : {
"aliases" : { }
}
}
4.多个索引添加别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "test1",
"alias": "alias1"
}
},
{
"add": {
"index": "test2",
"alias": "alias2"
}
}
]
}
POST /_aliases
{
"actions": [
{
"add": {
"indices":[ "test1","test2"], "alias": "alias1"
}
}
]
}
5.创建过滤器别名
使用别名应用于所有的search ,count,delete by Query,more like this 操作
创建过滤器别名,先确定字段在mapping中。
GET my_index
"my_index" : {
"aliases" : {
"alias1" : { }
},
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"created" : {
"type" : "date"
},
"name1" : {
"type" : "text"
},
"title" : {
"type" : "text"
}
}
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "my_index",
"alias": "alias3",
"filter": {"term": {
"age": "16"
}}
}
}
]
}
GET my_index/_alias/*
"my_index" : {
"aliases" : {
"alias1" : { },
"alias3" : {
"filter" : {
"term" : {
"age" : "16"
}
}
}
}
}
6. 查询别名
索引名/_alias/别名
*/_all/glob pattern/ name1,name2
GET my_index/_alias/*
GET my_index/_alias/alias3
GET my_index/_alias/alias3,alias1
GET my_index/_alias/_all
7.滚动索引
滚动索引API接受单个别名和条件列表。
别名必须指向一个可写的索引,保证滚动请求有效。
两种场景:
1.别名仅仅指向一个索引,is_write_index参数不配置(默认是true)
原始索引的别名将滚动到新的索引中,并将别名从原始索引中移除。
2.别名指向多个索引,在其中一个索引上设置is_write_index为true,用于滚动。
将原索引的滚动别名的参数is_write_index设置为false,而新创建的索引将滚动别名指向自己,并is_write_index设置为true
PUT /log-00001
{
"aliases": {
"logs_write": {}
}
}
POST /logs_write/_rollover
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
7天或更长时间之前创建
包含1000个或更多文档,
索引的大小至少约为5GB,才会创建新的索引
滚动索引的命名规则是遵循第一个索引的命名规则的,如果想改新的索引如下:
POST /logs_write/_rollover/logs-001
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
滚动索引的例子:
PUT my_logs_index-0001
{
"aliases": {
"logs": {"is_write_index":true}
}
}
PUT logs/_doc/1
{
"message":"a dummy log"
}
POST logs/_refresh
POST logs/_rollover
{
"conditions": {
"max_docs": 1
}
}
PUT logs/_doc/2
{
"message":"a newer log"
}
{
"_index" : "my_logs_index-000002",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
GET _alias/logs
{
"my_logs_index-0001" : {
"aliases" : {
"logs" : {
"is_write_index" : false
}
}
},
"my_logs_index-000002" : {
"aliases" : {
"logs" : {
"is_write_index" : true
}
}
}
}
GET logs/_search
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_logs_index-000002",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"message" : "a newer log"
}
},
{
"_index" : "my_logs_index-0001",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"message" : "a dummy log"
}
}
]
}
}
PUT my_logs_index-0001/_doc/5
{
"message":"aaaa"
}
PUT my_logs_index-000002/_doc/5
{
"message":"bbbb"
}
PUT logs/_doc/7
{
"message":"ddd"
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_logs_index-000002",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"message" : "a newer log"
}
},
{
"_index" : "my_logs_index-000002",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"message" : "cccc"
}
},
{
"_index" : "my_logs_index-000002",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"message" : "ddd"
}
},
{
"_index" : "my_logs_index-0001",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"message" : "a dummy log"
}
},
{
"_index" : "my_logs_index-0001",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"message" : "aaaa"
}
}
]
}
}
滚动索引,版本7测试。可以向旧索引,新索引,索引别名插入数据。
问题:滚动索引不是自动创建新的索引吗? 可是没有啊,当我手动执行的时候才创建的。
索引设置一个别名滚动,智能化滚动一次吗?