elasticsearch 索引管理

如果你想禁止自动创建索引,你可以通过在 config/elasticsearch.yml 的每个节点下添加下面的配置:
2.0版本
action.auto_create_index: false

创建索引

curl -X PUT "localhost:9200/dev_employee_20181221"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X PUT "localhost:9200/dev_employee_20181219"
{"acknowledged":true,"shards_acknowledged":true,"index":"dev_employee_20181219"}

删除索引

// 删除一个
curl -X DELETE "localhost:9200/dev_employee_20181221"

// 删除多个
curl -X DELETE "localhost:9200/dev_employee_20181220,dev_employee_20181221"
curl -X DELETE "localhost:9200/dev_employee_*"

// 删除所有
curl -X DELETE "localhost:9200/_all"
curl -X DELETE "localhost:9200/*"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X DELETE "localhost:9200/dev_employee_20181219"
{"acknowledged":true}

索引设置

number_of_shards 每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。

number_of_replicas 每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。

1
2
3
4
5
6
7
8
curl -X PUT "localhost:9200/my_temp_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}
'
1
2
3
4
5
curl -X PUT "localhost:9200/my_temp_index/_settings" -H 'Content-Type: application/json' -d'
{
"number_of_replicas": 1
}
'

配置分词器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl -X PUT "localhost:9200/spanish_docs" -H 'Content-Type: application/json' -d'
{
"settings": {
"analysis": {
"analyzer": {
"es_std": {
"type": "standard",
"stopwords": "_spanish_"
}
}
}
}
}
'
1
2
3
curl -X GET "localhost:9200/spanish_docs/_analyze?analyzer=es_std" -H 'Content-Type: application/json' -d'
El veloz zorro marrón
'

创建索引 curl -X PUT "localhost:9200/dev_employee_20181221"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X PUT "localhost:9200/dev_employee_20181221"
{"acknowledged":true,"shards_acknowledged":true,"index":"dev_employee_20181221"}

给索引添加别名 curl -X PUT "localhost:9200/dev_employee_20181221/_alias/dev_employee"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X PUT "localhost:9200/dev_employee_20181221/_alias/dev_employee"
{"acknowledged":true}

检测这个别名指向哪些索引 同一别名多个索引 curl -X GET "localhost:9200/*/_alias/dev_employee"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X GET "localhost:9200/*/_alias/dev_employee"
{"dev_employee_20181221":{"aliases":{"dev_employee":{}}},"dev_employee_20181220":{"aliases":{"dev_employee":{}}}}

哪些别名指向这个索引 一个索引多个别名 curl -X GET "localhost:9200/dev_employee_20181221/_alias/*"

1
2
3
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X GET "localhost:9200/dev_employee_20181221/_alias/*"
{"dev_employee_20181221":{"aliases":{"dev_employee":{},"sit_employee":{}}}}

索引添加别名、 索引删除别名、 索引修改别名(索引不变,别名变了)、 索引切换(别名不变,索引换了)

1
2
3
4
5
6
7
8
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "dev_employee_20181220", "alias": "dev_employee" }},
{ "add": { "index": "dev_employee_20181221", "alias": "dev_employee" }}
]
}
'

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
#索引添加别名
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "add": { "index": "dev_employee_20181221", "alias": "dev_employee" }}
]
}
'

#索引删除别名
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "dev_employee_20181220", "alias": "dev_employee" }}
]
}
'

#索引修改别名
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "dev_employee_20181220", "alias": "dev_employee" }},
{ "add": { "index": "dev_employee_20181220", "alias": "dev_employee_backup" }}
]
}
'
{"acknowledged":true}


#索引切换(别名不变,索引换了)
WKQ@WKQ-PC MINGW64 /d/WorkSpaces
$ curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "dev_employee_20181220", "alias": "dev_employee" }},
{ "add": { "index": "dev_employee_20181221", "alias": "dev_employee" }}
]
}
'
{"acknowledged":true}

References

[1] 索引管理