Przeglądaj źródła

feat:增加方法

wangchuanjin 2 lat temu
rodzic
commit
417cd09d13
3 zmienionych plików z 50 dodań i 15 usunięć
  1. 6 0
      es/es.go
  2. 21 0
      es/esv1.go
  3. 23 15
      es/esv7.go

+ 6 - 0
es/es.go

@@ -32,6 +32,8 @@ const (
 	HL_IK           = `"highlight": {"pre_tags": ["` + IK_pre_tags + `"],"post_tags": ["` + IK_post_tags + `"],"fields": {%s}}`
 )
 
+type Aggregations map[string]*json.RawMessage
+
 type Es interface {
 	Init()
 	Save(index, itype string, obj interface{}) bool
@@ -64,6 +66,7 @@ type Es interface {
 	GetAllByIk(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{}
 	GetResForJYView(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{}
 	GetWithCount(index, itype, query string) (int64, *[]map[string]interface{})
+	GetAggs(index, itype, query string) (aggs Aggregations, count int64)
 }
 
 var (
@@ -241,3 +244,6 @@ func GetResForJYView(index, itype string, keys []KeyConfig, allquery, findfields
 func GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
 	return VarEs.GetWithCount(index, itype, query)
 }
+func GetAggs(index, itype, query string) (aggs Aggregations, count int64) {
+	return VarEs.GetAggs(index, itype, query)
+}

+ 21 - 0
es/esv1.go

@@ -1239,3 +1239,24 @@ func (e *EsV1) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 	}
 	return count, &res
 }
+
+//GetAggs 聚合查询
+func (e *EsV1) GetAggs(index, itype, query string) (aggs Aggregations, count int64) {
+	defer catch()
+	client := e.GetEsConn()
+	defer e.DestoryEsConn(client)
+	if client != nil {
+		searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
+		if err != nil {
+			log.Println("从ES查询出错", err.Error())
+		}
+		count = searchResult.Hits.TotalHits
+		if searchResult.Aggregations != nil {
+			aggs = Aggregations{}
+			for k, v := range searchResult.Aggregations {
+				aggs[k] = v
+			}
+		}
+	}
+	return
+}

+ 23 - 15
es/esv7.go

@@ -1014,25 +1014,12 @@ func (e *EsV7) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
 
 //返回count 和 res
 func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
+	defer catch()
 	client := e.GetEsConn()
-	defer func() {
-		go e.DestoryEsConn(client)
-	}()
+	defer e.DestoryEsConn(client)
 	var res []map[string]interface{}
 	var count int64 = 0
 	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).Source(query).Do(context.TODO())
 		if err != nil {
 			log.Println("从ES查询出错", err.Error())
@@ -1063,3 +1050,24 @@ func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 	}
 	return count, &res
 }
+
+//GetAggs 聚合查询
+func (e *EsV7) GetAggs(index, itype, query string) (aggs Aggregations, count int64) {
+	defer catch()
+	client := e.GetEsConn()
+	defer e.DestoryEsConn(client)
+	if client != nil {
+		searchResult, err := client.Search().Index(index).Source(query).Do(context.TODO())
+		if err != nil {
+			log.Println("从ES查询出错", err.Error())
+		}
+		count = searchResult.TotalHits()
+		if searchResult.Aggregations != nil {
+			aggs = Aggregations{}
+			for k, v := range searchResult.Aggregations {
+				aggs[k] = &v
+			}
+		}
+	}
+	return
+}