projects.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "sfbase/core"
  6. "sfbase/elastic"
  7. "sfbase/global"
  8. "sfis/db"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. pjt_count = 100
  15. pjt_field = `"_id","jgtime","zbtime","projectname","buyer","buyerclass"`
  16. pjt_fields = `"_id","area","city","bidamount","budget","zbtime","jgtime","projectname","projectcode","s_winner","buyer","buyerclass","buyerperson","buyertel","sourceinfourl"`
  17. pjt_sort = `{"jgtime":-1}`
  18. query = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
  19. query_string = `{"constant_score": {"boost": 2,"query": {"match_phrase": {"projectname.pname": {"analyzer": "my_ngram","query": "%s","slop": 6}}}}},{"multi_match": {"query": "%s","fields": ["projectname.pname"],"analyzer": "ik","minimum_should_match": "100%%"}}`
  20. query_winner = `{"term": {"s_winner": "%s"}}`
  21. SR = strings.Replace
  22. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  23. highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
  24. )
  25. func ProjectListData(projectName, winner, times string, isDetail bool) (data []map[string]interface{}, httpStatus int, err error) {
  26. times = strings.TrimSpace(times)
  27. winner = strings.TrimSpace(winner)
  28. pjt_name := strings.TrimSpace(projectName)
  29. pjt_len := len([]rune(pjt_name))
  30. httpStatus = 200
  31. qstr := ""
  32. timestr := ""
  33. if times != "" {
  34. start := times + " 00:00:00"
  35. end := times + " 23:59:59"
  36. loc, _ := time.LoadLocation("Local")
  37. startTimes := ""
  38. endTimes := ""
  39. if startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, loc); err == nil {
  40. startTimes = fmt.Sprint(startTime.Unix())
  41. }
  42. if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
  43. endTimes = fmt.Sprint(endTime.Unix())
  44. }
  45. // if start == "" && end != "" {
  46. // timestr = `{"range": {"firsttime": {"lte": ` + end + `}}}`
  47. // } else if start != "" && end == "" {
  48. // timestr = `{"range": {"firsttime": {"gte": ` + start + `}}}`
  49. // } else if start != "" && end != "" {
  50. timestr = `{"range": {"jgtime": {"gte": ` + startTimes + `,"lte": ` + endTimes + `}}}`
  51. // }
  52. }
  53. if pjt_len >= 4 && winner == "" {
  54. qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name))
  55. if timestr != "" {
  56. qstr = fmt.Sprintf(query, timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  57. }
  58. } else if pjt_len >= 4 && winner != "" {
  59. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name))
  60. if timestr != "" {
  61. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  62. }
  63. } else if winner != "" {
  64. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "")
  65. if timestr != "" {
  66. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "")
  67. }
  68. } else {
  69. err = errors.New("项目名长度小于4")
  70. return
  71. }
  72. INDEX := core.GetStringConf("es.project.index")
  73. TYPE := core.GetStringConf("es.project.itype")
  74. Es := db.GetEs()
  75. fields := ""
  76. if isDetail {
  77. fields = pjt_fields
  78. } else {
  79. fields = pjt_field
  80. }
  81. repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, pjt_count, 0, false)
  82. if repl != nil && len(*repl) > 0 {
  83. data = *repl
  84. }
  85. return
  86. }
  87. func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
  88. if qstr != "" {
  89. if highlight {
  90. ws := []string{}
  91. for _, w := range strings.Split(findfields, ",") {
  92. ws = append(ws, fmt.Sprintf(highlightStr, w, count))
  93. }
  94. qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  95. }
  96. if len(fields) > 0 {
  97. qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
  98. }
  99. if len(order) > 0 {
  100. qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  101. }
  102. if start > -1 {
  103. qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
  104. }
  105. global.Logger.Info("GetAllByNgram方法es查询语句:" + qstr)
  106. return Es.Get(index, itype, qstr)
  107. } else {
  108. return nil
  109. }
  110. }