elastic.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/olivere/elastic/v7"
  7. "hongze/hongze_cygx/models"
  8. "hongze/hongze_cygx/utils"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func NewClient() (client *elastic.Client, err error) {
  15. //errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
  16. //file := ""
  17. //if utils.RunMode == "release" {
  18. // //file = `/data/rdlucklog/hongze_cygx/eslog.log`
  19. // file = `./rdlucklog/eslog.log`
  20. //} else {
  21. // file = `./rdlucklog/eslog.log`
  22. //}
  23. //logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
  24. //client, err = elastic.NewClient(
  25. // elastic.SetURL(ES_URL),
  26. // elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  27. // elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
  28. // elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
  29. client, err = elastic.NewClient(
  30. elastic.SetURL(ES_URL),
  31. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  32. elastic.SetSniff(false))
  33. return
  34. }
  35. // 创建文章阅读记录的Es索引
  36. func CreateIndexNameArticleHistory() {
  37. indexName := utils.IndexNameArticleHistory
  38. mappingJson := `{
  39. "mappings": {
  40. "dynamic": true,
  41. "properties": {
  42. "ArticleId": {
  43. "type": "integer"
  44. },
  45. "Id": {
  46. "type": "integer"
  47. },
  48. "ArticleType": {
  49. "type": "short"
  50. },
  51. "CompanyArticleHistoryNum": {
  52. "type": "integer"
  53. },
  54. "CompanyName": {
  55. "type": "keyword"
  56. },
  57. "CompanyId": {
  58. "type": "integer"
  59. },
  60. "CreateTime": {
  61. "type": "date",
  62. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  63. },
  64. "Email": {
  65. "type": "keyword"
  66. },
  67. "Mobile": {
  68. "type": "keyword"
  69. },
  70. "PublishDate": {
  71. "type": "date",
  72. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  73. },
  74. "RealName": {
  75. "type": "keyword"
  76. },
  77. "SellerName": {
  78. "type": "keyword"
  79. },
  80. "SellerId": {
  81. "type": "integer"
  82. },
  83. "StopTime": {
  84. "type": "integer"
  85. },
  86. "Title": {
  87. "type": "keyword"
  88. },
  89. "UserId": {
  90. "type": "integer"
  91. },
  92. "UserArticleHistoryNum": {
  93. "type": "integer"
  94. }
  95. }
  96. }
  97. }`
  98. EsCreateIndex(indexName, mappingJson)
  99. }
  100. // func UpdateWxUserLabel(cont context.Context) (err error) {
  101. func AddAllArticleHistory(cont context.Context) (err error) {
  102. defer func() {
  103. if err != nil {
  104. go utils.SendAlarmMsg("同步阅读记录到es失败;Err:"+err.Error(), 2)
  105. go utils.SendEmail("同步阅读记录到es失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
  106. utils.FileLog.Info("同步阅读记录到es失败,Err:%s", err.Error())
  107. }
  108. }()
  109. var updateUserIds string //更改过的用户ID
  110. userIdMap := make(map[int]int)
  111. condition := ` AND r.create_time < '` + time.Now().Format(utils.FormatDate) + `' AND r.company_id IN (
  112. SELECT a.company_id
  113. FROM company AS a INNER JOIN company_product AS b ON a.company_id = b.company_id
  114. WHERE a.enabled = 1 AND b.STATUS IN ( '正式', '试用', '冻结' )) `
  115. totalAll, err := models.GetCygxArticleHistoryCountByCompany(condition)
  116. if err != nil {
  117. fmt.Println("GetCygxArticleHistoryCountByCompany Err:totalAll", err.Error())
  118. return err
  119. }
  120. //更改阅读总数
  121. err = models.UpdateConfigByCode(strconv.Itoa(totalAll), "company_article_history_num")
  122. if err != nil {
  123. fmt.Println("UpdateConfigByCode Err:totalAll", err.Error())
  124. return err
  125. }
  126. //处理前一天新增的数据
  127. condition += ` AND r.create_time >='` + time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + `'`
  128. total, err := models.GetCygxArticleHistoryCountByCompany(condition)
  129. //fmt.Println(total)
  130. //return
  131. fmt.Println(total)
  132. if err != nil {
  133. fmt.Println("GetCygxArticleHistoryCountByCompany Err:", err.Error())
  134. return err
  135. }
  136. for i := 0; i <= total/1000; i++ {
  137. allList, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 1000*i, 1000)
  138. if err != nil {
  139. fmt.Println("GetCygxArticleHistoryRecordByCompanyList Err:", err.Error())
  140. return err
  141. }
  142. for k, v := range allList {
  143. fmt.Println(v.Id, "___", k)
  144. err := EsAddArticleHistoryData(v)
  145. if err != nil {
  146. fmt.Println("EsAddOrEditData Err:", err.Error())
  147. return err
  148. }
  149. if _, ok := userIdMap[v.UserId]; !ok {
  150. updateUserIds += strconv.Itoa(v.UserId) + ","
  151. userIdMap[v.UserId] = v.UserId
  152. }
  153. }
  154. }
  155. //处理前一天新增的数据 end
  156. //处理前一天被移动的用户
  157. startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  158. endDate := time.Now().Format(utils.FormatDate)
  159. var mobiles string
  160. listUpdateUser, err := models.GetWxUserOpLogList(startDate, endDate)
  161. if err != nil && err.Error() != utils.ErrNoRow() {
  162. return err
  163. }
  164. if len(listUpdateUser) > 0 {
  165. for _, v := range listUpdateUser {
  166. mobiles += "'" + v.Mobile + "',"
  167. }
  168. }
  169. mobiles = strings.TrimRight(mobiles, ",")
  170. if mobiles != "" {
  171. condition = ` AND r.mobile IN (` + mobiles + `)`
  172. //修改用户的阅读记录(es 自动判断,如果有他会修改数据)
  173. listUpdatePv, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 0, 0)
  174. if err != nil {
  175. fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
  176. return err
  177. }
  178. for _, v := range listUpdatePv {
  179. err := EsAddArticleHistoryData(v)
  180. if err != nil {
  181. fmt.Println("EsAddOrEditData Err:", err.Error())
  182. return err
  183. }
  184. }
  185. }
  186. //处理前一天被移动的用户 end
  187. //处理前一天被删除的用户
  188. {
  189. listDeleteUser, err := models.GetWxUserOpLogDeleteList(startDate, endDate)
  190. if err != nil && err.Error() != utils.ErrNoRow() {
  191. return err
  192. }
  193. mobiles = ""
  194. if len(listUpdateUser) > 0 {
  195. for _, v := range listDeleteUser {
  196. mobiles += "'" + v.Mobile + "',"
  197. }
  198. }
  199. mobiles = strings.TrimRight(mobiles, ",")
  200. if mobiles != "" {
  201. condition = ` AND r.mobile IN (` + mobiles + `)`
  202. listDeletePv, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 0, 0)
  203. if err != nil {
  204. fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
  205. return err
  206. }
  207. //fmt.Println("Es 删除")
  208. for _, v := range listDeletePv {
  209. err := EsDeleteData(utils.IndexNameArticleHistory, strconv.Itoa(v.Id))
  210. if err != nil {
  211. fmt.Println("EsAddOrEditData Err:", err.Error())
  212. return err
  213. }
  214. }
  215. }
  216. }
  217. //处理前一天被删除的用户 end
  218. //处理新增的阅读记录的用户阅读数量、机构阅读数量(暂未找到批量修改的方法,后期优化处理 2022.7.11)
  219. updateUserIds = strings.TrimRight(updateUserIds, ",")
  220. if updateUserIds != "" {
  221. condition = ` AND r.create_time >='` + time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + `' AND r.user_id IN (` + updateUserIds + `)`
  222. total, err := models.GetCygxArticleHistoryCountByCompany(condition)
  223. //fmt.Println(total)
  224. //return
  225. fmt.Println(total)
  226. if err != nil {
  227. fmt.Println("GetCygxArticleHistoryCountByCompany Err:", err.Error())
  228. return err
  229. }
  230. for i := 0; i <= total/1000; i++ {
  231. allList, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 1000*i, 1000)
  232. if err != nil {
  233. fmt.Println("GetCygxArticleHistoryRecordByCompanyList Err:", err.Error())
  234. return err
  235. }
  236. for k, v := range allList {
  237. fmt.Println(v.Id, "___", k)
  238. err := EsAddArticleHistoryData(v)
  239. if err != nil {
  240. fmt.Println("EsAddOrEditData Err:", err.Error())
  241. return err
  242. }
  243. if _, ok := userIdMap[v.UserId]; !ok {
  244. updateUserIds += strconv.Itoa(v.UserId) + ","
  245. userIdMap[v.UserId] = v.UserId
  246. }
  247. }
  248. }
  249. }
  250. return
  251. }
  252. // 新增数据
  253. func EsAddArticleHistoryData(item *models.EsUserInteraction) (err error) {
  254. defer func() {
  255. if err != nil {
  256. fmt.Println("EsAddOrEditData Err:", err.Error())
  257. }
  258. }()
  259. client := utils.Client
  260. resp, err := client.Index().Index(utils.IndexNameArticleHistory).Id(strconv.Itoa(item.Id)).BodyJson(item).Do(context.Background())
  261. if err != nil {
  262. fmt.Println("新增失败:", err.Error())
  263. return err
  264. }
  265. if resp.Status == 0 && resp.Result == "created" {
  266. //fmt.Println("新增成功")
  267. err = nil
  268. return err
  269. } else {
  270. fmt.Println("AddData", resp.Status, resp.Result)
  271. }
  272. return
  273. }
  274. // indexName:索引名称
  275. // mappingJson:表结构
  276. func EsCreateIndex(indexName, mappingJson string) (err error) {
  277. client := utils.Client
  278. //if err != nil {
  279. // return
  280. //}
  281. //定义表结构
  282. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  283. if err != nil {
  284. return
  285. }
  286. if !exists {
  287. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  288. //BodyJson(bodyJson).Do(context.Background())
  289. if err != nil {
  290. fmt.Println("CreateIndex Err:" + err.Error())
  291. return err
  292. }
  293. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  294. } else {
  295. fmt.Println(indexName + " 已存在")
  296. }
  297. return
  298. }
  299. // 新增和修改数据
  300. func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (err error) {
  301. defer func() {
  302. if err != nil {
  303. fmt.Println("EsAddOrEditData Err:", err.Error())
  304. }
  305. }()
  306. client := utils.Client
  307. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  308. if err != nil && !strings.Contains(err.Error(), "404") {
  309. fmt.Println("Get Err" + err.Error())
  310. return
  311. }
  312. if searchById != nil && searchById.Found {
  313. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  314. "BodyText": item.BodyText,
  315. "Title": item.Title,
  316. "PublishDate": item.PublishDate,
  317. "CategoryId": item.CategoryId,
  318. "ExpertBackground": item.ExpertBackground,
  319. "Abstract": item.Abstract,
  320. "Annotation": item.Annotation,
  321. }).Do(context.Background())
  322. if err != nil {
  323. return err
  324. }
  325. if resp.Status == 0 {
  326. fmt.Println("修改成功")
  327. } else {
  328. fmt.Println("EditData", resp.Status, resp.Result)
  329. }
  330. client.CloseIndex(indexName)
  331. } else {
  332. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  333. if err != nil {
  334. fmt.Println("新增失败:", err.Error())
  335. return err
  336. }
  337. if resp.Status == 0 && resp.Result == "created" {
  338. fmt.Println("新增成功")
  339. err = nil
  340. } else {
  341. fmt.Println("AddData", resp.Status, resp.Result)
  342. }
  343. }
  344. return
  345. }
  346. // 新增和修改数据
  347. func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4) (err error) {
  348. defer func() {
  349. if err != nil {
  350. fmt.Println("EsAddOrEditData Err:", err.Error())
  351. }
  352. }()
  353. client := utils.Client
  354. //if err != nil {
  355. // return
  356. //}
  357. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  358. if err != nil && !strings.Contains(err.Error(), "404") {
  359. fmt.Println("Get Err" + err.Error())
  360. return
  361. }
  362. if searchById != nil && searchById.Found {
  363. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  364. "BodyText": item.BodyText,
  365. "Title": item.Title,
  366. "PublishDate": item.PublishDate,
  367. "IsSummary": item.IsSummary,
  368. "IsReport": item.IsReport,
  369. }).Do(context.Background())
  370. if err != nil {
  371. return err
  372. }
  373. fmt.Println(resp.Status, resp.Result)
  374. if resp.Status == 0 {
  375. fmt.Println("修改成功")
  376. } else {
  377. fmt.Println("EditData", resp.Status, resp.Result)
  378. }
  379. } else {
  380. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  381. if err != nil {
  382. fmt.Println("新增失败:", err.Error())
  383. return err
  384. }
  385. if resp.Status == 0 && resp.Result == "created" {
  386. fmt.Println("新增成功")
  387. err = nil
  388. } else {
  389. fmt.Println("AddData", resp.Status, resp.Result)
  390. }
  391. }
  392. return
  393. }
  394. // 删除数据
  395. func EsDeleteData(indexName, docId string) (err error) {
  396. client := utils.Client
  397. //if err != nil {
  398. // return
  399. //}
  400. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  401. if err != nil {
  402. return
  403. }
  404. if resp.Status == 0 {
  405. fmt.Println("删除成功")
  406. } else {
  407. fmt.Println("AddData", resp.Status, resp.Result)
  408. }
  409. return
  410. }
  411. func GetWeight(length int) []int {
  412. steep := 10
  413. intArr := make([]int, 0)
  414. for i := 1; i <= length; i++ {
  415. if i == 1 {
  416. intArr = append(intArr, 1)
  417. } else {
  418. min := GetArrSum(intArr)
  419. maxVal := utils.GetRandInt(min, min+steep)
  420. intArr = append(intArr, maxVal)
  421. }
  422. }
  423. //数组排序
  424. sort.Slice(intArr, func(i, j int) bool {
  425. return intArr[i] > intArr[j]
  426. })
  427. return intArr
  428. }
  429. func GetArrSum(intArr []int) (sum int) {
  430. for _, val := range intArr {
  431. //累计求和
  432. sum += val
  433. }
  434. return
  435. }
  436. func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
  437. client := utils.Client
  438. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  439. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  440. //artidArr := make([]elastic.Query, 0)
  441. //matchArr := make([]elastic.Query, 0)
  442. n := 0
  443. keyWordLen := len(keyWordArr)
  444. if keyWordLen <= 0 {
  445. keyWordArr = append(keyWordArr, keyWord)
  446. keyWordLen = len(keyWordArr)
  447. }
  448. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  449. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  450. //keyWordWeight := GetWeight(keyWordLen)
  451. for _, v := range keyWordArr {
  452. if v != "" {
  453. matchArr := make([]elastic.Query, 0)
  454. boolquery := elastic.NewBoolQuery()
  455. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  456. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  457. bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
  458. //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  459. multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart").Boost(100)
  460. bodyFunctionQuery.Query(multiMatch)
  461. matchArr = append(matchArr, bodyFunctionQuery)
  462. multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart").Boost(1)
  463. bodyFunctionQuery2.Query(multiMatch)
  464. matchArr = append(matchArr, bodyFunctionQuery2)
  465. //multiMatch = elastic.NewMultiMatchQuery(1, "IsSummary")
  466. bodyFunctionQuery3.Query(multiMatch)
  467. matchArr = append(matchArr, bodyFunctionQuery3)
  468. boolquery.Should(matchArr...)
  469. //multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart")
  470. //bodyFunctionQuery.Query(multiMatch)
  471. //matchArr = append(matchArr, bodyFunctionQuery)
  472. //boolquery.Should(matchArr...)
  473. highlight := elastic.NewHighlight()
  474. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  475. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  476. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
  477. if orderColumn == "Matching" {
  478. request = client.Search(indexName).Highlight(highlight).From(0).Size(pageSize).Query(boolquery)
  479. }
  480. searchByMatch, err := request.Do(context.Background())
  481. if err != nil {
  482. return nil, 0, err
  483. }
  484. if searchByMatch != nil {
  485. if searchByMatch.Hits != nil {
  486. for _, v := range searchByMatch.Hits.Hits {
  487. var isAppend bool
  488. articleJson, err := v.Source.MarshalJSON()
  489. if err != nil {
  490. return nil, 0, err
  491. }
  492. article := new(models.CygxArticleEs)
  493. err = json.Unmarshal(articleJson, &article)
  494. if err != nil {
  495. return nil, 0, err
  496. }
  497. searchItem := new(models.SearchItem)
  498. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  499. if len(v.Highlight["BodyText"]) > 0 {
  500. searchItem.Body = v.Highlight["BodyText"]
  501. } else {
  502. bodyRune := []rune(article.BodyText)
  503. bodyRuneLen := len(bodyRune)
  504. if bodyRuneLen > 100 {
  505. bodyRuneLen = 100
  506. }
  507. body := string(bodyRune[:bodyRuneLen])
  508. searchItem.Body = []string{body}
  509. }
  510. var title string
  511. if len(v.Highlight["Title"]) > 0 {
  512. title = v.Highlight["Title"][0]
  513. } else {
  514. title = article.Title
  515. }
  516. searchItem.Title = title
  517. searchItem.PublishDate = article.PublishDate
  518. searchItem.ExpertBackground = article.ExpertBackground
  519. searchItem.CategoryId = article.CategoryId
  520. for _, v_result := range result {
  521. if v_result.ArticleId == searchItem.ArticleId {
  522. isAppend = true
  523. }
  524. }
  525. if !isAppend {
  526. result = append(result, searchItem)
  527. }
  528. }
  529. }
  530. //total += searchByMatch.Hits.TotalHits.Value
  531. }
  532. }
  533. n++
  534. }
  535. total = int64(len(result))
  536. return
  537. }
  538. func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  539. client := utils.Client
  540. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  541. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  542. boolquery := elastic.NewBoolQuery()
  543. matchArr := make([]elastic.Query, 0)
  544. //matchArr2 := make([]elastic.Query, 0)
  545. n := 0
  546. keyWordLen := len(keyWordArr)
  547. if keyWordLen <= 0 {
  548. keyWordArr = append(keyWordArr, keyWord)
  549. keyWordLen = len(keyWordArr)
  550. }
  551. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  552. for _, v := range keyWordArr {
  553. if v != "" {
  554. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  555. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  556. bodyFunctionQuery.Query(multiMatch)
  557. matchArr = append(matchArr, bodyFunctionQuery)
  558. }
  559. n++
  560. }
  561. boolquery.Should(matchArr...)
  562. highlight := elastic.NewHighlight()
  563. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  564. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  565. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).Size(pageSize).Query(boolquery)
  566. searchByMatch, err := request.Do(context.Background())
  567. if searchByMatch != nil {
  568. matchResult, _ := json.Marshal(searchByMatch)
  569. utils.FileLog.Info("%s", string(matchResult))
  570. fmt.Println(len(searchByMatch.Hits.Hits))
  571. if searchByMatch.Hits != nil {
  572. for _, v := range searchByMatch.Hits.Hits {
  573. articleJson, err := v.Source.MarshalJSON()
  574. utils.FileLog.Info("%s", string(articleJson))
  575. if err != nil {
  576. return nil, 0, err
  577. }
  578. article := new(models.CygxArticleEs)
  579. err = json.Unmarshal(articleJson, &article)
  580. if err != nil {
  581. return nil, 0, err
  582. }
  583. searchItem := new(models.SearchItem)
  584. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  585. if len(v.Highlight["BodyText"]) > 0 {
  586. searchItem.Body = v.Highlight["BodyText"]
  587. } else {
  588. bodyRune := []rune(article.BodyText)
  589. bodyRuneLen := len(bodyRune)
  590. if bodyRuneLen > 100 {
  591. bodyRuneLen = 100
  592. }
  593. body := string(bodyRune[:bodyRuneLen])
  594. searchItem.Body = []string{body}
  595. }
  596. var title string
  597. if len(v.Highlight["Title"]) > 0 {
  598. title = v.Highlight["Title"][0]
  599. } else {
  600. title = article.Title
  601. }
  602. searchItem.Title = title
  603. searchItem.PublishDate = article.PublishDate
  604. searchItem.ExpertBackground = article.ExpertBackground
  605. searchItem.CategoryId = article.CategoryId
  606. result = append(result, searchItem)
  607. }
  608. }
  609. total = searchByMatch.Hits.TotalHits.Value
  610. }
  611. return
  612. }
  613. func EsSearchReport(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  614. client := utils.Client
  615. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  616. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  617. n := 0
  618. keyWordLen := len(keyWordArr)
  619. if keyWordLen <= 0 {
  620. keyWordArr = append(keyWordArr, keyWord)
  621. keyWordLen = len(keyWordArr)
  622. }
  623. for _, v := range keyWordArr {
  624. fmt.Println(v)
  625. }
  626. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  627. for _, v := range keyWordArr {
  628. if v != "" {
  629. matchArr := make([]elastic.Query, 0)
  630. boolquery := elastic.NewBoolQuery()
  631. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  632. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  633. multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart")
  634. bodyFunctionQuery.Query(multiMatch)
  635. matchArr = append(matchArr, bodyFunctionQuery)
  636. multiMatch = elastic.NewMultiMatchQuery(1, "IsReport")
  637. bodyFunctionQuery2.Query(multiMatch)
  638. matchArr = append(matchArr, bodyFunctionQuery2)
  639. boolquery.Must(matchArr...)
  640. highlight := elastic.NewHighlight()
  641. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  642. highlight = highlight.Fields(elastic.NewHighlighterField("Title"))
  643. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
  644. searchByMatch, err := request.Do(context.Background())
  645. if err != nil {
  646. return nil, 0, err
  647. }
  648. if searchByMatch != nil {
  649. if searchByMatch.Hits != nil {
  650. for _, v := range searchByMatch.Hits.Hits {
  651. var isAppend bool
  652. articleJson, err := v.Source.MarshalJSON()
  653. if err != nil {
  654. return nil, 0, err
  655. }
  656. article := new(models.CygxArticleEs)
  657. err = json.Unmarshal(articleJson, &article)
  658. if err != nil {
  659. return nil, 0, err
  660. }
  661. searchItem := new(models.SearchItem)
  662. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  663. var title string
  664. if len(v.Highlight["Title"]) > 0 {
  665. title = v.Highlight["Title"][0]
  666. } else {
  667. title = article.Title
  668. }
  669. searchItem.Title = title
  670. searchItem.PublishDate = article.PublishDate
  671. for _, v_result := range result {
  672. if v_result.ArticleId == searchItem.ArticleId {
  673. isAppend = true
  674. }
  675. }
  676. if !isAppend {
  677. result = append(result, searchItem)
  678. }
  679. }
  680. }
  681. }
  682. }
  683. n++
  684. }
  685. total = int64(len(result))
  686. return
  687. }
  688. // 分页
  689. func EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  690. client := utils.Client
  691. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  692. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  693. boolquery := elastic.NewBoolQuery()
  694. matchArr := make([]elastic.Query, 0)
  695. n := 0
  696. keyWordLen := len(keyWordArr)
  697. if keyWordLen <= 0 {
  698. keyWordArr = append(keyWordArr, keyWord)
  699. keyWordLen = len(keyWordArr)
  700. }
  701. for _, v := range keyWordArr {
  702. if v != "" {
  703. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  704. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  705. bodyFunctionQuery.Query(multiMatch)
  706. matchArr = append(matchArr, bodyFunctionQuery)
  707. }
  708. n++
  709. }
  710. boolquery.Should(matchArr...)
  711. highlight := elastic.NewHighlight()
  712. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  713. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  714. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
  715. searchByMatch, err := request.Do(context.Background())
  716. if searchByMatch != nil {
  717. if searchByMatch.Hits != nil {
  718. for _, v := range searchByMatch.Hits.Hits {
  719. articleJson, err := v.Source.MarshalJSON()
  720. if err != nil {
  721. return nil, 0, err
  722. }
  723. article := new(models.CygxArticleEs)
  724. err = json.Unmarshal(articleJson, &article)
  725. if err != nil {
  726. return nil, 0, err
  727. }
  728. searchItem := new(models.SearchItem)
  729. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  730. if len(v.Highlight["BodyText"]) > 0 {
  731. searchItem.Body = v.Highlight["BodyText"]
  732. } else {
  733. bodyRune := []rune(article.BodyText)
  734. bodyRuneLen := len(bodyRune)
  735. if bodyRuneLen > 100 {
  736. bodyRuneLen = 100
  737. }
  738. body := string(bodyRune[:bodyRuneLen])
  739. searchItem.Body = []string{body}
  740. }
  741. var title string
  742. if len(v.Highlight["Title"]) > 0 {
  743. title = v.Highlight["Title"][0]
  744. } else {
  745. title = article.Title
  746. }
  747. searchItem.Title = title
  748. searchItem.PublishDate = article.PublishDate
  749. searchItem.ExpertBackground = article.ExpertBackground
  750. searchItem.CategoryId = article.CategoryId
  751. result = append(result, searchItem)
  752. }
  753. }
  754. total = searchByMatch.Hits.TotalHits.Value
  755. }
  756. return
  757. }
  758. func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
  759. client := utils.Client
  760. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  761. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  762. keyWordLen := len(keyWordArr)
  763. if keyWordLen <= 0 {
  764. keyWordArr = append(keyWordArr, keyWord)
  765. keyWordLen = len(keyWordArr)
  766. }
  767. var keyWords string
  768. for _, v := range keyWordArr {
  769. keyWords += v + " "
  770. }
  771. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  772. //keyWordWeight := GetWeight(keyWordLen)
  773. matchArr := make([]elastic.Query, 0)
  774. boolquery := elastic.NewBoolQuery()
  775. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  776. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  777. bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
  778. multiMatch := elastic.NewMultiMatchQuery(keyWords, "Title").Analyzer("ik_smart").Boost(100)
  779. bodyFunctionQuery.Query(multiMatch)
  780. matchArr = append(matchArr, bodyFunctionQuery)
  781. multiMatch = elastic.NewMultiMatchQuery(keyWords, "BodyText").Analyzer("ik_smart").Boost(1)
  782. bodyFunctionQuery2.Query(multiMatch)
  783. matchArr = append(matchArr, bodyFunctionQuery2)
  784. bodyFunctionQuery3.Query(multiMatch)
  785. matchArr = append(matchArr, bodyFunctionQuery3)
  786. boolquery.Should(matchArr...)
  787. highlight := elastic.NewHighlight()
  788. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  789. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  790. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
  791. if orderColumn == "Matching" {
  792. request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  793. }
  794. searchByMatch, err := request.Do(context.Background())
  795. if err != nil {
  796. return nil, 0, err
  797. }
  798. if searchByMatch != nil {
  799. if searchByMatch.Hits != nil {
  800. for _, v := range searchByMatch.Hits.Hits {
  801. var isAppend bool
  802. articleJson, err := v.Source.MarshalJSON()
  803. if err != nil {
  804. return nil, 0, err
  805. }
  806. article := new(models.CygxArticleEs)
  807. err = json.Unmarshal(articleJson, &article)
  808. if err != nil {
  809. return nil, 0, err
  810. }
  811. searchItem := new(models.SearchItem)
  812. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  813. if len(v.Highlight["BodyText"]) > 0 {
  814. searchItem.Body = v.Highlight["BodyText"]
  815. } else {
  816. bodyRune := []rune(article.BodyText)
  817. bodyRuneLen := len(bodyRune)
  818. if bodyRuneLen > 100 {
  819. bodyRuneLen = 100
  820. }
  821. body := string(bodyRune[:bodyRuneLen])
  822. searchItem.Body = []string{body}
  823. }
  824. var title string
  825. if len(v.Highlight["Title"]) > 0 {
  826. title = v.Highlight["Title"][0]
  827. } else {
  828. title = article.Title
  829. }
  830. searchItem.Title = title
  831. searchItem.PublishDate = article.PublishDate
  832. searchItem.ExpertBackground = article.ExpertBackground
  833. searchItem.CategoryId = article.CategoryId
  834. for _, v_result := range result {
  835. if v_result.ArticleId == searchItem.ArticleId {
  836. isAppend = true
  837. }
  838. }
  839. if !isAppend {
  840. result = append(result, searchItem)
  841. }
  842. }
  843. }
  844. }
  845. total += searchByMatch.Hits.TotalHits.Value
  846. return
  847. }
  848. //func init23423() {
  849. // EsArticleSearch("立高食品", 0, 10, "34")
  850. //}
  851. func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string, ikType int) (result []*models.SearchItem, total int64, err error) {
  852. indexName := utils.IndexName
  853. client := utils.Client
  854. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  855. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  856. keyWordLen := len(keyWordArr)
  857. if keyWordLen <= 0 {
  858. keyWordArr = append(keyWordArr, keyWord)
  859. keyWordLen = len(keyWordArr)
  860. }
  861. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  862. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  863. mustMap := make([]interface{}, 0)
  864. shouldMap := make(map[string]interface{}, 0)
  865. shouldMapquery := make([]interface{}, 0)
  866. mustNotMap := make([]interface{}, 0)
  867. shouldNotMap := make(map[string]interface{}, 0)
  868. shouldNotMapquery := make([]interface{}, 0)
  869. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  870. //keyWordWeight := GetWeight(keyWordLen)
  871. var boost int
  872. //lenkeyWordArr := len(keyWordArr)
  873. for k, v := range keyWordArr {
  874. if k == 0 {
  875. boost = 2 * 1000
  876. } else {
  877. boost = 1
  878. }
  879. //如果是 2:查询除了查询键入词之外的联想词
  880. if k == 0 && ikType == 2 {
  881. if v != "" {
  882. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  883. "function_score": map[string]interface{}{
  884. "query": map[string]interface{}{
  885. "multi_match": map[string]interface{}{
  886. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  887. "boost": boost, //给查询的值赋予权重
  888. "fields": []interface{}{"Title"},
  889. "query": v,
  890. },
  891. },
  892. },
  893. })
  894. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  895. "function_score": map[string]interface{}{
  896. "query": map[string]interface{}{
  897. "multi_match": map[string]interface{}{
  898. "boost": boost, //给查询的值赋予权重
  899. "fields": []interface{}{"Abstract"},
  900. "query": v,
  901. },
  902. },
  903. },
  904. })
  905. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  906. "function_score": map[string]interface{}{
  907. "query": map[string]interface{}{
  908. "multi_match": map[string]interface{}{
  909. "boost": boost, //给查询的值赋予权重
  910. "fields": []interface{}{"Annotation"},
  911. "query": v,
  912. },
  913. },
  914. },
  915. })
  916. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  917. "function_score": map[string]interface{}{
  918. "query": map[string]interface{}{
  919. "multi_match": map[string]interface{}{
  920. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  921. "boost": boost, //给查询的值赋予权重
  922. "fields": []interface{}{"BodyText"},
  923. "query": v,
  924. },
  925. },
  926. },
  927. })
  928. }
  929. continue
  930. }
  931. //如果是 1:查询键入词
  932. if k > 0 && ikType == 1 {
  933. continue
  934. }
  935. if v != "" {
  936. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  937. "function_score": map[string]interface{}{
  938. "query": map[string]interface{}{
  939. "multi_match": map[string]interface{}{
  940. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  941. "boost": boost, //给查询的值赋予权重
  942. "fields": []interface{}{"Title"},
  943. "query": v,
  944. },
  945. },
  946. },
  947. })
  948. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  949. "function_score": map[string]interface{}{
  950. "query": map[string]interface{}{
  951. "multi_match": map[string]interface{}{
  952. "boost": boost, //给查询的值赋予权重
  953. "fields": []interface{}{"Abstract"},
  954. "query": v,
  955. },
  956. },
  957. },
  958. })
  959. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  960. "function_score": map[string]interface{}{
  961. "query": map[string]interface{}{
  962. "multi_match": map[string]interface{}{
  963. "boost": boost, //给查询的值赋予权重
  964. "fields": []interface{}{"Annotation"},
  965. "query": v,
  966. },
  967. },
  968. },
  969. })
  970. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  971. "function_score": map[string]interface{}{
  972. "query": map[string]interface{}{
  973. "multi_match": map[string]interface{}{
  974. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  975. "boost": boost, //给查询的值赋予权重
  976. "fields": []interface{}{"BodyText"},
  977. "query": v,
  978. },
  979. },
  980. },
  981. })
  982. }
  983. }
  984. shouldMap = map[string]interface{}{
  985. "should": shouldMapquery,
  986. }
  987. shouldNotMap = map[string]interface{}{
  988. "should": shouldNotMapquery,
  989. }
  990. //排序
  991. sortMap := make([]interface{}, 0)
  992. //时间
  993. sortMap = append(sortMap, map[string]interface{}{
  994. "PublishDate": map[string]interface{}{
  995. "order": "desc",
  996. },
  997. })
  998. //高亮
  999. highlightMap := make(map[string]interface{}, 0)
  1000. highlightMap = map[string]interface{}{
  1001. "fields": map[string]interface{}{
  1002. "BodyText": map[string]interface{}{},
  1003. "Title": map[string]interface{}{},
  1004. "Abstract": map[string]interface{}{},
  1005. "Annotation": map[string]interface{}{},
  1006. },
  1007. //样式 红色
  1008. "post_tags": []interface{}{"</font>"},
  1009. "pre_tags": []interface{}{"<font color='red'>"},
  1010. }
  1011. mustMap = append(mustMap, map[string]interface{}{
  1012. "bool": shouldMap,
  1013. })
  1014. mustNotMap = append(mustNotMap, map[string]interface{}{
  1015. "bool": shouldNotMap,
  1016. })
  1017. queryMap := map[string]interface{}{
  1018. "query": map[string]interface{}{
  1019. "bool": map[string]interface{}{
  1020. "must": mustMap,
  1021. },
  1022. },
  1023. }
  1024. //把第一次键入词的筛选条件过滤掉
  1025. if ikType == 2 {
  1026. queryMap = map[string]interface{}{
  1027. "query": map[string]interface{}{
  1028. "bool": map[string]interface{}{
  1029. "must": mustMap,
  1030. "must_not": mustNotMap,
  1031. },
  1032. },
  1033. }
  1034. }
  1035. if orderColumn == "Matching" {
  1036. queryMap["sort"] = sortMap
  1037. }
  1038. queryMap["from"] = startSize
  1039. queryMap["size"] = pageSize
  1040. queryMap["highlight"] = highlightMap
  1041. jsonBytes, _ := json.Marshal(queryMap)
  1042. fmt.Println(string(jsonBytes))
  1043. //utils.FileLog.Info(string(jsonBytes))
  1044. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1045. searchByMatch, err := request.Do(context.Background())
  1046. if searchByMatch != nil {
  1047. if searchByMatch.Hits != nil {
  1048. for _, v := range searchByMatch.Hits.Hits {
  1049. var isAppend bool
  1050. articleJson, err := v.Source.MarshalJSON()
  1051. if err != nil {
  1052. return nil, 0, err
  1053. }
  1054. article := new(models.CygxArticleEs)
  1055. err = json.Unmarshal(articleJson, &article)
  1056. if err != nil {
  1057. return nil, 0, err
  1058. }
  1059. searchItem := new(models.SearchItem)
  1060. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1061. if len(v.Highlight["BodyText"]) > 0 {
  1062. searchItem.Body = v.Highlight["BodyText"]
  1063. } else {
  1064. bodyRune := []rune(article.BodyText)
  1065. bodyRuneLen := len(bodyRune)
  1066. if bodyRuneLen > 100 {
  1067. bodyRuneLen = 100
  1068. }
  1069. body := string(bodyRune[:bodyRuneLen])
  1070. searchItem.Body = []string{body}
  1071. }
  1072. var title string
  1073. if len(v.Highlight["Title"]) > 0 {
  1074. title = v.Highlight["Title"][0]
  1075. } else {
  1076. title = article.Title
  1077. }
  1078. searchItem.Title = title
  1079. searchItem.PublishDate = article.PublishDate
  1080. searchItem.ExpertBackground = article.ExpertBackground
  1081. searchItem.CategoryId = article.CategoryId
  1082. for _, v_result := range result {
  1083. if v_result.ArticleId == searchItem.ArticleId {
  1084. isAppend = true
  1085. }
  1086. }
  1087. if !isAppend {
  1088. result = append(result, searchItem)
  1089. }
  1090. }
  1091. }
  1092. total = searchByMatch.Hits.TotalHits.Value
  1093. }
  1094. return
  1095. }