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
|
// 批量添加
func (e ElasticController) BatchAddDoc() error {
userBulk := ElasticClient.Bulk().Index(userIndex)
loc, _ := time.LoadLocation("Local")
// 生日
birthSlice := []string{"1991-04-25", "1990-01-15", "1989-11-05", "1988-01-25", "1994-10-12"}
// 姓名
nameSlice := []string{"李四", "张飞", "赵云", "关羽", "刘备"}
rand.Seed(time.Now().Unix())
for i := 1; i < 20; i++ {
birth, _ := time.ParseInLocation("2006-01-02", birthSlice[rand.Intn(len(birthSlice))], loc)
height, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", rand.Float32()+175.0), 32)
user := User{
nameSlice[rand.Intn(len(nameSlice))],
rand.Intn(10) + 18,
"1760000000" + strconv.Itoa(i),
birth,
float32(height),
false,
"41.40338,2.17403",
}
fmt.Println(user, userBulk)
doc := elastic.NewBulkIndexRequest().Id(strconv.FormatInt(time.Now().UnixNano(), 10)).Doc(user)
userBulk.Add(doc)
}
if userBulk.NumberOfActions() < 0 {
return e.Error("没有要保存的数据")
}
if _, err := userBulk.Do(esCtx); err != nil {
return e.Error(err.Error())
}
return e.Success(nil)
}
|