|
@@ -0,0 +1,255 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "entity"
|
|
|
+ "fmt"
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+ "log"
|
|
|
+ qutil "qfw/util"
|
|
|
+ "qfw/util/elastic"
|
|
|
+ "qfw/util/redis"
|
|
|
+ "strings"
|
|
|
+ "util"
|
|
|
+)
|
|
|
+
|
|
|
+type IndexSearch struct {
|
|
|
+ *xweb.Action
|
|
|
+ getIndexMessage xweb.Mapper `xweb:"/index/getIndexMessage"` //首页p1数据
|
|
|
+}
|
|
|
+
|
|
|
+const (
|
|
|
+ search_index = "bidding"
|
|
|
+ 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}`
|
|
|
+ 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_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")
|
|
|
+ log.Println(userId, "=== 地区:", areaCity)
|
|
|
+ r := func() *entity.FuncResult {
|
|
|
+ SearchType := 0 //订阅 0 > 搜索 1(有无订阅)> 无搜索 2(有无订阅)
|
|
|
+ doSearchStr := ""
|
|
|
+ isVip := false
|
|
|
+ //获取订阅信息
|
|
|
+ userMap, ok := util.MQFW.FindById("user", userId, `{"o_jy":1,"o_vipjy":1,"i_vip_status":1}`)
|
|
|
+ if !ok || userMap == nil || len(*userMap) == 0 {
|
|
|
+ SearchType++
|
|
|
+ }
|
|
|
+ //根据订阅词获取查询语句
|
|
|
+ if SearchType == 0 {
|
|
|
+ vipStatus := qutil.IntAll((*userMap)["i_vip_status"])
|
|
|
+ if vipStatus > 0 { //vip用户
|
|
|
+ isVip = true
|
|
|
+ o_msgset := qutil.ObjToMap((*userMap)["o_vipjy"])
|
|
|
+ vip_items, ok := (*o_msgset)["a_items"].([]interface{})
|
|
|
+ if !ok || len(vip_items) == 0 {
|
|
|
+ SearchType++
|
|
|
+ } else {
|
|
|
+ //拼接查询语句-vip订阅词
|
|
|
+ doSearchStr = getVipSubscribeSql(o_msgset)
|
|
|
+ log.Println("getVipSubscribeSql SearchStr===", doSearchStr)
|
|
|
+ }
|
|
|
+ } else { //普通用户
|
|
|
+ o_msgset := qutil.ObjToMap((*userMap)["o_jy"])
|
|
|
+ items, ok := (*o_msgset)["a_key"].([]interface{})
|
|
|
+ if !ok || len(items) == 0 {
|
|
|
+ SearchType++
|
|
|
+ } else {
|
|
|
+ //拼接查询语句-普通用户订阅词
|
|
|
+ doSearchStr = getNormalSubscribeSql(items)
|
|
|
+ log.Println("getNormalSubscribeSql SearchStr===", doSearchStr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //根据搜索历史获取查询语句
|
|
|
+ if SearchType == 1 {
|
|
|
+ h := redis.GetStr("other", "s_"+userId)
|
|
|
+ if h == "" {
|
|
|
+ SearchType++
|
|
|
+ } else {
|
|
|
+ history := strings.Split(h, ",")
|
|
|
+ //拼接查询语句-普通用户订阅词
|
|
|
+ doSearchStr = getSimpleSql(areaCity, history)
|
|
|
+ log.Println(history)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //无订阅无搜索历史
|
|
|
+ if SearchType == 2 {
|
|
|
+ doSearchStr = getSimpleSql(areaCity, []string{})
|
|
|
+ }
|
|
|
+ list := elastic.Get(search_index, search_type, doSearchStr)
|
|
|
+ return &entity.FuncResult{true, nil, map[string]interface{}{
|
|
|
+ "isVip": isVip,
|
|
|
+ "list": list,
|
|
|
+ }}
|
|
|
+ }()
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf("%s CreateOrder err:%v\n", userId, r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
+}
|
|
|
+
|
|
|
+func getVipSubscribeSql(vipSets *map[string]interface{}) string {
|
|
|
+ items := qutil.ObjArrToMapArr((*vipSets)["a_items"].([]interface{}))
|
|
|
+ bools := []string{}
|
|
|
+ musts := []string{}
|
|
|
+
|
|
|
+ //订阅词
|
|
|
+ for _, v := range items {
|
|
|
+ a_key := (*qutil.ObjToMap(v))["a_key"]
|
|
|
+ keysets := qutil.ObjArrToMapArr(a_key.([]interface{}))
|
|
|
+ for _, keyset := range keysets {
|
|
|
+ musts := []string{}
|
|
|
+ must_not := []string{}
|
|
|
+
|
|
|
+ allKeys := []string{}
|
|
|
+ //关键词
|
|
|
+ keys := qutil.ObjArrToStringArr(keyset["key"].([]interface{}))
|
|
|
+ if len(keys) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ allKeys = append(allKeys, keys...)
|
|
|
+ //附加次
|
|
|
+ appendkeys := qutil.ObjArrToStringArr(keyset["appendkey"].([]interface{}))
|
|
|
+ allKeys = append(allKeys, appendkeys...)
|
|
|
+
|
|
|
+ for _, key := range allKeys {
|
|
|
+ musts = append(musts, fmt.Sprintf(multi_match, "\""+key+"\""))
|
|
|
+ }
|
|
|
+ //排除词
|
|
|
+ notkeys := qutil.ObjArrToStringArr(keyset["notkey"].([]interface{}))
|
|
|
+ for _, notkey := range notkeys {
|
|
|
+ must_not = append(must_not, fmt.Sprintf(multi_match, "\""+notkey+"\""))
|
|
|
+ }
|
|
|
+
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //地区
|
|
|
+ areaMap := qutil.ObjToMap((*vipSets)["o_area"])
|
|
|
+ var areas, citys, areaSql []string
|
|
|
+ for k, v := range (*areaMap) {
|
|
|
+ tmp := qutil.ObjArrToStringArr(v.([]interface{}))
|
|
|
+ if len(tmp) == 0 {
|
|
|
+ areas = append(areas, k)
|
|
|
+ } else {
|
|
|
+ citys = append(citys, tmp...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(areas) > 0 {
|
|
|
+ areaSql = append(areaSql, fmt.Sprintf(query_bool_must, "area", `"`+strings.Join(areas, `","`)+`"`))
|
|
|
+ }
|
|
|
+ if len(citys) > 0 {
|
|
|
+ areaSql = append(areaSql, fmt.Sprintf(query_bool_must, "city", `"`+strings.Join(citys, `","`)+`"`))
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(areaSql) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaSql, ",")))
|
|
|
+ }
|
|
|
+
|
|
|
+ //类型
|
|
|
+ infotypes := qutil.ObjArrToStringArr((*vipSets)["a_infotype"].([]interface{}))
|
|
|
+ if len(infotypes) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_must, "toptype", `"`+strings.Join(infotypes, `","`)+`"`))
|
|
|
+ }
|
|
|
+ //行业
|
|
|
+ buyerclasses := qutil.ObjArrToStringArr((*vipSets)["a_buyerclass"].([]interface{}))
|
|
|
+ if len(buyerclasses) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_must, "buyerclass", `"`+strings.Join(infotypes, `","`)+`"`))
|
|
|
+ }
|
|
|
+ return fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","))
|
|
|
+}
|
|
|
+
|
|
|
+func getNormalSubscribeSql(keySets []interface{}) string {
|
|
|
+ bools := []string{}
|
|
|
+ for _, v := range keySets {
|
|
|
+ keySet := qutil.ObjToMap(v)
|
|
|
+ if keySet == nil || len(*keySet) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var keywords, notkeys, areas, infotypes []string
|
|
|
+ if val, ok := (*keySet)["key"]; ok {
|
|
|
+ keywords = qutil.ObjArrToStringArr(val.([]interface{}))
|
|
|
+ } else {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if val, ok := (*keySet)["notkey"]; ok {
|
|
|
+ notkeys = qutil.ObjArrToStringArr(val.([]interface{}))
|
|
|
+ }
|
|
|
+ if val, ok := (*keySet)["area"]; ok {
|
|
|
+ areas = qutil.ObjArrToStringArr(val.([]interface{}))
|
|
|
+ }
|
|
|
+ if val, ok := (*keySet)["infotype"]; ok {
|
|
|
+ infotypes = qutil.ObjArrToStringArr(val.([]interface{}))
|
|
|
+ }
|
|
|
+ log.Println(keywords, notkeys, areas, infotypes)
|
|
|
+
|
|
|
+ musts := []string{}
|
|
|
+ must_not := []string{}
|
|
|
+ //关键词
|
|
|
+ for _, key := range keywords {
|
|
|
+ musts = append(musts, fmt.Sprintf(multi_match, "\""+key+"\""))
|
|
|
+ }
|
|
|
+ //排除词
|
|
|
+ for _, notkey := range notkeys {
|
|
|
+ must_not = append(must_not, fmt.Sprintf(multi_match, "\""+notkey+"\""))
|
|
|
+ }
|
|
|
+ //地区
|
|
|
+ if len(areas) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_must, "area", `"`+strings.Join(areas, `","`)+`"`))
|
|
|
+ }
|
|
|
+ //类型
|
|
|
+ if len(infotypes) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_must, "toptype", `"`+strings.Join(infotypes, `","`)+`"`))
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fmt.Sprintf(query, strings.Join(bools, ","))
|
|
|
+}
|
|
|
+
|
|
|
+func getSimpleSql(city string, history []string) string {
|
|
|
+ musts := []string{}
|
|
|
+ if len(history) > 0 {
|
|
|
+ for _, key := range history {
|
|
|
+ musts = append(musts, fmt.Sprintf(multi_match, "\""+key+"\""))
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+}
|