重建索引【全量】 采用批量重建索引的方式进行 每次操作100条数据
()
| 292 | //采用批量重建索引的方式进行 |
| 293 | //每次操作100条数据 |
| 294 | func (this *ElasticSearchClient) RebuildAllIndex() { |
| 295 | helper.ConfigMap.Store("indexing", true) |
| 296 | defer helper.ConfigMap.Store("indexing", false) |
| 297 | //假设有10个亿的文档... |
| 298 | pageSize := 1000 |
| 299 | maxPage := int(1e7) |
| 300 | for page := 1; page < maxPage; page++ { |
| 301 | if infos, rows, err := NewDocument().GetDocInfoForElasticSearch(page, pageSize, 0); err != nil || rows == 0 { |
| 302 | if err != nil && err != orm.ErrNoRows { |
| 303 | helper.Logger.Error(err.Error()) |
| 304 | } |
| 305 | page = maxPage |
| 306 | } else { |
| 307 | var ids []int |
| 308 | for _, info := range infos { |
| 309 | ids = append(ids, info.Id) |
| 310 | } |
| 311 | timeStart := time.Now().Unix() |
| 312 | if data, err := NewDocument().GetDocForElasticSearch(ids...); err != nil { |
| 313 | helper.Logger.Error("批量生成索引失败:" + err.Error()) |
| 314 | } else { |
| 315 | if err := this.BuildIndexByBuck(data); err != nil { |
| 316 | helper.Logger.Error("批量生成索引失败:" + err.Error()) |
| 317 | } |
| 318 | } |
| 319 | timeEnd := time.Now().Unix() |
| 320 | //如果生成/更新索引耗时超过默认超时时间的一半,则休眠一小段时间,避免由于生成索引导致服务器负载过高,从而影响整站服务 |
| 321 | if spend := timeEnd - timeStart; spend > int64(this.Timeout)/2 { |
| 322 | time.Sleep(time.Duration(spend) * time.Second) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | //根据id查询文档,并创建索引 |
| 330 | func (this *ElasticSearchClient) BuildIndexById(id int) (err error) { |
no test coverage detected