Bläddra i källkod

feat:增加方法

wangchuanjin 2 år sedan
förälder
incheckning
f34f65e711
3 ändrade filer med 33 tillägg och 5 borttagningar
  1. 2 2
      es/es.go
  2. 16 2
      es/esv1.go
  3. 15 1
      es/esv7.go

+ 2 - 2
es/es.go

@@ -84,7 +84,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)
+	GetAggs(index, itype, query string) (aggs Aggregations, count int64, res []map[string]interface{})
 }
 
 var (
@@ -262,6 +262,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) {
+func GetAggs(index, itype, query string) (aggs Aggregations, count int64, res []map[string]interface{}) {
 	return VarEs.GetAggs(index, itype, query)
 }

+ 16 - 2
es/esv1.go

@@ -1241,7 +1241,7 @@ func (e *EsV1) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 }
 
 //GetAggs 聚合查询
-func (e *EsV1) GetAggs(index, itype, query string) (aggs Aggregations, count int64) {
+func (e *EsV1) GetAggs(index, itype, query string) (aggs Aggregations, count int64, res []map[string]interface{}) {
 	defer catch()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
@@ -1250,13 +1250,27 @@ func (e *EsV1) GetAggs(index, itype, query string) (aggs Aggregations, count int
 		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
 			}
 		}
+		if searchResult.Hits != nil {
+			count = searchResult.Hits.TotalHits
+			resNum := len(searchResult.Hits.Hits)
+			if resNum < 5000 {
+				res = make([]map[string]interface{}, resNum)
+				for i, hit := range searchResult.Hits.Hits {
+					parseErr := json.Unmarshal(*hit.Source, &res[i])
+					if parseErr == nil && hit.Highlight != nil && res[i] != nil {
+						res[i]["highlight"] = map[string][]string(hit.Highlight)
+					}
+				}
+			} else {
+				log.Println("查询结果太多,查询到:", resNum, "条")
+			}
+		}
 	}
 	return
 }

+ 15 - 1
es/esv7.go

@@ -1052,7 +1052,7 @@ func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 }
 
 //GetAggs 聚合查询
-func (e *EsV7) GetAggs(index, itype, query string) (aggs Aggregations, count int64) {
+func (e *EsV7) GetAggs(index, itype, query string) (aggs Aggregations, count int64, res []map[string]interface{}) {
 	defer catch()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
@@ -1068,6 +1068,20 @@ func (e *EsV7) GetAggs(index, itype, query string) (aggs Aggregations, count int
 				aggs[k] = v
 			}
 		}
+		if searchResult.Hits != nil {
+			resNum := len(searchResult.Hits.Hits)
+			if resNum < 5000 {
+				res = make([]map[string]interface{}, resNum)
+				for i, hit := range searchResult.Hits.Hits {
+					parseErr := json.Unmarshal(hit.Source, &res[i])
+					if parseErr == nil && hit.Highlight != nil && res[i] != nil {
+						res[i]["highlight"] = map[string][]string(hit.Highlight)
+					}
+				}
+			} else {
+				log.Println("查询结果太多,查询到:", resNum, "条")
+			}
+		}
 	}
 	return
 }