search.go 8.9 KB

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