zhangjinkun 9 ani în urmă
părinte
comite
1d75c536cf

+ 37 - 4
common/src/qfw/util/elastic/elasticutil.go

@@ -146,7 +146,7 @@ func Save(index, itype string, obj interface{}) bool {
 //{"query":{"match_all":{}},"from":10,"size":10,"_source":["account_number","balance"],"sort":{"balance":{"order":"desc"}}}
 //{"query":{"match_phrase":{"address":"milllane"}}}和match不同会去匹配整个短语,相当于must[]
 func Get(index, itype, query string) *[]map[string]interface{} {
-	//log.Println("query  -- ", query)
+	log.Println("query  -- ", query)
 	client := GetEsConn()
 	defer DestoryEsConn(client)
 	var res []map[string]interface{}
@@ -188,6 +188,41 @@ func Get(index, itype, query string) *[]map[string]interface{} {
 	return &res
 }
 
+func GetNoLimit(index, itype, query string) *[]map[string]interface{} {
+	log.Println("query  -- ", query)
+	client := GetEsConn()
+	defer DestoryEsConn(client)
+	var res []map[string]interface{}
+	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)
+				}
+			}
+		}()
+		searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
+		if err != nil {
+			log.Println("从ES查询出错", err.Error())
+			return nil
+		}
+
+		if searchResult.Hits != nil {
+			resNum := len(searchResult.Hits.Hits)
+			res = make([]map[string]interface{}, resNum)
+			for i, hit := range searchResult.Hits.Hits {
+				json.Unmarshal(*hit.Source, &res[i])
+			}
+		}
+	}
+	return &res
+}
+
 //分页查询
 //{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
 //fields直接是 `"_id","title"`
@@ -767,9 +802,7 @@ func Count(index, itype string, query interface{}) int64 {
 		if err != nil {
 			log.Println("统计出错", err.Error())
 		}
-		if n > 500 {
-			n = 500
-		}
+
 		return n
 	}
 	return 0

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

@@ -161,6 +161,16 @@ func Test_BulkUpdate(t *testing.T) {
 	ids := []string{"55a85997a442ceca9e20de9f", "55a85997a442ceca9e20deaf"}
 	BulkUpdate("enterprise", "enterprise", ids, `ctx._source.Ycml=false`)
 }
+
+//获取数据,不受限制
+func Test_getNoLimit(t *testing.T) {
+	InitElasticSize("http://192.168.3.18:9800", 1)
+	k := "EntType"
+	v := "9600"
+	query := `{"query": {"bool":{"must":[{"query_string":{"default_field":"` + k + `","query":"` + v + `"}},{"query_string":{"default_field":"Ycml","query":true}}]}},"_source": ["_id","EntName"],"from": 0,"size": 999999}`
+	nt := GetNoLimit("enterprise", "enterprise", query)
+	log.Println("总数", len(*nt))
+}
 func Test_Count(t *testing.T) {
 	log.Println("ESCOUNT", Count("test", "test", MakeQuery(`{"name":"你们","TERM_age":503}`, "", "", -1, -1)))
 }