重建索引【全量】 采用批量重建索引的方式进行 每次操作100条数据
(bookId ...int)
| 267 | //采用批量重建索引的方式进行 |
| 268 | //每次操作100条数据 |
| 269 | func (this *ElasticSearchClient) RebuildAllIndex(bookId ...int) { |
| 270 | if !this.On { |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | bid := 0 |
| 275 | if len(bookId) > 0 { |
| 276 | bid = bookId[0] |
| 277 | } |
| 278 | |
| 279 | if IsRebuildAllIndex && bid <= 0 { |
| 280 | return |
| 281 | } |
| 282 | if bid <= 0 { |
| 283 | defer func() { |
| 284 | IsRebuildAllIndex = false |
| 285 | }() |
| 286 | IsRebuildAllIndex = true |
| 287 | } |
| 288 | |
| 289 | pageSize := 1000 |
| 290 | maxPage := int(1e7) |
| 291 | |
| 292 | privateMap := make(map[int]int) //map[book_id]private |
| 293 | |
| 294 | o := orm.NewOrm() |
| 295 | book := NewBook() |
| 296 | // 更新书籍 |
| 297 | for page := 1; page < maxPage; page++ { |
| 298 | var books []Book |
| 299 | fields := []string{"book_id", "book_name", "label", "description", "privately_owned"} |
| 300 | q := o.QueryTable(book).Limit(pageSize).Offset((page - 1) * pageSize) |
| 301 | if bid > 0 { |
| 302 | q.Filter("book_id", bookId).All(&books, fields...) |
| 303 | } else { |
| 304 | q.All(&books, fields...) |
| 305 | } |
| 306 | |
| 307 | if len(books) > 0 { |
| 308 | var data []ElasticSearchData |
| 309 | var bookId []int |
| 310 | for _, item := range books { |
| 311 | data = append(data, ElasticSearchData{ |
| 312 | Id: item.BookId, |
| 313 | Title: item.BookName, |
| 314 | Keywords: item.Label, |
| 315 | Content: item.Description, |
| 316 | BookId: 0, //注意:这里必须设置为0 |
| 317 | Private: item.PrivatelyOwned, |
| 318 | Vcnt: item.Vcnt, |
| 319 | }) |
| 320 | privateMap[item.BookId] = item.PrivatelyOwned |
| 321 | bookId = append(bookId, item.BookId) |
| 322 | } |
| 323 | if err := this.BuildIndexByBuck(data); err != nil { |
| 324 | beego.Error(err.Error()) |
| 325 | beego.Error("书籍索引创建失败,书籍ID:", bookId) |
| 326 | } else { |
no test coverage detected