commonSearch.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package common
  2. import (
  3. qutil "app.yhyue.com/moapp/jybase/common"
  4. "fmt"
  5. "strings"
  6. "time"
  7. )
  8. // GetCommonQuerySqlWithAggs 此方法用于聚合查询
  9. func (mae *MarketAnalysisEntity) GetCommonQuerySqlWithAggs() string {
  10. return fmt.Sprintf(mae.GetCommonQuerySql(), `,"aggs":{%s},"size":0`)
  11. }
  12. // GetCommonQuerySql 公共筛选
  13. func (mae *MarketAnalysisEntity) GetCommonQuerySql() string {
  14. var musts, bools []string
  15. //时间
  16. musts = append(musts, fmt.Sprintf(`{"range":{"firsttime":{"gte":%d,"lte":%d}}}`, time.Now().AddDate(-1, 0, 0), time.Now().Unix()))
  17. //地区
  18. /*if len(mae.FormatParam.Area) > 0 || len(mae.FormatParam.City) > 0 {
  19. var areaCity []string
  20. if len(mae.FormatParam.Area) > 0 {
  21. areaCity = append(areaCity, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, strings.Join(mae.FormatParam.Area, `","`)))
  22. }
  23. if len(mae.FormatParam.City) > 0 {
  24. areaCity = append(areaCity, fmt.Sprintf(`{"terms":{"city":["%s"]}}`, strings.Join(mae.FormatParam.City, `","`)))
  25. }
  26. musts = append(musts, fmt.Sprintf(`{"bool":{"should":[%s],"minimum_should_match": 1}}`, strings.Join(areaCity, ",")))
  27. }
  28. //行业
  29. if len(mae.FormatParam.Industry) > 0 {
  30. musts = append(musts, fmt.Sprintf(`{"terms":{"subscopeclass":["%s"]}}`, strings.Join(mae.FormatParam.Industry, `","`)))
  31. }
  32. //类型
  33. if len(mae.FormatParam.BuyerClass) > 0 {
  34. musts = append(musts, fmt.Sprintf(`{"terms":{"buyerclass":["%s"]}}`, strings.Join(mae.FormatParam.BuyerClass, `","`)))
  35. }*/
  36. //分析报告中标状态限制
  37. musts = append(musts, fmt.Sprintf(query_bool_must, PSearch_DecMust))
  38. //订阅词
  39. for _, v := range getAllKeywordArr(mae.FormatParam.KeysItems) {
  40. if sql := getKeyWordSql(v); sql != "" {
  41. bools = append(bools, sql)
  42. }
  43. }
  44. return fmt.Sprintf(`{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}%s}`, strings.Join(musts, ","), strings.Join(bools, ","), qutil.If(len(bools) > 0, 1, 0).(int), "%s")
  45. }
  46. // getAllKeywordArr 获取所有匹配词
  47. func getAllKeywordArr(res []keyWordGroup) (rData []viewKeyWord) {
  48. for _, kwg := range res {
  49. rData = append(rData, getGroupKeywordArr(kwg.A_Key)...)
  50. }
  51. return
  52. }
  53. // getGroupKeywordArr 模糊拆分为多个精准匹配
  54. func getGroupKeywordArr(res []viewKeyWord) (rData []viewKeyWord) {
  55. for _, kw := range res {
  56. if kw.MatchWay == 1 {
  57. for _, kk := range kw.Keyword {
  58. rData = append(rData, viewKeyWord{
  59. Keyword: []string{kk},
  60. Exclude: kw.Exclude,
  61. })
  62. }
  63. for _, kk := range kw.Appended {
  64. rData = append(rData, viewKeyWord{
  65. Keyword: []string{kk},
  66. Exclude: kw.Exclude,
  67. })
  68. }
  69. } else {
  70. rData = append(rData, kw)
  71. }
  72. }
  73. return
  74. }
  75. func getKeyWordSql(v viewKeyWord) string {
  76. var shoulds, must_not []string
  77. //附加词
  78. for _, vv := range v.Keyword {
  79. vv = strings.TrimSpace(vv)
  80. if vv == "" {
  81. continue
  82. }
  83. shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  84. }
  85. for _, vv := range v.Appended {
  86. vv = strings.TrimSpace(vv)
  87. if vv == "" {
  88. continue
  89. }
  90. shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  91. }
  92. //排除词
  93. for _, vv := range v.Exclude {
  94. vv = strings.TrimSpace(vv)
  95. if vv == "" {
  96. continue
  97. }
  98. must_not = append(must_not, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
  99. }
  100. //添加
  101. if len(shoulds) > 0 {
  102. notStr := ""
  103. if len(must_not) > 0 {
  104. notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
  105. }
  106. return fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr)
  107. }
  108. return ""
  109. }