MCPcopy Index your code
hub / github.com/TruthHun/BookStack / BuildIndexByBuck

Method BuildIndexByBuck

models/elasticsearch.go:400–434  ·  view source on GitHub ↗

通过bulk,批量创建/更新索引

(data []ElasticSearchData)

Source from the content-addressed store, hash-verified

398
399//通过bulk,批量创建/更新索引
400func (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// 设置书籍的公有和私有,需要根据文档同时更新文档的公有和私有
437func (this *ElasticSearchClient) SetBookPublic(bookId int, public bool) (err error) {

Callers 1

RebuildAllIndexMethod · 0.95

Calls 4

postMethod · 0.95
HandleResponseFunction · 0.92
ResponseMethod · 0.80
appendFunction · 0.50

Tested by

no test coverage detected