search.go 7.6 KB

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