es.go 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package es
  2. import (
  3. "fmt"
  4. IC "jyBXCore/rpc/init"
  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. queryBoolMust = `{"bool":{"must":[{"terms":{"s_subscopeclass":[%s]}}]}}`
  17. queryBoolMustTerm = `{"bool": {"must": [{ "term": {"isValidFile": %t }}]}}`
  18. queryExists = `{"constant_score":{"filter":{"exists":{"field":"%s"}}}}`
  19. gte = `"gte": %s`
  20. lte = `"lte": %s`
  21. HighlightStr = `"%s": {"fragment_size": %d,"number_of_fragments": 1}`
  22. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  23. INDEX = "bidding"
  24. TYPE = "bidding"
  25. INDEXNoLogin = "bidding_year"
  26. TYPENoLogin = "bidding_year"
  27. BidSearchSort = `{"dataweight":-1,"publishtime":-1}`
  28. BidSearchFieldBase = `"_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","buyer","winner","bidopentime"` //搜索列表基础字段
  29. BidSearchFieldOfVip = BidSearchFieldBase + `,"buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist"` //付费列表字段
  30. BidSearchFieldFile = `,"isValidFile"` //根据配置开关 选择是否显示 是否有附件提示,IC.C.FileSignBool
  31. BidSearchDomainField = BidSearchFieldOfVip + `,"purchasing"` //领域数据字段基本字段
  32. //DefaultFields = `"title"`
  33. IndexProjectSet = "projectset" // 项目信息es index
  34. TypeProjectSet = "projectset" // 项目信息es type //最新招标信息
  35. )
  36. var (
  37. SR = strings.Replace
  38. //信息类型 一级类型参数和数据类型转换
  39. topTypeMap = map[string]string{
  40. "招标预告": "预告",
  41. "招标公告": "招标",
  42. "招标结果": "结果",
  43. "招标信用信息": "其它",
  44. "拟建项目": "拟建",
  45. "采购意向": "采购意向",
  46. }
  47. )
  48. type SearchByES struct {
  49. Index string
  50. IType string
  51. Query string
  52. FindFields string
  53. Order string
  54. Fields string
  55. Start int
  56. Limit int
  57. Count int
  58. HighLight bool
  59. }
  60. // GetAllByNgramWithCount 获取es查询结果及总数量
  61. func (e *SearchByES) GetAllByNgramWithCount(isLogin bool) (int64, *[]map[string]interface{}) {
  62. if e.Query != "" {
  63. queryStr := e.Query
  64. if e.HighLight {
  65. var ws []string
  66. for _, w := range strings.Split(e.FindFields, ",") {
  67. ws = append(ws, fmt.Sprintf(HighlightStr, w, e.Count))
  68. }
  69. queryStr = queryStr[:len(queryStr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  70. }
  71. if len(e.Fields) > 0 {
  72. queryStr = queryStr[:len(queryStr)-1] + `,"_source":[` + e.Fields + "]}"
  73. }
  74. if len(e.Order) > 0 {
  75. queryStr = queryStr[:len(queryStr)-1] + `,"sort":[` + SR(SR(SR(SR(e.Order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  76. }
  77. if e.Start > -1 {
  78. queryStr = queryStr[:len(queryStr)-1] + `,"from":` + strconv.Itoa(e.Start) + `,"size":` + strconv.Itoa(e.Limit) + "}"
  79. }
  80. log.Println("queryStr:", queryStr)
  81. if isLogin {
  82. return elastic.GetWithCount(e.Index, e.IType, e.Query, queryStr)
  83. } else {
  84. return IC.NoLoginEs.GetWithCount(e.Index, e.IType, e.Query, queryStr)
  85. }
  86. } else {
  87. return 0, nil
  88. }
  89. }