es.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package es
  2. import (
  3. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. elastic "app.yhyue.com/moapp/jybase/es"
  8. "log"
  9. )
  10. const (
  11. queryBoolMustTermDomain = `{"bool": {"must": [{ "term": {"bid_field": "%s" }}]}}` // 领域化数据类型
  12. MultiMatch = `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s]}}`
  13. Query = `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
  14. QueryBoolShould = `{"bool":{"should":[%s],"minimum_should_match": 1}}`
  15. queryBoolMustBoolShould = `{"bool":{"must":[{"range":{"bidamount":{%s}}}]}},{"bool":{"must":[{"range":{"budget":{%s}}}],"must_not":[{"range":{"bidamount":{"gte":-1}}}]}}`
  16. queryBoolMustScopeClass = `{"bool":{"should":[{"terms":{"s_subscopeclass":[%s]}},{"bool":{"must":[{"terms":{"s_topscopeclass":[%s]}},{"bool":{"must_not":[{"exists":{"field":"s_subscopeclass"}}]}}]}}],"minimum_should_match":1}}` //`{"bool":{"must":[{"terms":{"s_subscopeclass":[%s]}}]}}`
  17. QueryBoolMustTermBool = `{"bool": {"must": [{ "term": {"%s": %t }}]}}`
  18. QueryBoolMustTerm = `{"bool": {"must": [{ "term": {"%s": %s }}]}}`
  19. QueryBoolMustA = `{"bool":{"must":[{"terms":{"%s":[%s]}}]}}`
  20. queryExists = `{"constant_score":{"filter":{"exists":{"field":"%s"}}}}`
  21. gte = `"gte": %s`
  22. lte = `"lte": %s`
  23. HighlightStr = `"%s": {"fragment_size": %d,"number_of_fragments": 1}`
  24. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  25. INDEX = "bidding"
  26. TYPE = "bidding"
  27. INDEXNoLogin = "bidding_year"
  28. TYPENoLogin = "bidding_year"
  29. BidSearchSort = `{"dataweight":-1,"publishtime":-1}`
  30. BidSearchFieldBase = `"_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","buyer","winner","bidopentime","district"` //搜索列表基础字段
  31. BidSearchFieldOfVip = BidSearchFieldBase + `,"buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist"` //付费列表字段
  32. BidSearchFieldFile = `,"isValidFile"` //根据配置开关 选择是否显示 是否有附件提示,IC.C.FileSignBool
  33. BidSearchDomainField = BidSearchFieldOfVip + `,"purchasing"`
  34. BidSearchFieldProperty = BidSearchFieldBase + `,"tag_topinformation","tag_subinformation","tag_set.wuye.property_form","tag_set.wuye.isfile"` //领域数据字段基本字段
  35. //DefaultFields = `"title"`
  36. IndexProjectSet = "projectset" // 项目信息es index
  37. TypeProjectSet = "projectset" // 项目信息es type //最新招标信息
  38. LoginTypePay = 1 // 付费用户
  39. LoginTypeFree = 2 // 免费用户
  40. LoginTypeNoLogin = 3 // 未登录用户
  41. //直采-采购信息
  42. PurchaseIndex, PurchaseType = "bidding_yg", "bidding_yg"
  43. PurchaseBaseField = `"_id","title","area","city","district","buyerclass","publishtime","isValidFile","budget","bidamount","signendtime","deliver_area","deliver_city","deliver_district","buyer","buyertel","s_subscopeclass","public_type","purchasing","purchasinglist","projectname","spidercode","subtype"`
  44. PurchaseSearchSort = `{"dataweight":-1,"publishtime":-1}`
  45. PurchaseBaseFieldForPayer = PurchaseBaseField + `,"buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist"`
  46. )
  47. var (
  48. SR = strings.Replace
  49. //信息类型 一级类型参数和数据类型转换
  50. topTypeMap = map[string]string{
  51. "招标预告": "预告",
  52. "招标公告": "招标",
  53. "招标结果": "结果",
  54. "招标信用信息": "其它",
  55. "拟建项目": "拟建",
  56. "采购意向": "采购意向",
  57. }
  58. )
  59. type SearchByES struct {
  60. Index string
  61. IType string
  62. Query string
  63. FindFields string
  64. Order string
  65. Fields string
  66. Start int
  67. Limit int
  68. Count int
  69. HighLight bool
  70. }
  71. // GetAllByNgramWithCount 获取es查询结果及总数量
  72. func (e *SearchByES) GetAllByNgramWithCount(loginType int) (int64, *[]map[string]interface{}) {
  73. if e.Query != "" {
  74. queryStr := e.Query
  75. if e.HighLight {
  76. var ws []string
  77. for _, w := range strings.Split(e.FindFields, ",") {
  78. ws = append(ws, fmt.Sprintf(HighlightStr, w, e.Count))
  79. }
  80. queryStr = queryStr[:len(queryStr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  81. }
  82. if len(e.Fields) > 0 {
  83. queryStr = queryStr[:len(queryStr)-1] + `,"_source":[` + e.Fields + "]}"
  84. }
  85. if len(e.Order) > 0 {
  86. queryStr = queryStr[:len(queryStr)-1] + `,"sort":[` + SR(SR(SR(SR(e.Order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  87. }
  88. if e.Start > -1 {
  89. queryStr = queryStr[:len(queryStr)-1] + `,"from":` + strconv.Itoa(e.Start) + `,"size":` + strconv.Itoa(e.Limit) + "}"
  90. }
  91. log.Println("queryStr:", queryStr)
  92. switch loginType {
  93. case LoginTypePay:
  94. return elastic.GetWithCount(e.Index, e.IType, e.Query, queryStr)
  95. case LoginTypeFree:
  96. // 免费用户
  97. return IC.FreeEs.GetWithCount(e.Index, e.IType, e.Query, queryStr)
  98. default:
  99. // 未登录
  100. return IC.NoLoginEs.GetWithCount(e.Index, e.IType, e.Query, queryStr)
  101. }
  102. } else {
  103. return 0, nil
  104. }
  105. }