Ver código fonte

增加es查询err

xuzhiheng 4 anos atrás
pai
commit
f474a2e764

+ 4 - 1
service/projectDetail.go

@@ -16,7 +16,10 @@ func ProjectDetailData(id string) (data []map[string]interface{}, httpStatus int
 	query := fmt.Sprintf(_query, id, fields)
 	INDEX := core.GetStringConf("es.project.index")
 	TYPE := core.GetStringConf("es.project.itype")
-	infos := db.Es.Get(INDEX, TYPE, query)
+	infos, err := db.Es.Get(INDEX, TYPE, query)
+	if err != nil {
+		return nil, 200, err
+	}
 	if infos != nil && len(*infos) > 0 {
 		data = *infos
 		for _, val := range data {

+ 7 - 4
service/projects.go

@@ -81,8 +81,11 @@ func ProjectListData(productId int, appid, projectName, winner, times string, is
 	}
 	userProduct := &model.UserProduct{}
 	db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId})
-	repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, userProduct.DataNumLimitOneTimes, 0, false)
-	if repl != nil && len(*repl) > 0 {
+	repl, err := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, userProduct.DataNumLimitOneTimes, 0, false)
+	if err != nil {
+		err = errors.New("查询失败")
+		return
+	} else if repl != nil && len(*repl) > 0 {
 		data = *repl
 		for _, i := range data {
 			i["project_id"] = SE.EncodeString(utils.ObjToString(i["_id"]))
@@ -92,7 +95,7 @@ func ProjectListData(productId int, appid, projectName, winner, times string, is
 	return
 }
 
-func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
+func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) (*[]map[string]interface{}, error) {
 	if qstr != "" {
 		if highlight {
 			ws := []string{}
@@ -113,6 +116,6 @@ func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, f
 		global.Logger.Info("GetAllByNgram方法es查询", zap.Any("es语句", qstr))
 		return Es.Get(index, itype, qstr)
 	} else {
-		return nil
+		return nil, nil
 	}
 }

+ 3 - 3
sword_base/elastic/elasticSim.go

@@ -82,7 +82,7 @@ func (e *Elastic) GetEsConn() *es.Client {
 	}
 }
 
-func (e *Elastic) Get(index, itype, query string) *[]map[string]interface{} {
+func (e *Elastic) Get(index, itype, query string) (*[]map[string]interface{}, error) {
 	client := e.GetEsConn()
 	defer func() {
 		go e.DestoryEsConn(client)
@@ -104,7 +104,7 @@ func (e *Elastic) Get(index, itype, query string) *[]map[string]interface{} {
 		searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
 		if err != nil {
 			log.Println("从ES查询出错", err.Error())
-			return nil
+			return nil, err
 		}
 		if searchResult.Hits != nil {
 			resNum := len(searchResult.Hits.Hits)
@@ -121,7 +121,7 @@ func (e *Elastic) Get(index, itype, query string) *[]map[string]interface{} {
 			}
 		}
 	}
-	return &res
+	return &res, nil
 }
 
 //关闭elastic

+ 4 - 1
utils/cost_by_account_balance.go

@@ -30,7 +30,10 @@ func costByAccountBalance(getData func() ([]map[string]interface{}, int, error),
 	var err error
 	errStr := ""
 	orderCode := ""
-	data, statusCode, _ := execute(getData, appID, productID)
+	data, statusCode, errs := execute(getData, appID, productID)
+	if errs != nil {
+		return nil, "", errs, "查询失败"
+	}
 	beforeJudge, payMoney := beforeCheck(productType, product.UnitPrice, len(data), userProduct)
 	if beforeJudge {
 		global.Logger.Info("交易金额", zap.Any("payMoney:", payMoney))