Fork me on GitHub

Elasticsearch之索引重建

当我们在使用ES(Elasticsearch)作为全文检索服务器时, 文档结构避免不了会发生变化, 比如增加字段, 而且增加的字段需要进行分词时, 再比如原来文档结构中的字段类型发生变化了, 假设原来的字段类型是String, 而修改后的字段类型是Object, ES是不支持这种直接修改字段类型的。我们能做的只有进行索引重建, 当然关系型数据库也不支持这种类型字段改变的情况。
这里我们假定一些条件:

  • 端口为19200/19300
  • 带有HTTP Basic Auth认证
  • 原始的索引名称为goods

为原来的索引设置别名(如果不存在)

1
2
3
4
5
6
7
8
9
10
11
curl -u user:passwd -XPOST 'http://127.0.0.1:19200/_aliases?pretty' -d '
{
"actions": [
{
"add": {
"alias": "goods_alias",
"index": "goods"
}
}
]
}'

创建新的索引goods_v1

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
curl -u user:passwd -XPOST 'http://127.0.0.1:19200/goods_v1?pretty' -d '
{
"settings": {
"number_of_replicas": 0,
"number_of_shards": 8
},
"mappings": {
"clothing": {
"_source": {
"enabled": true
},
"_all": {
"term_vector": "no"
},
"properties": {
"desc" : {
"type" : "string"
},
"sale_time" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
},
"unit_price": {
"type": "double"
}
}
}
}
}'

用Golang脚本将旧的索引数据导入到新的索引中

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
package main

import (
"fmt"
"gopkg.in/olivere/elastic.v3"
)

func main() {
client, err := elastic.NewClient(
elastic.SetURL("http://127.0.0.1:19200"),
elastic.SetBasicAuth("user", "passwd"),
elastic.SetSniff(false))
if err != nil {
panic(err)
}

exists, err := client.IndexExists("goods").Do()
if err != nil {
panic(err)
}
if !exists {
fmt.Printf("%s does not exist yet\n", "goods")
} else {
// 索引迁移
reindexResp, err := client.Reindex("goods", "goods_v1").
TargetClient(client).
Query(elastic.NewMatchAllQuery()).
BulkSize(2000).
Scroll("5m").
Do()
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", reindexResp)
}
}

删除原来的索引别名, 并为新的索引设置跟之前索引一样的别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
curl -u user:passwd -XPOST 'http://127.0.0.1:19200/_aliases?pretty' -d '
{
"actions": [
{
"remove": {
"alias": "goods_alias",
"index": "goods"
},
"add": {
"alias": "goods_alias",
"index": "goods_v1"
}
}
]
}'

索引别名更正

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
curl -u user:passwd -XPOST 'http://127.0.0.1:19200/_aliases?pretty' -d '
{
"actions": [
{
"remove": {
"alias": "goods_alias",
"index": "goods_v1"
},
"add": {
"alias": "goods",
"index": "goods_v1"
}
}
]
}'

删除原来的索引

1
curl -u user:passwd -XDELETE 'http://127.0.0.1:19200/goods?pretty'