search.go 8.1 KB

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