ES ReIndex迁移数据

一、给目标集群新增白名单

1、elasticsearch.yml中添加reindex.remote.whitelist: [“xx.xx.xx.xx:9200”],不加http,重启ES服务;

1
2
3
4
5
6
7
http.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
reindex.remote.whitelist: ["124.70.107.101:30997","47.94.255.84:30997"]
1
2
3
4
5
6
7
docker ps|grep elasticsearch
docker exec -it elasticsearch /bin/bash
vi config/elasticsearch.yml
docker restart elasticsearch

二、使用PostMan修改目标主机ES参数

1、PUT _settings?pretty

1
2
3
4
{
"index.refresh_interval": -1,
"index.number_of_replicas": 0
}

三、使用reindex同步数据

1、同步数据,POST _reindex?wait_for_completion=false

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
43
44
45
46
47
48
49
50
51
52
53
# 1、过滤数据
{
"source": {
"remote": {
"host": "http://47.94.255.84:30997",
"username": "elastic",
"password": "IQsMLRniP5BcYfoNzTBT"
},
"size": 10000,
"index": "aiot_towercrane_realtime_master",
"query": {
"bool": {
"filter": [
{
"range": {
"createTime": {
"gte": 1652262590167,
"lte": 1652262590346
}
}
},
{
"term": {
"tenantId": "1441953685304872961"
}
}
]
}
}
},
"dest": {
"index": "aiot_towercrane_realtime_master"
}
}
# 2 查询数据
{
"source": {
"remote": {
"host": "http://47.94.255.84:30997",
"username": "elastic",
"password": "IQsMLRniP5BcYfoNzTBT"
},
"index": "aiot_deeppit_mesure_day_record_master",
"query": {
"match": {
"projectId": "1447476989099610114"
}
}
},
"dest": {
"index": "aiot_deeppit_mesure_day_record_xiongan"
}
}

注:

  • 同步之前,需要删除目标主机中已存在索引,否则同步不成功。
  • 同步ES数据时,如果需要认证,目标主机添加Basic auth认证,源主机添加username和password;

2、批量修改数据 POST aiot_towercrane_param_xiongan/_update_by_query

1
2
3
4
5
6
7
8
9
10
11
http://144.7.110.56:9200/aiot_towercrane_param_xiongan/_update_by_query
{
"query": {
"match_all": {
}
},
"script": {
"source": "ctx._source.tenantId = 1504718344228839425L "
}
}
  • 注意:Long类型更新需要添加L

四、查看迁移状况

1、查看所有task

GET _tasks?detailed=true&actions=*reindex

1
http://144.7.110.56:9200/_tasks?detailed=true&actions=*reindex

GET _tasks/xxx?pretty

1
http://144.7.110.56:9200/_tasks/6kPnJej0SZKt4Y5THf6TuQ:697134?pretty

五、恢复新es设置

1、PUT _settings?pretty

1
2
3
4
{
"index.refresh_interval": "10m",
"index.number_of_replicas": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool查询总结
must:与关系,相当于关系型数据库中的 and。
should:或关系,相当于关系型数据库中的 or。
must_not:非关系,相当于关系型数据库中的 not。
filter:过滤条件。
range:条件筛选范围。
gt:大于,相当于关系型数据库中的 >。
gte:大于等于,相当于关系型数据库中的 >=。
lt:小于,相当于关系型数据库中的 <。
lte:小于等于,相当于关系型数据库中的 <=。