wangchuanjin 4 سال پیش
والد
کامیت
ab11de8dcc
1فایلهای تغییر یافته به همراه60 افزوده شده و 2 حذف شده
  1. 60 2
      esv7/elasticutil.go

+ 60 - 2
esv7/elasticutil.go

@@ -183,8 +183,8 @@ func Save(index, itype string, obj interface{}) bool {
 //{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"balance":{"gte":20000,"lte":30000}}}}}}
 //{"query":{"match_all":{}},"from":10,"size":10,"_source":["account_number","balance"],"sort":{"balance":{"order":"desc"}}}
 //{"query":{"match_phrase":{"address":"milllane"}}}和match不同会去匹配整个短语,相当于must[]
-func DfsGet(index, itype, query string) (int64, *[]map[string]interface{}) {
-	t, _, l := get(index, itype, query, "dfs_query_then_fetch", true, true)
+func DfsGet(index, query string) (int64, *[]map[string]interface{}) {
+	t, _, l := get(index, "", query, "dfs_query_then_fetch", true, true)
 	return t, l
 }
 func Get(index, itype, query string) *[]map[string]interface{} {
@@ -453,6 +453,64 @@ func GetById(index, itype string, ids ...string) *[]map[string]interface{} {
 	return &res
 }
 
+//根据语句更新对象
+func Update(index, itype, id string, updateStr string) bool {
+	client := GetEsConn()
+	defer DestoryEsConn(client)
+	b := false
+	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)
+				}
+			}
+		}()
+		var err error
+		esc := es.NewScript(updateStr)
+		esc.Lang("groovy")
+		_, err = client.Update().Index(index).Type(itype).Id(id).Script(esc).Do(context.TODO())
+		if err != nil {
+			log.Println("更新检索出错:", err.Error())
+		} else {
+			b = true
+		}
+	}
+	return b
+}
+
+func BulkUpdate(index, itype string, ids []string, updateStr string) {
+	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)
+				}
+			}
+		}()
+		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())
+			if err != nil {
+				log.Println("更新检索出错:", err.Error())
+			}
+		}
+	}
+}
+
 //根据id删除索引对象
 func DelById(index, itype, id string) bool {
 	client := GetEsConn()