Sfoglia il codice sorgente

feat:修改工具类

wangchuanjin 2 anni fa
parent
commit
bedf77840b
4 ha cambiato i file con 28 aggiunte e 40 eliminazioni
  1. 4 22
      es/es.go
  2. 19 11
      es/es_test.go
  3. 3 2
      es/esv1.go
  4. 2 5
      es/esv7.go

+ 4 - 22
es/es.go

@@ -6,6 +6,8 @@ import (
 	"log"
 	"runtime"
 	"strings"
+
+	esV7 "github.com/olivere/elastic/v7"
 )
 
 const (
@@ -33,26 +35,6 @@ const (
 	HL_IK           = `"highlight": {"pre_tags": ["` + IK_pre_tags + `"],"post_tags": ["` + IK_post_tags + `"],"fields": {%s}}`
 )
 
-type Aggregations map[string]json.RawMessage
-type AggregationSingleBucket struct {
-	Aggregations
-
-	DocCount int64 // `json:"doc_count"`
-}
-
-func (a Aggregations) Children(name string) (*AggregationSingleBucket, bool) {
-	if raw, found := a[name]; found {
-		agg := new(AggregationSingleBucket)
-		if raw == nil {
-			return agg, true
-		}
-		if err := json.Unmarshal(raw, agg); err == nil {
-			return agg, true
-		}
-	}
-	return nil, false
-}
-
 type Es interface {
 	Init()
 	Save(index, itype string, obj interface{}) bool
@@ -85,7 +67,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, res []map[string]interface{})
+	GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{})
 }
 
 var (
@@ -262,6 +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, res []map[string]interface{}) {
+func GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{}) {
 	return VarEs.GetAggs(index, itype, query)
 }

+ 19 - 11
es/es_test.go

@@ -1,21 +1,29 @@
 package es
 
 import (
+	"encoding/json"
+	"log"
 	"testing"
 )
 
+type AggregationsBucket struct {
+	Key       string `json:"key"`
+	Doc_count int    `json:"doc_count"`
+	Count     struct {
+		Value float64 `json:value`
+	} `json:"count"`
+}
+
 func TestGet(t *testing.T) {
-	var es Es
-	if true {
-		es = &EsV1{
-			Address: "http://192.168.3.206:9800",
-			Size:    5,
-		}
-	} else {
-		es = &EsV7{
-			Address: "http://192.168.3.206:9800",
-			Size:    5,
+	NewEs("v7", "http://192.168.3.241:9205,http://192.168.3.149:9200", 2, "", "")
+	//NewEs("v1", "http://192.168.3.206:9800", 2, "", "")
+	aggs, _, _ := GetAggs("projectset", "projectset", `{"query":{"bool":{"must":[{"range":{"firsttime":{"gte":1609430400,"lt":1681891594}}}],"must_not": [{"term": {"buyer": ""}}],"should":[{"bool": {"must": [{"multi_match": {"query": "实验室","type": "phrase", "fields": ["projectname.pname","purchasing"]}}]}}],"minimum_should_match": 1}},"size": 0,"aggs": {"group_field": {"terms": {"field": "entidlist","size": 1000},"aggs": {"count": {"sum": {"field": "bidamount"}}}}}}`)
+	var aggsMap []*AggregationsBucket
+	if aggsMap_g, ok := aggs.Children("group_field"); ok {
+		bs, _ := aggsMap_g.Aggregations["buckets"].MarshalJSON()
+		if len(bs) > 0 {
+			json.Unmarshal(bs, &aggsMap)
+			log.Println(aggsMap)
 		}
 	}
-	es.Init()
 }

+ 3 - 2
es/esv1.go

@@ -13,6 +13,7 @@ import (
 	"time"
 
 	es "app.yhyue.com/moapp/esv1/gopkg.in/olivere/elastic.v1"
+	esV7 "github.com/olivere/elastic/v7"
 )
 
 type EsV1 struct {
@@ -1236,7 +1237,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, res []map[string]interface{}) {
+func (e *EsV1) GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{}) {
 	defer catch()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
@@ -1246,7 +1247,7 @@ func (e *EsV1) GetAggs(index, itype, query string) (aggs Aggregations, count int
 			log.Println("从ES查询出错", err.Error())
 		}
 		if searchResult.Aggregations != nil {
-			aggs = Aggregations{}
+			aggs = esV7.Aggregations{}
 			for k, v := range searchResult.Aggregations {
 				aggs[k] = *v
 			}

+ 2 - 5
es/esv7.go

@@ -1031,7 +1031,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, res []map[string]interface{}) {
+func (e *EsV7) GetAggs(index, itype, query string) (aggs es.Aggregations, count int64, res []map[string]interface{}) {
 	defer catch()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
@@ -1042,10 +1042,7 @@ func (e *EsV7) GetAggs(index, itype, query string) (aggs Aggregations, count int
 		}
 		count = searchResult.TotalHits()
 		if searchResult.Aggregations != nil {
-			aggs = Aggregations{}
-			for k, v := range searchResult.Aggregations {
-				aggs[k] = v
-			}
+			aggs = searchResult.Aggregations
 		}
 		if searchResult.Hits != nil {
 			resNum := len(searchResult.Hits.Hits)