通过bulk,批量创建/更新索引
(data []ElasticSearchData)
| 398 | |
| 399 | //通过bulk,批量创建/更新索引 |
| 400 | func (this *ElasticSearchClient) BuildIndexByBuck(data []ElasticSearchData) (err error) { |
| 401 | if !this.On { |
| 402 | return |
| 403 | } |
| 404 | |
| 405 | now := time.Now() |
| 406 | var bodySlice []string |
| 407 | if len(data) > 0 { |
| 408 | var _id string |
| 409 | for _, item := range data { |
| 410 | if item.BookId > 0 { //书籍的id大于0,表示这个数据是文档的数据,否则是书籍的数据 |
| 411 | _id = fmt.Sprintf("doc_%v", item.Id) |
| 412 | } else { |
| 413 | _id = fmt.Sprintf("book_%v", item.Id) |
| 414 | } |
| 415 | action := fmt.Sprintf(`{"index":{"_index":"%v","_type":"%v","_id":"%v"}}`, this.Index, this.Type, _id) |
| 416 | bodySlice = append(bodySlice, action) |
| 417 | bodySlice = append(bodySlice, util.InterfaceToJson(item)) |
| 418 | } |
| 419 | api := this.Host + "_bulk" |
| 420 | body := strings.Join(bodySlice, "\n") + "\n" |
| 421 | if orm.Debug { |
| 422 | beego.Info("批量更新索引请求体") |
| 423 | beego.Info(body) |
| 424 | } |
| 425 | err = utils.HandleResponse(this.post(api).Body(body).Response()) |
| 426 | } |
| 427 | d := time.Since(now) |
| 428 | if d > time.Duration(this.Timeout) { |
| 429 | // 生成索引时长过长,休眠一小段时间 |
| 430 | beego.Info("sleep second", (time.Duration(this.Timeout/2) * time.Second).Seconds()) |
| 431 | time.Sleep(time.Duration(this.Timeout/2) * time.Second) |
| 432 | } |
| 433 | return |
| 434 | } |
| 435 | |
| 436 | // 设置书籍的公有和私有,需要根据文档同时更新文档的公有和私有 |
| 437 | func (this *ElasticSearchClient) SetBookPublic(bookId int, public bool) (err error) { |
no test coverage detected