wangkaiyue преди 5 години
родител
ревизия
b10a44aee6
променени са 1 файла, в които са добавени 22 реда и са изтрити 31 реда
  1. 22 31
      src/jfw/modules/subscribepay/src/service/index_p1.go

+ 22 - 31
src/jfw/modules/subscribepay/src/service/index_p1.go

@@ -22,23 +22,13 @@ const (
 	search_type  = "bidding"
 	search_field = `"_id","title","publishtime","toptype","subtype","type","city","s_subscopeclass","budget"`
 
-	query               = `{"query":{"bool":{"should":[%s]}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":50}`
+	query               = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": 1}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":50}`
 	multi_match         = `{"multi_match": {"query": %s,"type": "phrase", "fields": ["title"]}}`
 	query_bool_must     = `{"terms":{"%s":[%s]}}`
-	query_bool_must_and = `{"bool":{"must":[%s]%s}}`
+	query_bool_must_and = `{"bool":{"must":[%s],"must_not":[%s]}}`
 	query_bool_should   = `{"bool":{"should":[%s],"minimum_should_match": 1}}`
 )
 
-/* p1获取信息
-  return
-	  success : true or false
-	  errMsh : 错误信息
-	  data  :{
-		isVip: true or false,
-		weekNullData:true or false,
-		list:[{},{},{}]
-	 }
-*/
 func (this *IndexSearch) GetIndexMessage() {
 	userId := qutil.ObjToString(this.GetSession("userId"))
 	areaCity := this.GetString("city")
@@ -87,17 +77,22 @@ func (this *IndexSearch) GetIndexMessage() {
 				history := strings.Split(h, ",")
 				//拼接查询语句-普通用户订阅词
 				doSearchStr = getSimpleSql(areaCity, history)
+				log.Println("getSimpleSql with history SearchStr===", doSearchStr)
 				log.Println(history)
 			}
 		}
 		//无订阅无搜索历史
 		if SearchType == 2 {
 			doSearchStr = getSimpleSql(areaCity, []string{})
+			log.Println("getSimpleSql SearchStr===", doSearchStr)
 		}
 		list := elastic.Get(search_index, search_type, doSearchStr)
 		return &entity.FuncResult{true, nil, map[string]interface{}{
-			"isVip": isVip,
-			"list":  list,
+			"isVip":        isVip,           //是否是vip用户
+			"hasSubscribe": SearchType == 0, //是否有订阅词
+			"hasHistory":   SearchType < 2,  //是否有搜索历史
+			"listLength":   len(*list),      //数据长度
+			"list":         list,            //数据
 		}}
 	}()
 	if r.Err != nil {
@@ -140,11 +135,7 @@ func getVipSubscribeSql(vipSets *map[string]interface{}) string {
 			}
 
 			if len(musts) > 0 {
-				notStr := ""
-				if len(must_not) > 0 {
-					notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
-				}
-				bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(musts, ","), notStr))
+				bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(musts, ","), strings.Join(must_not, ",")))
 			}
 		}
 	}
@@ -179,7 +170,7 @@ func getVipSubscribeSql(vipSets *map[string]interface{}) string {
 	//行业
 	buyerclasses := qutil.ObjArrToStringArr((*vipSets)["a_buyerclass"].([]interface{}))
 	if len(buyerclasses) > 0 {
-		musts = append(musts, fmt.Sprintf(query_bool_must, "buyerclass", `"`+strings.Join(infotypes, `","`)+`"`))
+		musts = append(musts, fmt.Sprintf(query_bool_must, "buyerclass", `"`+strings.Join(buyerclasses, `","`)+`"`))
 	}
 	return fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","))
 }
@@ -229,27 +220,27 @@ func getNormalSubscribeSql(keySets []interface{}) string {
 
 		//添加
 		if len(musts) > 0 {
-			notStr := ""
-			if len(must_not) > 0 {
-				notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
-			}
-			bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(musts, ","), notStr))
+			bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(musts, ","), strings.Join(must_not, ",")))
 		}
 	}
-	return fmt.Sprintf(query, strings.Join(bools, ","))
+	return fmt.Sprintf(query, "", strings.Join(bools, ","))
 }
 
 func getSimpleSql(city string, history []string) string {
-	musts := []string{}
+	var musts, bools []string
 	if len(history) > 0 {
-		for _, key := range history {
-			musts = append(musts, fmt.Sprintf(multi_match, "\""+key+"\""))
+		for _, v := range history {
+			keys := strings.Split(v, " ") //历史搜索 空格划分
+			must_tmp := []string{}
+			for _, key := range keys {
+				must_tmp = append(must_tmp, fmt.Sprintf(multi_match, "\""+key+"\""))
+			}
+			bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(must_tmp, ","), ""))
 		}
 
 	}
 	if city != "" {
 		musts = append(musts, fmt.Sprintf(query_bool_must, "area", `"`+city+`"`))
 	}
-	bools := fmt.Sprintf(query_bool_must_and, strings.Join(musts, ","), "")
-	return fmt.Sprintf(query, bools)
+	return fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","))
 }