wangchuanjin 4 лет назад
Родитель
Сommit
26c10b4159
1 измененных файлов с 33 добавлено и 6 удалено
  1. 33 6
      esv7/elasticutil.go

+ 33 - 6
esv7/elasticutil.go

@@ -476,9 +476,7 @@ func Update(index, itype, id string, updateStr string) bool {
 			}
 		}()
 		var err error
-		esc := es.NewScript(updateStr)
-		esc.Lang("groovy")
-		_, err = client.Update().Index(index).Type(itype).Id(id).Script(esc).Do(context.TODO())
+		_, err = client.Update().Index(index).Id(id).Script(es.NewScript(updateStr)).Do(context.TODO())
 		if err != nil {
 			log.Println("更新检索出错:", err.Error())
 		} else {
@@ -505,9 +503,7 @@ func BulkUpdate(index, itype string, ids []string, updateStr string) {
 			}
 		}()
 		for _, id := range ids {
-			esc := es.NewScript(updateStr)
-			esc.Lang("groovy")
-			_, err := client.Update().Index(index).Type(itype).Id(id).Script(esc).Do(context.TODO())
+			_, err := client.Update().Index(index).Id(id).Script(es.NewScript(updateStr)).Do(context.TODO())
 			if err != nil {
 				log.Println("更新检索出错:", err.Error())
 			}
@@ -515,6 +511,37 @@ func BulkUpdate(index, itype string, ids []string, updateStr string) {
 	}
 }
 
+func NewBulkUpdate(index string, params [][]string) bool {
+	client := GetEsConn()
+	defer DestoryEsConn(client)
+	if client != nil {
+		defer func() {
+			if r := recover(); r != nil {
+				log.Println("[E]", r)
+				for skip := 1; ; skip++ {
+					_, file, line, ok := runtime.Caller(skip)
+					if !ok {
+						break
+					}
+					go log.Printf("%v,%v\n", file, line)
+				}
+			}
+		}()
+		bulk := client.Bulk()
+		for _, param := range params {
+			req := es.NewBulkUpdateRequest().Index(index).Id(param[0]).RetryOnConflict(3).Script(es.NewScript(param[1]))
+			bulk.Add(req)
+		}
+		_, err := bulk.Refresh("wait_for").Do(context.TODO())
+		if err != nil {
+			log.Println("批量更新检索出错:", err.Error())
+			return false
+		}
+		return true
+	}
+	return false
+}
+
 //根据id删除索引对象
 func DelById(index, itype, id string) bool {
 	client := GetEsConn()