Ver código fonte

feat:修改方法

wangchuanjin 2 anos atrás
pai
commit
19f19d73b1
4 arquivos alterados com 10 adições e 12 exclusões
  1. 3 3
      es/es.go
  2. 2 2
      es/es_test.go
  3. 2 2
      es/esv1.go
  4. 3 5
      es/esv7.go

+ 3 - 3
es/es.go

@@ -72,7 +72,7 @@ type Es interface {
 	GetAllByNgram_MP(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]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{})
+	GetWithCount(index, itype, countQuery, allQuery string) (int64, *[]map[string]interface{})
 	GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{})
 	Analyze(text, index, analyzer string) (res string)
 	analyzeResp(text, analyzer, u string) (*http.Request, error)
@@ -306,8 +306,8 @@ func GetAllByIk(index, itype, qstr, findfields, order, fields string, start, lim
 func GetResForJYView(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{} {
 	return VarEs.GetResForJYView(index, itype, keys, allquery, findfields, SortQuery, fields, start, limit)
 }
-func GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
-	return VarEs.GetWithCount(index, itype, query)
+func GetWithCount(index, itype, countQuery, allQuery string) (int64, *[]map[string]interface{}) {
+	return VarEs.GetWithCount(index, itype, countQuery, allQuery)
 }
 func GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{}) {
 	return VarEs.GetAggs(index, itype, query)

+ 2 - 2
es/es_test.go

@@ -16,8 +16,8 @@ type AggregationsBucket struct {
 func TestGet(t *testing.T) {
 	NewEs("v7", "http://192.168.3.241:9205,http://192.168.3.149:9200", 2, "", "")
 	//NewEs("v1", "http://192.168.3.206:9800", 2, "", "")
-	_, res := GetWithCount("bidding", "bidding", `{"query":{"bool":{"must":[{"range":{"publishtime":{"gte":1524141314,"lt":1681907714}}},{"bool":{"must":[{"multi_match": {"query": "软件","type": "phrase", "fields": ["title"]}}]}}],"must_not":[]}},"_source":["_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","projectname","projectcode","buyer","winner","bidopentime","buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist","district","sinendtime","buyeraddr","isValidFile"],"sort":[{"dataweight":"desc"},{"publishtime":"desc"}],"from":0,"size":50}`)
-	log.Println(res)
+	count, _ := GetWithCount("bidding", "bidding", `{"query":{"bool":{"must":[{"range":{"publishtime":{"gte":1524141314,"lt":1681907714}}},{"bool":{"must":[{"multi_match": {"query": "软件","type": "phrase", "fields": ["title"]}}]}}],"must_not":[]}},"_source":["_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","projectname","projectcode","buyer","winner","bidopentime","buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist","district","sinendtime","buyeraddr","isValidFile"],"sort":[{"dataweight":"desc"},{"publishtime":"desc"}],"from":0,"size":50}`)
+	log.Println(count)
 }
 
 func TestAnalyze(t *testing.T) {

+ 2 - 2
es/esv1.go

@@ -1188,7 +1188,7 @@ func (e *EsV1) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
 }
 
 //返回count 和 res
-func (e *EsV1) GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
+func (e *EsV1) GetWithCount(index, itype, countQuery, allQuery string) (int64, *[]map[string]interface{}) {
 	client := e.GetEsConn()
 	defer func() {
 		go e.DestoryEsConn(client)
@@ -1208,7 +1208,7 @@ func (e *EsV1) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 				}
 			}
 		}()
-		searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
+		searchResult, err := client.Search().Index(index).Type(itype).Source(allQuery).Do()
 		if err != nil {
 			log.Println("从ES查询出错", err.Error())
 			return count, nil

+ 3 - 5
es/esv7.go

@@ -993,19 +993,18 @@ func (e *EsV7) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
 }
 
 //返回count 和 res
-func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
+func (e *EsV7) GetWithCount(index, itype, countQuery, allQuery string) (int64, *[]map[string]interface{}) {
 	defer catch()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	var res []map[string]interface{}
-	var count int64 = 0
+	count := e.Count(index, itype, countQuery)
 	if client != nil {
-		searchResult, err := client.Search().Index(index).Source(query).Do(context.TODO())
+		searchResult, err := client.Search().Index(index).Source(countQuery).Do(context.TODO())
 		if err != nil {
 			log.Println("从ES查询出错", err.Error())
 			return count, nil
 		}
-		count = searchResult.TotalHits()
 		if searchResult.Hits != nil {
 			resNum := len(searchResult.Hits.Hits)
 			if resNum <= 5000 {
@@ -1025,7 +1024,6 @@ func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]in
 			} else {
 				log.Println("查询结果太多,查询到:", resNum, "条")
 			}
-
 		}
 	}
 	return count, &res