projects.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "fmt"
  4. "sfbase/core"
  5. "sfbase/elastic"
  6. "sfbase/global"
  7. "sfis/db"
  8. "strconv"
  9. "strings"
  10. )
  11. var (
  12. pjt_count = 100
  13. pjt_field = `"_id","area","city","bidamount","firsttime","projectname","s_winner","buyer","buyerclass"`
  14. pjt_sort = `{"firsttime":-1}`
  15. query = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
  16. 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%%"}}`
  17. query_winner = `{"term": {"s_winner": "%s"}}`
  18. query_time = `{"range": {"firsttime": {%s}}}`
  19. SR = strings.Replace
  20. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  21. highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
  22. )
  23. func ProjectListData(projectName, winner, time string) (data *map[string]interface{}) {
  24. global.Logger.Info("projectName " + projectName)
  25. global.Logger.Info("winner " + winner)
  26. global.Logger.Info("time " + time)
  27. time = strings.TrimSpace(time)
  28. winner = strings.TrimSpace(winner)
  29. pjt_name := strings.TrimSpace(projectName)
  30. pjt_len := len([]rune(pjt_name))
  31. data = &map[string]interface{}{}
  32. qstr := ""
  33. timestr := ""
  34. if time != "" {
  35. start := strings.Split(time, "_")[0]
  36. end := strings.Split(time, "_")[1]
  37. if start == "" && end != "" {
  38. timestr = `{"range": {"firsttime": {"lte": ` + end + `}}}`
  39. } else if start != "" && end == "" {
  40. timestr = `{"range": {"firsttime": {"gte": ` + start + `}}}`
  41. } else if start != "" && end != "" {
  42. timestr = `{"range": {"firsttime": {"gte": ` + start + `,"lte": ` + end + `}}}`
  43. }
  44. }
  45. if pjt_len >= 4 && winner == "" {
  46. qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name))
  47. if timestr != "" {
  48. qstr = fmt.Sprintf(query, timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  49. }
  50. } else if pjt_len >= 4 && winner != "" {
  51. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name))
  52. if timestr != "" {
  53. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  54. }
  55. } else if winner != "" {
  56. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "")
  57. if timestr != "" {
  58. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "")
  59. }
  60. } else {
  61. return
  62. }
  63. INDEX := core.GetStringConf("es.project.index")
  64. TYPE := core.GetStringConf("es.project.itype")
  65. Es := db.GetEs()
  66. global.Logger.Info("INDEX " + INDEX)
  67. global.Logger.Info("TYPE " + TYPE)
  68. repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, pjt_field, 0, pjt_count, 0, false)
  69. if repl != nil && len(*repl) > 0 {
  70. (*data)["data"] = repl
  71. }
  72. return
  73. }
  74. func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
  75. if qstr != "" {
  76. if highlight {
  77. ws := []string{}
  78. for _, w := range strings.Split(findfields, ",") {
  79. ws = append(ws, fmt.Sprintf(highlightStr, w, count))
  80. }
  81. qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  82. }
  83. if len(fields) > 0 {
  84. qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
  85. }
  86. if len(order) > 0 {
  87. qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  88. }
  89. if start > -1 {
  90. qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
  91. }
  92. global.Logger.Info("GetAllByNgram:" + qstr)
  93. return Es.Get(index, itype, qstr)
  94. } else {
  95. return nil
  96. }
  97. }