es.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package es
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. elastic "app.yhyue.com/moapp/jybase/es"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. )
  9. const (
  10. queryBoolMustTermDomain = `{"bool": {"must": [{ "term": {"bid_field": "%s" }}]}}` // 领域化数据类型
  11. multiMatch = `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s]}}`
  12. query = `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
  13. queryBoolShould = `{"bool":{"should":[%s],"minimum_should_match": 1}}`
  14. queryBoolMustBoolShould = `{"bool":{"must":[{"range":{"bidamount":{%s}}}]}},{"bool":{"must":[{"range":{"budget":{%s}}}],"must_not":[{"range":{"bidamount":{"gte":-1}}}]}}`
  15. queryBoolMust = `{"bool":{"must":[{"terms":{"s_subscopeclass":[%s]}}]}}`
  16. queryBoolMustTerm = `{"bool": {"must": [{ "term": {"isValidFile": %d }}]}}`
  17. queryMissing = `{"constant_score":{"filter":{"missing":{"field":"%s"}}}}`
  18. gte = `"gte": %s`
  19. lte = `"lte": %s`
  20. HighlightStr = `"%s": {"fragment_size": %d,"number_of_fragments": 1}`
  21. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  22. INDEX = "bidding"
  23. TYPE = "bidding"
  24. BidSearchSort = `{"dataweight":-1,"publishtime":-1}`
  25. BidSearchFieldBase = `"_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","buyer","winner","bidopentime"` //搜索列表基础字段
  26. BidSearchFieldOfVip = BidSearchFieldBase + `,"buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist"` //付费列表字段
  27. BidSearchFieldFile = `,"isValidFile"` //根据配置开关 选择是否显示 是否有附件提示,IC.C.FileSignBool
  28. BidSearchDomainField = BidSearchFieldOfVip + `,"purchasing"` //领域数据字段基本字段
  29. //DefaultFields = `"title"` //最新招标信息
  30. )
  31. var (
  32. SR = strings.Replace
  33. //信息类型 一级类型参数和数据类型转换
  34. topTypeMap = map[string]string{
  35. "招标预告": "预告",
  36. "招标公告": "招标",
  37. "招标结果": "结果",
  38. "招标信用信息": "其它",
  39. "拟建项目": "拟建",
  40. "采购意向": "采购意向",
  41. }
  42. )
  43. type SearchByES struct {
  44. Index string
  45. IType string
  46. Query string
  47. FindFields string
  48. Order string
  49. Fields string
  50. Start int
  51. Limit int
  52. Count int
  53. HighLight bool
  54. }
  55. // GetAllByNgramWithCount 获取es查询结果及总数量
  56. func (e *SearchByES) GetAllByNgramWithCount() (int64, *[]map[string]interface{}) {
  57. if e.Query != "" {
  58. queryStr := e.Query
  59. if e.HighLight {
  60. var ws []string
  61. for _, w := range strings.Split(e.FindFields, ",") {
  62. ws = append(ws, fmt.Sprintf(HighlightStr, w, e.Count))
  63. }
  64. queryStr = queryStr[:len(queryStr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  65. }
  66. if len(e.Fields) > 0 {
  67. queryStr = queryStr[:len(queryStr)-1] + `,"_source":[` + e.Fields + "]}"
  68. }
  69. if len(e.Order) > 0 {
  70. queryStr = queryStr[:len(queryStr)-1] + `,"sort":[` + SR(SR(SR(SR(e.Order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  71. }
  72. if e.Start > -1 {
  73. queryStr = queryStr[:len(queryStr)-1] + `,"from":` + strconv.Itoa(e.Start) + `,"size":` + strconv.Itoa(e.Limit) + "}"
  74. }
  75. logx.Info("queryStr:", queryStr)
  76. return elastic.GetWithCount(e.Index, e.IType, queryStr)
  77. } else {
  78. return 0, nil
  79. }
  80. }