utiltag.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. package util
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "models"
  7. "qfw/util"
  8. "qfw/util/elastic"
  9. "regexp"
  10. sql "sqlmodel"
  11. "strings"
  12. "time"
  13. // "go.mongodb.org/mongo-driver/bson/primitive"
  14. "gopkg.in/mgo.v2/bson"
  15. )
  16. const (
  17. TitleMatchType = "1" //标题匹配
  18. DetailMatchType = "2" //全文匹配
  19. PurchasingMatchType = "3" //标的物匹配
  20. AttachmentsMatchType = "4" //附件匹配
  21. ProjectnameMatchType = "5" //项目名称匹配
  22. BuyerMatchType = "6" //采购单位匹配
  23. SWinnerMatchType = "7" //中标单位匹配
  24. BuyerMatchType1 = "8" //采购单位(正则)匹配
  25. SWinnerMatchType1 = "9" //中标单位(正则)匹配
  26. TitleMatchTypeStr = "title" //标题匹配
  27. DetailMatchTypeStr = "detail" //全文匹配
  28. PurchasingMatchTypeStr = "purchasing" //标的物匹配
  29. AttachmentsMatchTypeStr = "filetext" //附件匹配
  30. ProjectnameMatchTypeStr = "projectname" //项目名称匹配
  31. BuyerMatchTypeStr = "buyer" //项目名称匹配
  32. SWinnerMatchTypeStr = "s_winner" //项目名称匹配
  33. TitleMatchTypeField = "title" //标题匹配
  34. DetailMatchTypeField = "detail" //全文匹配
  35. PurchasingMatchTypeField = "purchasing" //标的物匹配
  36. AttachmentsMatchTypeField = "filetext" //附件匹配
  37. ProjectnameMatchTypeField = "projectname" //项目名称匹配
  38. BuyerMatchTypeField = "buyer" //项目名称匹配
  39. SWinnerMatchTypeField = "s_winner" //项目名称匹配
  40. MultiMatchType = "phrase"
  41. Standard = 1 //1:标准字段包,
  42. Advanced = 2 //2:高级字段包
  43. Standardstr = "standard" //1:标准字段包
  44. Advancedstr = "advanced" //2:高级字段包
  45. Url = "https://www.jianyu360.com/article/content/%s.html"
  46. )
  47. func UtilEsSaveData(sdataid string, datas *[]map[string]interface{}) error {
  48. defer util.Catch()
  49. Mgo.Del("tagsdata", bson.M{"s_dataid": sdataid})
  50. if Mgo.SaveBulk("tagsdata", *datas...) {
  51. return nil
  52. } else {
  53. return errors.New("mongo save err")
  54. }
  55. }
  56. //客户规则
  57. func UtilEsFind1(tags map[string]interface{}) (error, int64) {
  58. defer util.Catch()
  59. sdataid := util.ObjToString(tags["s_dataid"])
  60. esquery := util.ObjToString(tags["s_esquery"])
  61. if len(esquery) < 1 || len(sdataid) < 1 {
  62. return errors.New("s_esquery or s_dataid no found"), 0
  63. }
  64. i_maxnum := util.Int64All(tags["i_maxnum"])
  65. if i_maxnum <= 0 {
  66. i_maxnum = 100
  67. }
  68. maths := make([]map[string]string, 0)
  69. if orules, ok := tags["o_rules"].([]interface{}); ok {
  70. for _, v := range orules {
  71. orule, _ := v.(map[string]interface{})
  72. maths = append(maths, map[string]string{
  73. "s_matchkey": util.ObjToString(orule["s_matchkey"]),
  74. "s_keymatch": util.ObjToString(orule["s_keymatch"]),
  75. })
  76. }
  77. } else if orules, ok := tags["o_rules"].([]map[string]interface{}); ok {
  78. for _, v := range orules {
  79. maths = append(maths, map[string]string{
  80. "s_matchkey": util.ObjToString(v["s_matchkey"]),
  81. "s_keymatch": util.ObjToString(v["s_keymatch"]),
  82. })
  83. }
  84. } else {
  85. return errors.New("o_rules no found"), 0
  86. }
  87. client := elastic.GetEsConn()
  88. defer elastic.DestoryEsConn(client)
  89. esquery = esquery[:len(esquery)-1] + `,"size":` + fmt.Sprintf("%d", i_maxnum) + `}`
  90. searchResult, err := client.Search(EsIndex).Source(esquery).Do()
  91. if err == nil && searchResult.Hits != nil {
  92. datas := make([]map[string]interface{}, 0)
  93. util.Debug("es查询到的数量:", searchResult.Hits.TotalHits)
  94. for _, v := range searchResult.Hits.Hits {
  95. item := make(map[string]interface{})
  96. if json.Unmarshal(*v.Source, &item) == nil {
  97. delete(item, "_id")
  98. item["info_id"] = v.Id
  99. item["s_dataid"] = sdataid
  100. item["s_jyhref"] = fmt.Sprintf(Url, util.CommonEncodeArticle("content", v.Id))
  101. item["i_createtime"] = time.Now().Unix()
  102. var d *DFA = &DFA{}
  103. var analyKeys []string //找到的关键词
  104. var matchType []string //匹配方式
  105. for _, math := range maths {
  106. fileds := strsToArr(math["s_keymatch"], "field")
  107. d.AddWord(strings.Split(math["s_matchkey"], ",")...)
  108. mkMap := make(map[string]interface{})
  109. tmpMap := make(map[string]interface{})
  110. for _, mk := range strings.Split(math["s_matchkey"], ",") {
  111. if strings.Contains(mk, "&&") {
  112. arr := strings.Split(mk, "&&")
  113. for _, s := range arr {
  114. if s != "" {
  115. tmpMap[s] = mk
  116. if b, _ := regexp.MatchString("[A-Z]", s); b {
  117. mkMap[strings.ToLower(s)] = s
  118. d.AddWord(strings.ToLower(s))
  119. } else {
  120. d.AddWord(s)
  121. }
  122. }
  123. }
  124. } else {
  125. if b, _ := regexp.MatchString("[A-Z]", mk); b {
  126. mkMap[strings.ToLower(mk)] = mk
  127. d.AddWord(strings.ToLower(mk))
  128. } else {
  129. d.AddWord(mk)
  130. }
  131. }
  132. }
  133. for _, filed := range fileds {
  134. filed1 := strings.ToLower(util.ObjToString(item[filed]))
  135. ddds := d.Analy(filed1)
  136. analyKeys = append(analyKeys, ddds...)
  137. }
  138. if len(analyKeys) > 0 {
  139. matchType = append(matchType, strings.Join(fileds, ","))
  140. for k, v := range analyKeys {
  141. if tmpMap[v] != "" && tmpMap[v] != nil {
  142. analyKeys[k] = util.ObjToString(tmpMap[v])
  143. }
  144. }
  145. for _, v1 := range analyKeys {
  146. if mkMap[v1] != "" && mkMap[v1] != nil {
  147. analyKeys = deleteSlice(analyKeys, v1)
  148. analyKeys = append(analyKeys, util.ObjToString(mkMap[v1]))
  149. }
  150. }
  151. }
  152. d.Clear()
  153. }
  154. //去重
  155. ssavekey := make(map[string]bool)
  156. for _, v := range analyKeys {
  157. ssavekey[v] = true
  158. }
  159. ssavekeys := []string{}
  160. for k := range ssavekey {
  161. ssavekeys = append(ssavekeys, k)
  162. }
  163. item["s_matchkey"] = GetMactchKey(maths, item)
  164. item["s_matchtype"] = strings.Join(matchType, ",")
  165. findwinner := strings.TrimSpace(util.ObjToString(item["winner"]))
  166. if findwinner != "" {
  167. finddata := MgoEnps.FindOne(EnpsColl, bson.M{"company_name": findwinner})
  168. if finddata != nil {
  169. if legal_person := util.ObjToString(finddata["legal_person"]); legal_person != "" {
  170. item["legal_person"] = legal_person
  171. }
  172. if email := util.ObjToString(finddata["company_email"]); email != "" {
  173. item["company_email"] = email
  174. }
  175. if phone := util.ObjToString(finddata["company_phone"]); phone != "" {
  176. item["company_phone"] = phone
  177. }
  178. //从最新年报中获取 中标单位联系电话、中标单位邮箱
  179. // if annual_reports, ok := finddata["annual_reports"].(primitive.A); ok && len(annual_reports) > 0 {
  180. // report_year := Sort_year_report(annual_reports) //最新年报
  181. // if len(report_year) > 0 {
  182. // if email := util.ObjToString(report_year["company_email"]); email != "" {
  183. // item["company_email"] = email
  184. // }
  185. // if phone := util.ObjToString(report_year["company_phone"]); phone != "" {
  186. // item["company_phone"] = phone
  187. // }
  188. // }
  189. // }
  190. item["qyk"] = finddata
  191. }
  192. }
  193. datas = append(datas, item)
  194. }
  195. }
  196. Mgo.Update("cuserdepartrule", bson.M{"_id": tags["_id"]}, bson.M{
  197. "$set": bson.M{
  198. "i_estotal": searchResult.Hits.TotalHits,
  199. }}, false, false)
  200. count := searchResult.Hits.TotalHits
  201. return UtilEsSaveData(sdataid, &datas), count
  202. } else {
  203. util.Debug(err)
  204. return err, 0
  205. }
  206. }
  207. func Utiltags(tag map[string]interface{}) string {
  208. defer util.Catch()
  209. tmpbyte, _ := json.Marshal(tag)
  210. tab := models.Tag{}
  211. err := json.Unmarshal(tmpbyte, &tab)
  212. if err != nil {
  213. return "json err:" + err.Error()
  214. }
  215. QueryObjecct := sql.QueryObjecct{}
  216. ffBoolObject := sql.BoolObject{}
  217. adsBoolObect := sql.NewEsObject{}
  218. if tab.Sarea != "" {
  219. adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{Area: strings.Split(tab.Sarea, ",")}})
  220. }
  221. if tab.Scity != "" {
  222. adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{City: strings.Split(tab.Scity, ",")}})
  223. }
  224. if tab.Sdistrict != "" {
  225. //城市——区县
  226. cityds := strings.Split(tab.Sdistrict, ",")
  227. var ds []string
  228. for _, v := range cityds {
  229. ds = append(ds, strings.Split(v, "-")[1])
  230. }
  231. adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{District: ds}})
  232. }
  233. if len(adsBoolObect.Bool.Should) > 0 {
  234. ffBoolObject.Must = append(ffBoolObject.Must, adsBoolObect)
  235. }
  236. if tab.Stoptype != "" || tab.Ssubtype != "" {
  237. if len(tab.Stoptype) > 0 {
  238. toptypeSubtype := sql.ToptypeSubtype{}
  239. toptypeSubtype.Toptype = strings.Split(tab.Stoptype, ",")
  240. ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype})
  241. }
  242. if len(tab.Ssubtype) > 0 {
  243. toptypeSubtype := sql.ToptypeSubtype{}
  244. toptypeSubtype.Subtype = strings.Split(tab.Ssubtype, ",")
  245. ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype})
  246. }
  247. }
  248. if tab.Istarttime > 0 && tab.Iendtime > 0 {
  249. ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime: &sql.Publishtime{
  250. Gte: tab.Istarttime,
  251. Lt: tab.Iendtime,
  252. }}})
  253. } else if tab.Istarttime > 0 {
  254. ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime1: &sql.Publishtime1{
  255. Gte: tab.Istarttime,
  256. }}})
  257. } else if tab.Iendtime > 0 {
  258. ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime2: &sql.Publishtime2{
  259. Lt: tab.Iendtime,
  260. }}})
  261. }
  262. if tab.Sbudget != "" {
  263. if strings.Contains(tab.Sbudget, "大于") && strings.Contains(tab.Sbudget, "小于") {
  264. arr := strings.Split(tab.Sbudget, ",")
  265. limit := &sql.BudgetOrBidamount{
  266. Gte: util.Float64All(strings.Replace(arr[0], "大于", "", -1)),
  267. Lt: util.Float64All(strings.Replace(arr[1], "小于", "", -1)),
  268. }
  269. ffBoolObject.Must = append(ffBoolObject.Must, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
  270. } else if strings.Contains(tab.Sbudget, "大于") {
  271. limit := &sql.BudgetOrBidamount{
  272. Gte: util.Float64All(strings.Replace(tab.Sbudget, "大于", "", -1)),
  273. }
  274. ffBoolObject.Must = append(ffBoolObject.Must, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
  275. } else if strings.Contains(tab.Sbudget, "小于") {
  276. limit := &sql.BudgetOrBidamount{
  277. Lt: util.Float64All(strings.Replace(tab.Sbudget, "小于", "", -1)),
  278. }
  279. ffBoolObject.Must = append(ffBoolObject.Must, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
  280. }
  281. }
  282. if tab.Sbidamount != "" {
  283. if strings.Contains(tab.Sbidamount, "大于") && strings.Contains(tab.Sbidamount, "小于") {
  284. arr := strings.Split(tab.Sbidamount, ",")
  285. limit := &sql.BudgetOrBidamount{
  286. Gte: util.Float64All(strings.Replace(arr[0], "大于", "", -1)),
  287. Lt: util.Float64All(strings.Replace(arr[1], "小于", "", -1)),
  288. }
  289. ffBoolObject.Must = append(ffBoolObject.Must, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
  290. } else if strings.Contains(tab.Sbidamount, "大于") {
  291. limit := &sql.BudgetOrBidamount{
  292. Gte: util.Float64All(strings.Replace(tab.Sbidamount, "大于", "", -1)),
  293. }
  294. ffBoolObject.Must = append(ffBoolObject.Must, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
  295. } else if strings.Contains(tab.Sbidamount, "小于") {
  296. limit := &sql.BudgetOrBidamount{
  297. Lt: util.Float64All(strings.Replace(tab.Sbidamount, "小于", "", -1)),
  298. }
  299. ffBoolObject.Must = append(ffBoolObject.Must, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
  300. }
  301. }
  302. if tab.Sglobalbuyerclass != "" {
  303. if len(tab.Sglobalbuyerclass) > 0 {
  304. buyerclass := sql.BuyerclassObject{
  305. Terms: struct {
  306. Buyerclass []string `json:"buyerclass,omitempty"`
  307. }{
  308. Buyerclass: strings.Split(tab.Sglobalbuyerclass, ","),
  309. },
  310. }
  311. ffBoolObject.Must = append(ffBoolObject.Must, buyerclass)
  312. }
  313. }
  314. if tab.Sglobaltopscopeclass != "" || tab.Sglobalsubscopeclass != "" {
  315. if len(tab.Sglobaltopscopeclass) > 0 {
  316. topScopeclass := sql.Scopeclass{}
  317. topScopeclass.Globaltopscopeclass = strings.Split(tab.Sglobaltopscopeclass, ",")
  318. ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&topScopeclass})
  319. } else if len(tab.Sglobalsubscopeclass) > 0 {
  320. subScopeclass := sql.Scopeclass{}
  321. subScopeclass.Globalsubscopeclass = strings.Split(tab.Sglobalsubscopeclass, ",")
  322. ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&subScopeclass})
  323. }
  324. }
  325. fqBoolObject := sql.BoolObject{}
  326. if len(tab.Sexistfields) > 0 {
  327. tmpsfields := strings.Split(tab.Sexistfields, ",")
  328. for _, v := range tmpsfields {
  329. fqBoolObject.MustNot = append(fqBoolObject.MustNot, sql.ExistfieldsObjectMust{
  330. ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct {
  331. Missing struct {
  332. Field string `json:"field,omitempty"`
  333. } `json:"missing,omitempty"`
  334. }{
  335. Missing: struct {
  336. Field string `json:"field,omitempty"`
  337. }{Field: v},
  338. },
  339. },
  340. })
  341. }
  342. }
  343. newEsObject := sql.NewEsObject{}
  344. if tab.Sglobaladdkey != "" && tab.Sglobaladdkeymatch != "" {
  345. if tmps := strsToArr(tab.Sglobaladdkeymatch, "str"); tmps != nil {
  346. newEsObject = method1(newEsObject, tab.Sglobaladdkey, tab.Sglobaladdkeymatch, tmps)
  347. }
  348. }
  349. if len(newEsObject.Bool.Should) > 0 || len(newEsObject.Bool.MustNot) > 0 || len(newEsObject.Bool.Must) > 0 {
  350. fqBoolObject.Must = append(fqBoolObject.Must, newEsObject)
  351. }
  352. nots := sql.NewEsObject{}
  353. if tab.Sglobalnotkey != "" && tab.Sglobalnotkeymatch != "" {
  354. if tmps := strsToArr(tab.Sglobalnotkeymatch, "str"); tmps != nil {
  355. nots = method1(nots, tab.Sglobalnotkey, tab.Sglobalnotkeymatch, tmps)
  356. }
  357. }
  358. if len(nots.Bool.Should) > 0 {
  359. fqBoolObject.MustNot = append(fqBoolObject.MustNot, nots)
  360. }
  361. torules := sql.BoolObject{}
  362. for _, v := range tab.Orules {
  363. tmpses := sql.NewEsObject{}
  364. if skeymatchs := strsToArr(v.Skeymatch, "str"); skeymatchs != nil {
  365. tmpnewEsObject := sql.NewEsObject{}
  366. tmpnewEsObject = method1(tmpnewEsObject, v.Smatchkey, v.Skeymatch, skeymatchs)
  367. if len(tmpnewEsObject.Bool.Should) > 0 || len(tmpnewEsObject.Bool.Must) > 0 || len(tmpnewEsObject.Bool.MustNot) > 0 {
  368. tmpses.Bool.Must = append(tmpses.Bool.Must, tmpnewEsObject)
  369. }
  370. }
  371. if saddkeymatchs := strsToArr(v.Saddkeymatch, "str"); saddkeymatchs != nil {
  372. addkeyarr := sql.NewEsObject{}
  373. addkeyarr = method1(addkeyarr, v.Saddkey, v.Saddkeymatch, saddkeymatchs)
  374. if len(addkeyarr.Bool.Should) > 0 {
  375. tmpses.Bool.Must = append(tmpses.Bool.Must, addkeyarr)
  376. }
  377. }
  378. if snotkeymatchs := strsToArr(v.Snotkeymatch, "str"); snotkeymatchs != nil {
  379. tmpses = method2(tmpses, v.Snotkey, v.Snotkeymatch, snotkeymatchs)
  380. }
  381. if len(v.Stopscopeclass) > 0 {
  382. sArr := strings.Split(v.Stopscopeclass, ",")
  383. tmpses.Bool.Must = append(tmpses.Bool.Must, sql.TopscopeclassObject{
  384. Terms: struct {
  385. Topscopeclass []string `json:"s_topscopeclass,omitempty"`
  386. }{
  387. Topscopeclass: sArr,
  388. },
  389. })
  390. } else if len(v.Ssubscopeclass) > 0 {
  391. tmpses.Bool.Must = append(tmpses.Bool.Must, sql.SubscopeclassObject{
  392. Terms: struct {
  393. Subscopeclass []string `json:"s_subscopeclass,omitempty"`
  394. }{
  395. Subscopeclass: strings.Split(v.Ssubscopeclass, ","),
  396. },
  397. })
  398. }
  399. if len(v.Sbuyerclass) > 0 {
  400. tmpses.Bool.Must = append(tmpses.Bool.Must, sql.BuyerclassObject{
  401. Terms: struct {
  402. Buyerclass []string `json:"buyerclass,omitempty"`
  403. }{
  404. Buyerclass: strings.Split(v.Sbuyerclass, ","),
  405. },
  406. })
  407. }
  408. torules.Should = append(torules.Should, tmpses)
  409. }
  410. if len(ffBoolObject.Must) > 0 || len(ffBoolObject.MustNot) > 0 || len(ffBoolObject.Should) > 0 {
  411. QueryObjecct.Filtered.Filter = &sql.Filter{}
  412. QueryObjecct.Filtered.Filter.Bool = &ffBoolObject
  413. }
  414. if len(fqBoolObject.Must) > 0 || len(fqBoolObject.MustNot) > 0 || len(fqBoolObject.Should) > 0 {
  415. QueryObjecct.Filtered.Query = &sql.Query{}
  416. QueryObjecct.Filtered.Query.Bool = &fqBoolObject
  417. if len(torules.Should) > 0 {
  418. QueryObjecct.Filtered.Query.Bool.Must = append(QueryObjecct.Filtered.Query.Bool.Must, map[string]interface{}{
  419. "bool": torules,
  420. })
  421. }
  422. } else if len(torules.Should) > 0 {
  423. if QueryObjecct.Filtered.Query == nil {
  424. QueryObjecct.Filtered.Query = &sql.Query{}
  425. QueryObjecct.Filtered.Query.Bool = &fqBoolObject
  426. }
  427. QueryObjecct.Filtered.Query.Bool.Must = append(QueryObjecct.Filtered.Query.Bool.Must, map[string]interface{}{
  428. "bool": torules,
  429. })
  430. }
  431. rdata := make(map[string]interface{})
  432. rdata["query"] = QueryObjecct
  433. esbytes, _ := json.Marshal(rdata)
  434. esStr := strings.Replace(string(esbytes), "regexp1", "regexp", -1)
  435. esStr = strings.Replace(esStr, "publishtime1", "publishtime", -1)
  436. esStr = strings.Replace(esStr, "publishtime2", "publishtime", -1)
  437. return strings.Replace(esStr, "regexp2", "regexp", -1)
  438. }
  439. func strsToArr(strs string, typestr string) []string {
  440. if len(strs) > 0 {
  441. tmps := strings.Split(strs, ",")
  442. switch typestr {
  443. case "str":
  444. for index, vint := range tmps {
  445. switch vint {
  446. case TitleMatchType:
  447. tmps[index] = TitleMatchTypeStr
  448. case DetailMatchType:
  449. tmps[index] = DetailMatchTypeStr
  450. case PurchasingMatchType:
  451. tmps[index] = PurchasingMatchTypeStr
  452. case AttachmentsMatchType:
  453. tmps[index] = AttachmentsMatchTypeStr
  454. case ProjectnameMatchType:
  455. tmps[index] = ProjectnameMatchTypeStr
  456. case BuyerMatchType:
  457. tmps[index] = BuyerMatchTypeStr
  458. case SWinnerMatchType:
  459. tmps[index] = SWinnerMatchTypeStr
  460. case BuyerMatchType1:
  461. tmps[index] = BuyerMatchTypeStr
  462. case SWinnerMatchType1:
  463. tmps[index] = SWinnerMatchTypeStr
  464. }
  465. }
  466. return tmps
  467. case "field":
  468. for index, vint := range tmps {
  469. switch vint {
  470. case TitleMatchType:
  471. tmps[index] = TitleMatchTypeField
  472. case DetailMatchType:
  473. tmps[index] = DetailMatchTypeField
  474. case PurchasingMatchType:
  475. tmps[index] = PurchasingMatchTypeField
  476. case AttachmentsMatchType:
  477. tmps[index] = AttachmentsMatchTypeField
  478. case ProjectnameMatchType:
  479. tmps[index] = ProjectnameMatchTypeField
  480. case BuyerMatchType:
  481. tmps[index] = BuyerMatchTypeField
  482. case SWinnerMatchType:
  483. tmps[index] = SWinnerMatchTypeField
  484. case BuyerMatchType1:
  485. tmps[index] = BuyerMatchTypeField
  486. case SWinnerMatchType1:
  487. tmps[index] = SWinnerMatchTypeField
  488. }
  489. }
  490. return tmps
  491. }
  492. }
  493. return nil
  494. }
  495. func init() {
  496. EsIndex = util.ObjToString(Sysconfig["elasticsearch_index"])
  497. EsType = util.ObjToString(Sysconfig["elasticsearch_type"])
  498. }
  499. func method1(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject {
  500. for _, vv := range strings.Split(keyword, ",") {
  501. if vv == "" {
  502. continue
  503. }
  504. if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) {
  505. //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选
  506. strs := ""
  507. if strings.Contains(vv, "&&") {
  508. strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*"
  509. } else {
  510. strs = ".*" + vv + ".*"
  511. }
  512. if tmps[0] == "buyer" {
  513. newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.Regular{
  514. Regexp1: &sql.Regular_Buyer{Buyer: strs},
  515. })
  516. } else if tmps[0] == "s_winner" {
  517. newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.Regular{
  518. Regexp2: &sql.Regular_winner{Winner: strs},
  519. })
  520. }
  521. } else {
  522. addkeylines := strings.Split(vv, "&&")
  523. if len(addkeylines) > 1 {
  524. addkeyline := sql.NewEsObject{}
  525. for _, vvv := range addkeylines {
  526. if vvv == "" {
  527. continue
  528. }
  529. addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.ShouldObj{
  530. MultiMatch: &sql.MultiMatch{
  531. Query: vvv,
  532. Type: MultiMatchType,
  533. Fields: tmps,
  534. },
  535. })
  536. }
  537. if len(addkeyline.Bool.Must) > 0 {
  538. newEsObject.Bool.Should = append(newEsObject.Bool.Should, addkeyline)
  539. }
  540. } else {
  541. newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{
  542. MultiMatch: &sql.MultiMatch{
  543. Query: vv,
  544. Type: MultiMatchType,
  545. Fields: tmps,
  546. },
  547. })
  548. }
  549. }
  550. }
  551. return newEsObject
  552. }
  553. func method2(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject {
  554. for _, vv := range strings.Split(keyword, ",") {
  555. if vv == "" {
  556. continue
  557. }
  558. if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) {
  559. //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选
  560. strs := ""
  561. if strings.Contains(vv, "&&") {
  562. strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*"
  563. } else {
  564. strs = ".*" + vv + ".*"
  565. }
  566. if tmps[0] == "buyer" {
  567. newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.Regular{
  568. Regexp1: &sql.Regular_Buyer{Buyer: strs},
  569. })
  570. } else if tmps[0] == "s_winner" {
  571. newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.Regular{
  572. Regexp2: &sql.Regular_winner{Winner: strs},
  573. })
  574. }
  575. } else {
  576. addkeylines := strings.Split(vv, "&&")
  577. if len(addkeylines) > 1 {
  578. addkeyline := sql.NewEsObject{}
  579. for _, vvv := range addkeylines {
  580. if vvv == "" {
  581. continue
  582. }
  583. addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.ShouldObj{
  584. MultiMatch: &sql.MultiMatch{
  585. Query: vvv,
  586. Type: MultiMatchType,
  587. Fields: tmps,
  588. },
  589. })
  590. }
  591. if len(addkeyline.Bool.Must) > 0 {
  592. newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, addkeyline)
  593. }
  594. } else {
  595. newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{
  596. MultiMatch: &sql.MultiMatch{
  597. Query: vv,
  598. Type: MultiMatchType,
  599. Fields: tmps,
  600. },
  601. })
  602. }
  603. }
  604. }
  605. return newEsObject
  606. }
  607. /**
  608. * 一级公告行业处理 d、t、p
  609. */
  610. func method3(arr []string) []string {
  611. var sArr []string
  612. for _, v := range arr {
  613. sArr = append(sArr, v+"d")
  614. sArr = append(sArr, v+"t")
  615. sArr = append(sArr, v+"p")
  616. }
  617. return sArr
  618. }
  619. func GetMactchKey(match []map[string]string, data map[string]interface{}) string {
  620. keyWord := []string{}
  621. for _, keys := range match {
  622. types := keys["s_keymatch"]
  623. key := keys["s_matchkey"]
  624. if strings.Contains(types, "1") {
  625. title := util.ObjToString(data["title"])
  626. keyWord = KeyWordToData(types, title, key, keyWord)
  627. }
  628. if strings.Contains(types, "2") {
  629. detail := util.ObjToString(data["detail"])
  630. keyWord = KeyWordToData(types, detail, key, keyWord)
  631. }
  632. if strings.Contains(types, "3") {
  633. purchasing := util.ObjToString(data["purchasing"])
  634. keyWord = KeyWordToData(types, purchasing, key, keyWord)
  635. }
  636. if strings.Contains(types, "4") {
  637. filetext := util.ObjToString(data["filetext"])
  638. keyWord = KeyWordToData(types, filetext, key, keyWord)
  639. }
  640. if strings.Contains(types, "5") {
  641. projectname := util.ObjToString(data["projectname"])
  642. keyWord = KeyWordToData(types, projectname, key, keyWord)
  643. }
  644. if strings.Contains(types, "6") || strings.Contains(types, "8") {
  645. buyer := util.ObjToString(data["buyer"])
  646. keyWord = KeyWordToData(types, buyer, key, keyWord)
  647. }
  648. if strings.Contains(types, "7") || strings.Contains(types, "9") {
  649. winner := util.ObjToString(data["s_winner"])
  650. keyWord = KeyWordToData(types, winner, key, keyWord)
  651. }
  652. }
  653. keyMap := map[string]bool{}
  654. keyArr := []string{}
  655. for _, key := range keyWord {
  656. keyMap[key] = true
  657. }
  658. for k, _ := range keyMap {
  659. keyArr = append(keyArr, k)
  660. }
  661. return strings.Join(keyArr, ",")
  662. }
  663. func KeyWordToData(types, item, key string, keyWord []string) []string {
  664. for _, mk := range strings.Split(key, ",") {
  665. if strings.Contains(mk, "&&") {
  666. arr := strings.Split(mk, "&&")
  667. isok := true
  668. for _, s := range arr {
  669. if s != "" {
  670. if !strings.Contains(item, s) {
  671. isok = false
  672. }
  673. }
  674. }
  675. if isok {
  676. keyWord = append(keyWord, mk)
  677. }
  678. } else {
  679. if strings.Contains(item, mk) {
  680. keyWord = append(keyWord, mk)
  681. }
  682. }
  683. }
  684. return keyWord
  685. }