commonSearch.go 3.7 KB

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