Преглед изворни кода

elastic提供批量更新方法

zhangjinkun пре 9 година
родитељ
комит
4f543ef91c

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
common/src/qfw/util/elastic/city.json


+ 25 - 0
common/src/qfw/util/elastic/elasticutil.go

@@ -445,6 +445,31 @@ func Update(index, itype, id string, updateStr string) bool {
 	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 {
+			ret, err := client.Update().Index(index).Type(itype).Id(id).Script(updateStr).ScriptLang("groovy").Do()
+			if err != nil {
+				log.Println("更新检索出错:", err.Error())
+			}
+		}
+	}
+}
+
 //根据id删除索引对象
 func DelById(index, itype, id string) bool {
 	client := GetEsConn()

+ 7 - 0
common/src/qfw/util/elastic/elasticutil_test.go

@@ -154,6 +154,13 @@ func Test_BulkSave(t *testing.T) {
 	BulkSave("test", "test", &res, true)
 }
 
+//批量更新字段
+func Test_BulkUpdate(t *testing.T) {
+	InitElastic("http://192.168.3.18:9800")
+	//regno 540192600031442,542422600000086
+	ids := []string{"55a85997a442ceca9e20de9f", "55a85997a442ceca9e20deaf"}
+	BulkUpdate("enterprise", "enterprise", ids, `ctx._source.Ycml=false`)
+}
 func Test_Count(t *testing.T) {
 	log.Println("ESCOUNT", Count("test", "test", MakeQuery(`{"name":"你们","TERM_age":503}`, "", "", -1, -1)))
 }

Неке датотеке нису приказане због велике количине промена