search.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
  7. userPb "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  8. "context"
  9. "fmt"
  10. IC "jyBXCore/rpc/init"
  11. "jyBXCore/rpc/model/es"
  12. "jyBXCore/rpc/type/bxcore"
  13. "jyBXCore/rpc/util"
  14. "log"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. 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"}}]}`
  20. entQueryCount = `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
  21. index, itype = "qyxy", "qyxy"
  22. query = `{%s "query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer_name": ""}}]}}}`
  23. BuyerIndex = "buyer" // 采购单位index
  24. BuyerType = "buyer"
  25. )
  26. // GetBidSearchData 默认查询缓存数据 只查标题
  27. // 登录用户默认搜索500条数据,付费用户字段和免费用户字段不同,未登录用户查询5000条。
  28. // 标信息搜索 isCache:是否是获取缓存信息
  29. func GetBidSearchData(in *bxcore.SearchReq, isCache bool) (count int64, list []*bxcore.SearchList) {
  30. var start = int((in.PageNum - 1) * in.PageSize)
  31. if start >= 0 {
  32. t := time.Now()
  33. fields := MC.If(in.IsPay, es.BidSearchFieldOfVip, es.BidSearchFieldBase).(string)
  34. //in.BidField(医疗)领域化字段
  35. if in.BidField != "" {
  36. fields = es.BidSearchDomainField
  37. }
  38. //IC.C.FileSignBool列表是否显示附件开关
  39. if IC.C.FileSignBool {
  40. fields = fields + es.BidSearchFieldFile
  41. }
  42. biddingSearch := es.SearchByES{
  43. Index: MC.If(in.UserId == "", es.INDEXNoLogin, es.INDEX).(string),
  44. IType: MC.If(in.UserId == "", es.TYPENoLogin, es.TYPE).(string),
  45. Query: es.GetSearchQuery(in, es.GetBidSearchQuery(in)),
  46. FindFields: MC.If(isCache, "title", "detail").(string),
  47. Order: es.BidSearchSort,
  48. Fields: fields,
  49. Start: MC.If(isCache, 0, start).(int),
  50. Limit: MC.If(isCache, MC.If(in.IsPay, IC.C.DefaultBidInfo.PayCount, IC.C.DefaultBidInfo.Count).(int), int(in.PageSize)).(int),
  51. Count: MC.If(strings.Contains(in.SelectType, "detail"), 115, 0).(int), //高亮正文数量
  52. HighLight: MC.If(strings.Contains(in.SelectType, "detail"), true, false).(bool), //是否高亮正文
  53. }
  54. var loginType int
  55. // 处理免费用户index
  56. if in.UserId == "" {
  57. loginType = es.LoginTypeNoLogin
  58. } else if !in.IsPay {
  59. biddingSearch.Index = IC.DB.EsFree.Index
  60. biddingSearch.IType = IC.DB.EsFree.Type
  61. loginType = es.LoginTypeFree
  62. } else {
  63. loginType = es.LoginTypePay
  64. }
  65. var repl *[]map[string]interface{}
  66. if in.UserId != "" {
  67. count, repl = biddingSearch.GetAllByNgramWithCount(loginType)
  68. } else {
  69. if IC.C.NoLoginSearch.Switch {
  70. if flag := IC.ReqLimitInit.Limit(context.Background()); flag == 1 {
  71. defer IC.ReqLimitInit.Release()
  72. } else {
  73. if flag == -2 {
  74. log.Println("等待队列已满")
  75. } else if flag == -1 {
  76. log.Println("等待超时")
  77. }
  78. return 0, nil
  79. }
  80. }
  81. count, repl = biddingSearch.GetAllByNgramWithCount(loginType)
  82. }
  83. if repl != nil && *repl != nil && len(*repl) > 0 {
  84. //格式化查询结果
  85. list = util.SearchListFormat(in.UserId, in.Industry, repl, strings.Contains(in.SelectType, "detail"))
  86. } else {
  87. log.Println("查询数据异常")
  88. }
  89. log.Println(in.KeyWords, "关键词 -1- 查询耗时:", time.Since(t).Seconds())
  90. }
  91. return
  92. }
  93. func EntSearch(searchCode string) ([]*bxcore.Search, int64) {
  94. data := make([]*bxcore.Search, 0, 0)
  95. count := int64(0)
  96. musts := make([]string, 0, 0)
  97. musts = append(musts, `{"range":{"company_type_int":{"to":"22"}}}`)
  98. thisQuery := []string{}
  99. //查询指定内容
  100. thisQuery = append(thisQuery, fmt.Sprintf(`{"match_phrase":{"name":"%s"}}`, searchCode))
  101. musts = append(musts, fmt.Sprintf(`{"bool":{"should":[%s],"minimum_should_match": 1}}`, strings.Join(thisQuery, ",")))
  102. sql := fmt.Sprintf(entQuery, strings.Join(musts, ","), "")
  103. sql = sql[:len(sql)-1] + fmt.Sprintf(`,"from":%d,"size":%d}`, 0, 5)
  104. log.Println("企业搜索sql:", sql)
  105. count, list := elastic.GetWithCount(index, itype, "", sql)
  106. if list != nil {
  107. for _, value := range *list {
  108. data = append(data, &bxcore.Search{
  109. Title: MC.InterfaceToStr(value["company_name"]),
  110. Url: encrypt.EncodeArticleId2ByCheck(MC.ObjToString(value["_id"])),
  111. })
  112. }
  113. }
  114. return data, count
  115. }
  116. func ProcureSearch(searchCode string) ([]*bxcore.Search, int64) {
  117. data := []*bxcore.Search{}
  118. count := int64(0)
  119. //数据查询处理
  120. entNameQuery := fmt.Sprintf(`{"multi_match": {"query": "%s","type": "phrase", "fields": ["name"]}}`, searchCode)
  121. qstr := fmt.Sprintf(query, fmt.Sprintf(`"from":%d,"size": %d,`, 0, 5), entNameQuery)
  122. log.Println("采购单位搜索sql:", qstr)
  123. count, rs := elastic.GetWithCount(BuyerIndex, BuyerType, "", qstr)
  124. //rs := elastic.Get(BuyerIndex, BuyerType, qstr) // 采购单位列表
  125. if rs == nil || len(*rs) == 0 {
  126. return data, count
  127. }
  128. for i := 0; i < len(*rs); i++ {
  129. data = append(data, &bxcore.Search{
  130. Title: MC.ObjToString((*rs)[i]["name"]),
  131. })
  132. }
  133. return data, count
  134. }
  135. // 菜单搜索
  136. func MenuSearch(in *bxcore.PolymerizeSearchReq) []*bxcore.MenuList {
  137. data := []*bxcore.MenuList{}
  138. workData := IC.Middleground.UserCenter.WorkDesktopMenuInfo(userPb.WorkDesktopMenuInfoReq{
  139. UserId: in.UserId,
  140. AppId: in.AppId,
  141. Platform: "PC",
  142. NewUserId: MC.InterfaceToStr(in.NewUserId),
  143. EntId: MC.InterfaceToStr(in.EntId),
  144. EntUserId: MC.InterfaceToStr(in.EntUserId),
  145. AccountId: MC.InterfaceToStr(in.AccountId),
  146. PositionType: MC.InterfaceToStr(in.PositionType),
  147. PositionId: MC.InterfaceToStr(in.PositionId),
  148. EntAccountId: MC.InterfaceToStr(in.EntAccountId),
  149. })
  150. if workData != nil && len(workData.Data.MenuList) > 0 {
  151. for _, value1 := range workData.Data.MenuList {
  152. for _, value2 := range value1.Child {
  153. for _, value3 := range value2.Child {
  154. if strings.Contains(value3.Name, in.SearchCode) {
  155. tipInfo := &bxcore.TipInfo{}
  156. if value3.TipInfo != nil {
  157. tipInfo = &bxcore.TipInfo{
  158. Title: value3.TipInfo.Title,
  159. Content: value3.TipInfo.Content,
  160. ConfirmUrl: value3.TipInfo.ConfirmUrl,
  161. ConfirmText: value3.TipInfo.ConfirmText,
  162. IsShowCancel: value3.TipInfo.IsShowCancel,
  163. AppType: value3.TipInfo.AppType,
  164. OpenType: value3.TipInfo.OpenType,
  165. }
  166. }
  167. data = append(data, &bxcore.MenuList{
  168. Name: value3.Name,
  169. Icon: value3.Icon,
  170. Url: value3.Url,
  171. Usable: value3.Usable,
  172. AppType: value3.AppType,
  173. OpenType: value3.OpenType,
  174. TipInfo: tipInfo,
  175. Match: value3.Match,
  176. Path: fmt.Sprintf("%s>%s>%s", value1.Name, value2.Name, value3.Name),
  177. })
  178. }
  179. }
  180. }
  181. }
  182. }
  183. return data
  184. }
  185. func SubscribeSearch(searchCode string, powerCheck *pb.CheckResp) []*bxcore.Search {
  186. data := []*bxcore.Search{}
  187. in := &bxcore.SearchReq{
  188. PageNum: int64(1),
  189. PageSize: int64(10),
  190. IsPay: false,
  191. SelectType: "title",
  192. KeyWords: searchCode,
  193. PublishTime: fmt.Sprintf("%v_%v", time.Now().AddDate(-1, 0, 0).Unix(), time.Now().Unix()),
  194. }
  195. isLimit := int64(0)
  196. isLimit = util.IsLimited(in.LimitFlag, in.UserId, powerCheck.Vip.Status > 0 || powerCheck.Member.Status > 0, in.IsNew)
  197. if isLimit == 1 { //没有被限制
  198. defer util.Limit()
  199. }
  200. list := []*bxcore.SearchList{}
  201. if isLimit == 1 {
  202. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  203. t1 := time.Now()
  204. _, list = GetBidSearchData(in, false)
  205. log.Println("1查询耗时:", time.Since(t1))
  206. }
  207. if list != nil && len(list) > 0 {
  208. for _, value := range list {
  209. data = append(data, &bxcore.Search{
  210. Title: value.Title,
  211. Url: value.Id,
  212. DataTime: value.PublishTime,
  213. })
  214. }
  215. }
  216. return data
  217. }