search.go 7.1 KB

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