Bladeren bron

feat:推荐最新标讯

wangchuanjin 11 maanden geleden
bovenliggende
commit
e1a5c91619
1 gewijzigde bestanden met toevoegingen van 31 en 14 verwijderingen
  1. 31 14
      recommend/recommend.go

+ 31 - 14
recommend/recommend.go

@@ -11,32 +11,31 @@ import (
 )
 
 const (
-	query = `{"query":{"bool":{"must":[{"terms":{"autoid":[%s]}}]}},"_source":["_id","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype"],"sort":[{"publishtime":"desc"}],"from":0,"size":100}`
+	fields = `"_id","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype"`
 )
 
-var VarRecommend = &Recommend{}
+var (
+	VarRecommend = &Recommend{}
+	query        = `{"query":{"bool":{"must":[{"terms":{"autoid":[%s]}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"from":0,"size":100}`
+	newestQuery  = `{"query":{"bool":{"must":[{"match_all":{}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"size":1}`
+)
 
 type Recommend struct {
+	UseNewest bool
 }
 
 func (r *Recommend) SubRecommend(clickhouse *Mysql, domain, userId, content string, mailPush int) (string, string, string, int) {
-	datas := clickhouse.SelectBySql(`select bitmapToArray(infoids) as infoids,area from jianyu.sub_recommend_list where userid=? order by update_time desc limit 1`, userId)
-	if datas == nil || len(*datas) == 0 {
-		return "", "", "", 0
-	}
-	area, _ := (*datas)[0]["area"].(string)
+	query, area := r.getQuery(clickhouse, userId)
 	if area == "" {
 		area = "全国"
 	}
-	infoids, _ := (*datas)[0]["infoids"].([]uint64)
-	if len(infoids) == 0 {
-		return "", "", "", 0
+	if query == "" && r.UseNewest {
+		query = newestQuery
 	}
-	ids := []string{}
-	for _, v := range infoids {
-		ids = append(ids, fmt.Sprint(v))
+	if query == "" {
+		return "", "", "", 0
 	}
-	list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ",")))
+	list := elastic.Get(Es_Bidding, Es_Bidding, query)
 	if list == nil || len(*list) == 0 {
 		return "", "", "", 0
 	}
@@ -72,3 +71,21 @@ func (r *Recommend) SubRecommend(clickhouse *Mysql, domain, userId, content stri
 	}
 	return firstTitle, area, mailContent, infosLength
 }
+
+//
+func (r *Recommend) getQuery(clickhouse *Mysql, userId string) (string, string) {
+	datas := clickhouse.SelectBySql(`select bitmapToArray(infoids) as infoids,area from jianyu.sub_recommend_list where userid=? order by update_time desc limit 1`, userId)
+	if datas == nil || len(*datas) == 0 {
+		return "", ""
+	}
+	area, _ := (*datas)[0]["area"].(string)
+	infoids, _ := (*datas)[0]["infoids"].([]uint64)
+	if len(infoids) == 0 {
+		return "", ""
+	}
+	ids := []string{}
+	for _, v := range infoids {
+		ids = append(ids, fmt.Sprint(v))
+	}
+	return fmt.Sprintf(query, strings.Join(ids, ",")), area
+}