search.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package service
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/entity"
  8. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  9. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/model/es"
  10. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/type/bxcore"
  11. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/util"
  12. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
  13. userPb "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  14. "context"
  15. "fmt"
  16. "log"
  17. "strings"
  18. "time"
  19. )
  20. const (
  21. entQuery = `{"query":{"bool":{"must":[%s],"must_not":[%s]}},"_source":["_id","company_name","company_status","legal_person","capital","company_address","company_shortname","company_phone","establish_date"],"sort":[{"capital":{"order":"desc"}}]}`
  22. entQueryCount = `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
  23. index, itype = "qyxy", "qyxy"
  24. query = `{%s "query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer_name": ""}}]}}}`
  25. BuyerIndex = "buyer" // 采购单位index
  26. BuyerType = "buyer"
  27. )
  28. // GetBidSearchData 默认查询缓存数据 只查标题
  29. // 登录用户默认搜索500条数据,付费用户字段和免费用户字段不同,未登录用户查询5000条。
  30. // 标信息搜索 isCache:是否是获取缓存信息
  31. func GetBidSearchData(in *bxcore.SearchReq, isCache bool) (count int64, list []*bxcore.SearchList) {
  32. var start = int((in.PageNum - 1) * in.PageSize)
  33. if start >= 0 {
  34. t := time.Now()
  35. fields := MC.If(in.IsPay, es.BidSearchFieldOfVip, es.BidSearchFieldBase).(string)
  36. switch in.BidField {
  37. case "BIProperty": //物业专版-BI
  38. fields = es.BidSearchFieldProperty
  39. case "medical": //(医疗)领域化字段
  40. fields = es.BidSearchDomainField
  41. }
  42. //IC.C.FileSignBool列表是否显示附件开关
  43. if IC.C.FileSignBool {
  44. fields = fields + es.BidSearchFieldFile
  45. }
  46. biddingSearch := es.SearchByES{
  47. Index: MC.If(in.UserId == "", es.INDEXNoLogin, es.INDEX).(string),
  48. IType: MC.If(in.UserId == "", es.TYPENoLogin, es.TYPE).(string),
  49. Query: es.GetSearchQuery(in, es.GetBidSearchQuery(in)),
  50. //FindFields: MC.If(isCache, "title", "detail").(string),
  51. FindFields: in.SelectType,
  52. Order: es.BidSearchSort,
  53. Fields: fields,
  54. Start: MC.If(isCache, 0, start).(int),
  55. Limit: MC.If(isCache, MC.If(in.IsPay, IC.C.DefaultBidInfo.PayCount, IC.C.DefaultBidInfo.Count).(int), int(in.PageSize)).(int),
  56. Count: MC.If(strings.Contains(in.SelectType, "detail"), 115, 0).(int), //高亮正文数量
  57. HighLight: MC.If(strings.Contains(in.SelectType, "detail"), true, false).(bool) || MC.If(strings.Contains(in.SelectType, "filetext"), true, false).(bool), //是否高亮正文
  58. }
  59. var loginType int
  60. // 处理免费用户index
  61. if in.UserId == "" {
  62. loginType = es.LoginTypeNoLogin
  63. } else if !in.IsPay {
  64. biddingSearch.Index = IC.DB.EsFree.Index
  65. biddingSearch.IType = IC.DB.EsFree.Type
  66. loginType = es.LoginTypeFree
  67. } else {
  68. loginType = es.LoginTypePay
  69. }
  70. var repl *[]map[string]interface{}
  71. if in.UserId != "" {
  72. count, repl = biddingSearch.GetAllByNgramWithCount(loginType)
  73. } else {
  74. if IC.C.NoLoginSearch.Switch {
  75. if flag := IC.ReqLimitInit.Limit(context.Background()); flag == 1 {
  76. defer IC.ReqLimitInit.Release()
  77. } else {
  78. if flag == -2 {
  79. log.Println("等待队列已满")
  80. } else if flag == -1 {
  81. log.Println("等待超时")
  82. }
  83. return 0, nil
  84. }
  85. }
  86. count, repl = biddingSearch.GetAllByNgramWithCount(loginType)
  87. }
  88. if repl != nil && *repl != nil && len(*repl) > 0 {
  89. //格式化查询结果
  90. list = util.SearchListFormat(in.UserId, in.SubInformation, in.PropertyForm, in.Industry, repl, strings.Contains(in.SelectType, "detail"), in.BidField)
  91. } else {
  92. log.Println("查询数据异常")
  93. }
  94. log.Println(in.KeyWords, "关键词 -1- 查询耗时:", time.Since(t).Seconds())
  95. }
  96. return
  97. }
  98. func GetBidSearchCount(in *bxcore.SearchReq) int64 {
  99. bidIndex := MC.If(in.UserId == "", es.INDEXNoLogin, es.INDEX).(string)
  100. bidIType := MC.If(in.UserId == "", es.TYPENoLogin, es.TYPE).(string)
  101. var loginType int
  102. // 处理免费用户index
  103. if in.UserId == "" {
  104. loginType = es.LoginTypeNoLogin
  105. } else if !in.IsPay {
  106. bidIndex = IC.DB.EsFree.Index
  107. bidIType = IC.DB.EsFree.Type
  108. loginType = es.LoginTypeFree
  109. } else {
  110. loginType = es.LoginTypePay
  111. }
  112. switch loginType {
  113. case es.LoginTypePay:
  114. return elastic.Count(bidIndex, bidIType, es.GetSearchQuery(in, es.GetBidSearchQuery(in)))
  115. case es.LoginTypeFree:
  116. // 免费用户
  117. return IC.FreeEs.Count(bidIndex, bidIType, es.GetSearchQuery(in, es.GetBidSearchQuery(in)))
  118. default:
  119. // 未登录
  120. return IC.NoLoginEs.Count(bidIndex, bidIType, es.GetSearchQuery(in, es.GetBidSearchQuery(in)))
  121. }
  122. }
  123. func EntSearch(searchCode string) ([]*bxcore.Search, int64) {
  124. data := make([]*bxcore.Search, 0, 0)
  125. count := int64(0)
  126. musts := make([]string, 0, 0)
  127. musts = append(musts, `{"range":{"company_type_int":{"to":"22"}}}`)
  128. thisQuery := []string{}
  129. //查询指定内容
  130. thisQuery = append(thisQuery, fmt.Sprintf(`{"match_phrase":{"name":"%s"}}`, searchCode))
  131. musts = append(musts, fmt.Sprintf(`{"bool":{"should":[%s],"minimum_should_match": 1}}`, strings.Join(thisQuery, ",")))
  132. sql := fmt.Sprintf(entQuery, strings.Join(musts, ","), "")
  133. sql = sql[:len(sql)-1] + fmt.Sprintf(`,"from":%d,"size":%d}`, 0, 5)
  134. log.Println("企业搜索sql:", sql)
  135. count, list := elastic.GetWithCount(index, itype, "", sql)
  136. if list != nil {
  137. for _, value := range *list {
  138. data = append(data, &bxcore.Search{
  139. Title: MC.InterfaceToStr(value["company_name"]),
  140. Url: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(value["_id"])),
  141. })
  142. }
  143. }
  144. return data, count
  145. }
  146. func ProcureSearch(searchCode string) ([]*bxcore.Search, int64) {
  147. data := []*bxcore.Search{}
  148. count := int64(0)
  149. //数据查询处理
  150. entNameQuery := fmt.Sprintf(`{"multi_match": {"query": "%s","type": "phrase", "fields": ["name"]}}`, searchCode)
  151. qstr := fmt.Sprintf(query, fmt.Sprintf(`"from":%d,"size": %d,`, 0, 5), entNameQuery)
  152. log.Println("采购单位搜索sql:", qstr)
  153. count, rs := elastic.GetWithCount(BuyerIndex, BuyerType, "", qstr)
  154. //rs := elastic.Get(BuyerIndex, BuyerType, qstr) // 采购单位列表
  155. if rs == nil || len(*rs) == 0 {
  156. return data, count
  157. }
  158. for i := 0; i < len(*rs); i++ {
  159. data = append(data, &bxcore.Search{
  160. Title: MC.ObjToString((*rs)[i]["name"]),
  161. })
  162. }
  163. return data, count
  164. }
  165. // 菜单搜索
  166. func MenuSearch(in *bxcore.PolymerizeSearchReq) []*bxcore.MenuList {
  167. data := []*bxcore.MenuList{}
  168. workData := IC.Middleground.UserCenter.WorkDesktopMenuInfo(userPb.WorkDesktopMenuInfoReq{
  169. UserId: in.UserId,
  170. AppId: in.AppId,
  171. Platform: "PC",
  172. NewUserId: MC.InterfaceToStr(in.NewUserId),
  173. EntId: MC.InterfaceToStr(in.EntId),
  174. EntUserId: MC.InterfaceToStr(in.EntUserId),
  175. AccountId: MC.InterfaceToStr(in.AccountId),
  176. PositionType: MC.InterfaceToStr(in.PositionType),
  177. PositionId: MC.InterfaceToStr(in.PositionId),
  178. EntAccountId: MC.InterfaceToStr(in.EntAccountId),
  179. })
  180. if workData != nil && len(workData.Data.MenuList) > 0 {
  181. for _, value1 := range workData.Data.MenuList {
  182. for _, value2 := range value1.Child {
  183. for _, value3 := range value2.Child {
  184. if strings.Contains(value3.Name, in.SearchCode) {
  185. tipInfo := &bxcore.TipInfo{}
  186. if value3.TipInfo != nil {
  187. tipInfo = &bxcore.TipInfo{
  188. Title: value3.TipInfo.Title,
  189. Content: value3.TipInfo.Content,
  190. ConfirmUrl: value3.TipInfo.ConfirmUrl,
  191. ConfirmText: value3.TipInfo.ConfirmText,
  192. IsShowCancel: value3.TipInfo.IsShowCancel,
  193. AppType: value3.TipInfo.AppType,
  194. OpenType: value3.TipInfo.OpenType,
  195. }
  196. }
  197. data = append(data, &bxcore.MenuList{
  198. Name: value3.Name,
  199. Icon: value3.Icon,
  200. Url: value3.Url,
  201. Usable: value3.Usable,
  202. AppType: value3.AppType,
  203. OpenType: value3.OpenType,
  204. TipInfo: tipInfo,
  205. Match: value3.Match,
  206. Path: fmt.Sprintf("%s>%s>%s", value1.Name, value2.Name, value3.Name),
  207. })
  208. }
  209. }
  210. }
  211. }
  212. }
  213. return data
  214. }
  215. func SubscribeSearch(searchCode string, powerCheck *pb.CheckResp) []*bxcore.Search {
  216. data := []*bxcore.Search{}
  217. in := &bxcore.SearchReq{
  218. PageNum: int64(1),
  219. PageSize: int64(10),
  220. IsPay: false,
  221. SelectType: "title",
  222. KeyWords: searchCode,
  223. PublishTime: fmt.Sprintf("%v_%v", time.Now().AddDate(-1, 0, 0).Unix(), time.Now().Unix()),
  224. }
  225. isLimit := int64(0)
  226. isLimit = util.IsLimited(in.LimitFlag, in.UserId, powerCheck.Vip.Status > 0 || powerCheck.Member.Status > 0, in.IsNew)
  227. if isLimit == 1 { //没有被限制
  228. defer util.Limit()
  229. }
  230. list := []*bxcore.SearchList{}
  231. if isLimit == 1 {
  232. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  233. t1 := time.Now()
  234. _, list = GetBidSearchData(in, false)
  235. log.Println("1查询耗时:", time.Since(t1))
  236. }
  237. if list != nil && len(list) > 0 {
  238. for _, value := range list {
  239. data = append(data, &bxcore.Search{
  240. Title: value.Title,
  241. Url: value.Id,
  242. DataTime: value.PublishTime,
  243. })
  244. }
  245. }
  246. return data
  247. }
  248. // IsOnTheWhitelist 是否在白名单
  249. func IsOnTheWhitelist(userId, mgoUserId string) (flag bool, err error) {
  250. // mongoid 在白名单中没有查到 再去查职位id有没有在白名单里面
  251. flag, err = redis.Exists(entity.RedisPoly, fmt.Sprintf(entity.WhitelistRedisKey, mgoUserId))
  252. if err == nil && flag {
  253. return
  254. }
  255. flag, err = redis.Exists(entity.RedisPoly, fmt.Sprintf(entity.WhitelistRedisKey, userId))
  256. return
  257. }
  258. // FilterGeneric 通用词处理
  259. func FilterGeneric(keyWords string) string {
  260. keyWords = entity.FilterReg_3.ReplaceAllString(keyWords, "")
  261. keyWords = entity.FilterReg_2.ReplaceAllString(keyWords, "")
  262. keyWords = entity.FilterReg_1.ReplaceAllString(keyWords, "")
  263. keyWords = entity.FilterReg.ReplaceAllString(keyWords, "")
  264. return keyWords
  265. }
  266. // AdditionalFilterGeneric 附加词处理通用词
  267. // 逗号分割后再空格分割然后再过滤
  268. // 过滤完再合回去
  269. func AdditionalFilterGeneric(keyWords string) (additionalWords string) {
  270. keyWordsArr := strings.Split(keyWords, ",")
  271. var finalArr []string
  272. for i := 0; i < len(keyWordsArr); i++ {
  273. var tmpArr []string
  274. words := strings.Split(keyWordsArr[i], " ")
  275. for j := 0; j < len(words); j++ {
  276. word := FilterGeneric(words[j])
  277. if word != "" {
  278. tmpArr = append(tmpArr, word)
  279. }
  280. }
  281. if len(tmpArr) > 0 {
  282. // 还原空格分割
  283. spaceWord := strings.Join(tmpArr, " ")
  284. finalArr = append(finalArr, spaceWord)
  285. }
  286. }
  287. // 还原逗号分割
  288. if len(finalArr) > 0 {
  289. additionalWords = strings.Join(finalArr, ",")
  290. }
  291. return additionalWords
  292. }