Browse Source

feat:修改获取总数方法

wangchuanjin 2 years ago
parent
commit
4dfc804ea6
2 changed files with 65 additions and 1 deletions
  1. 61 0
      es/es_test.go
  2. 4 1
      es/esv7.go

File diff suppressed because it is too large
+ 61 - 0
es/es_test.go


+ 4 - 1
es/esv7.go

@@ -900,13 +900,16 @@ func (e *EsV7) GetWithCount(index, itype, countQuery, allQuery string) (int64, *
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	var res []map[string]interface{}
-	count := e.Count(index, itype, countQuery)
+	var count int64
 	if client != nil {
+		allQuery = strings.TrimPrefix(strings.TrimSpace(allQuery), "{")
+		allQuery = `{"track_total_hits": true,` + allQuery
 		searchResult, err := client.Search().Index(index).Source(allQuery).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 {

Some files were not shown because too many files changed in this diff