elastic.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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. }).Do(context.Background())
  320. if err != nil {
  321. return err
  322. }
  323. if resp.Status == 0 {
  324. fmt.Println("修改成功")
  325. } else {
  326. fmt.Println("EditData", resp.Status, resp.Result)
  327. }
  328. client.CloseIndex(indexName)
  329. } else {
  330. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  331. if err != nil {
  332. fmt.Println("新增失败:", err.Error())
  333. return err
  334. }
  335. if resp.Status == 0 && resp.Result == "created" {
  336. fmt.Println("新增成功")
  337. err = nil
  338. } else {
  339. fmt.Println("AddData", resp.Status, resp.Result)
  340. }
  341. }
  342. return
  343. }
  344. //新增和修改数据
  345. func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4) (err error) {
  346. defer func() {
  347. if err != nil {
  348. fmt.Println("EsAddOrEditData Err:", err.Error())
  349. }
  350. }()
  351. client := utils.Client
  352. //if err != nil {
  353. // return
  354. //}
  355. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  356. if err != nil && !strings.Contains(err.Error(), "404") {
  357. fmt.Println("Get Err" + err.Error())
  358. return
  359. }
  360. if searchById != nil && searchById.Found {
  361. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  362. "BodyText": item.BodyText,
  363. "Title": item.Title,
  364. "PublishDate": item.PublishDate,
  365. "IsSummary": item.IsSummary,
  366. "IsReport": item.IsReport,
  367. }).Do(context.Background())
  368. if err != nil {
  369. return err
  370. }
  371. fmt.Println(resp.Status, resp.Result)
  372. if resp.Status == 0 {
  373. fmt.Println("修改成功")
  374. } else {
  375. fmt.Println("EditData", resp.Status, resp.Result)
  376. }
  377. } else {
  378. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  379. if err != nil {
  380. fmt.Println("新增失败:", err.Error())
  381. return err
  382. }
  383. if resp.Status == 0 && resp.Result == "created" {
  384. fmt.Println("新增成功")
  385. err = nil
  386. } else {
  387. fmt.Println("AddData", resp.Status, resp.Result)
  388. }
  389. }
  390. return
  391. }
  392. //删除数据
  393. func EsDeleteData(indexName, docId string) (err error) {
  394. client := utils.Client
  395. //if err != nil {
  396. // return
  397. //}
  398. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  399. if err != nil {
  400. return
  401. }
  402. if resp.Status == 0 {
  403. fmt.Println("删除成功")
  404. } else {
  405. fmt.Println("AddData", resp.Status, resp.Result)
  406. }
  407. return
  408. }
  409. func MappingModify(indexName, mappingJson string) {
  410. client := utils.Client
  411. //if err != nil {
  412. // return
  413. //}
  414. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  415. fmt.Println(err)
  416. fmt.Println(result)
  417. return
  418. }
  419. func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
  420. client := utils.Client
  421. pageSize := 20
  422. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  423. fmt.Println(keyWordArr)
  424. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  425. fmt.Println("-------------------------------")
  426. fmt.Println(keyWordArr)
  427. searchMap := make(map[int]int)
  428. boolquery := elastic.NewBoolQuery()
  429. keyLen := len(keyWordArr)
  430. n := 5.0 * float64(keyLen)
  431. matchArr := make([]elastic.Query, 0)
  432. //
  433. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  434. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  435. //
  436. //matchArr = append(matchArr, matchq1)
  437. //matchArr = append(matchArr, matchq2)
  438. for _, v := range keyWordArr {
  439. if v != "" {
  440. matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
  441. matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
  442. matchArr = append(matchArr, matchq1)
  443. matchArr = append(matchArr, matchq2)
  444. }
  445. n = n - 5
  446. }
  447. boolquery.Should(matchArr...)
  448. highlight := elastic.NewHighlight()
  449. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  450. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  451. request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
  452. searchByMatch, err := request.Do(context.Background())
  453. if searchByMatch.Hits != nil {
  454. for _, v := range searchByMatch.Hits.Hits {
  455. articleJson, err := v.Source.MarshalJSON()
  456. if err != nil {
  457. return nil, err
  458. }
  459. article := new(models.CygxArticle)
  460. err = json.Unmarshal(articleJson, &article)
  461. if err != nil {
  462. return nil, err
  463. }
  464. if _, ok := searchMap[article.ArticleId]; !ok {
  465. searchItem := new(models.SearchItem)
  466. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  467. if len(v.Highlight["BodyText"]) > 0 {
  468. searchItem.Body = v.Highlight["BodyText"]
  469. } else {
  470. bodyRune := []rune(article.BodyText)
  471. bodyRuneLen := len(bodyRune)
  472. if bodyRuneLen > 100 {
  473. bodyRuneLen = 100
  474. }
  475. body := string(bodyRune[:bodyRuneLen])
  476. fmt.Println(body)
  477. searchItem.Body = []string{body}
  478. }
  479. var title string
  480. if len(v.Highlight["Title"]) > 0 {
  481. title = v.Highlight["Title"][0]
  482. } else {
  483. title = article.Title
  484. }
  485. searchItem.Title = title
  486. searchItem.PublishDate = article.PublishDate
  487. result = append(result, searchItem)
  488. searchMap[article.ArticleId] = article.ArticleId
  489. }
  490. }
  491. }
  492. return
  493. }
  494. func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
  495. client := utils.Client
  496. pageSize := 20
  497. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  498. fmt.Println(keyWordArr)
  499. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  500. fmt.Println("-------------------------------")
  501. fmt.Println(keyWordArr)
  502. searchMap := make(map[int]int)
  503. boolquery := elastic.NewBoolQuery()
  504. //keyLen := len(keyWordArr)
  505. //n := float64(keyLen)
  506. matchArr := make([]elastic.Query, 0)
  507. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  508. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  509. matchq1 := elastic.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart")
  510. matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord)
  511. matchArr = append(matchArr, matchq1)
  512. matchArr = append(matchArr, matchq2)
  513. //matchArr = append(matchArr, matchq2)
  514. //for _, v := range keyWordArr {
  515. // if v != "" {
  516. // matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
  517. // matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
  518. // matchArr = append(matchArr, matchq1)
  519. // matchArr = append(matchArr, matchq2)
  520. // }
  521. // n--
  522. //}
  523. //boolquery.Should(matchArr...)
  524. boolquery.Should(matchArr...)
  525. highlight := elastic.NewHighlight()
  526. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  527. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  528. request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
  529. searchByMatch, err := request.Do(context.Background())
  530. fmt.Println("err:", err, searchByMatch)
  531. return
  532. if searchByMatch.Hits != nil {
  533. for _, v := range searchByMatch.Hits.Hits {
  534. articleJson, err := v.Source.MarshalJSON()
  535. if err != nil {
  536. return nil, err
  537. }
  538. article := new(models.CygxArticle)
  539. err = json.Unmarshal(articleJson, &article)
  540. if err != nil {
  541. return nil, err
  542. }
  543. if _, ok := searchMap[article.ArticleId]; !ok {
  544. searchItem := new(models.SearchItem)
  545. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  546. searchItem.Body = v.Highlight["BodyText"]
  547. var title string
  548. if len(v.Highlight["Title"]) > 0 {
  549. title = v.Highlight["Title"][0]
  550. } else {
  551. title = article.Title
  552. }
  553. searchItem.Title = title
  554. searchItem.PublishDate = article.PublishDate
  555. result = append(result, searchItem)
  556. searchMap[article.ArticleId] = article.ArticleId
  557. }
  558. }
  559. }
  560. return
  561. }
  562. func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
  563. client := utils.Client
  564. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  565. fmt.Println(keyWordArr)
  566. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  567. fmt.Println("-------------------------------")
  568. fmt.Println(keyWordArr)
  569. searchMap := make(map[int]int)
  570. boolquery := elastic.NewBoolQuery()
  571. matchArr := make([]elastic.Query, 0)
  572. //
  573. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  574. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  575. //
  576. //matchArr = append(matchArr, matchq1)
  577. //matchArr = append(matchArr, matchq2)
  578. n := 0
  579. keyWordLen := len(keyWordArr)
  580. if keyWordLen <= 0 {
  581. keyWordArr = append(keyWordArr, keyWord)
  582. keyWordLen = len(keyWordArr)
  583. }
  584. keyWordWeight := GetWeight(keyWordLen)
  585. fmt.Println(keyWordArr)
  586. fmt.Println(keyWordWeight)
  587. for k, v := range keyWordArr {
  588. if v != "" {
  589. weight := float64(keyWordWeight[k])
  590. titleMatchq := elastic.NewMatchQuery("Title", v).Analyzer("ik_smart")
  591. bodyMatchq := elastic.NewMatchQuery("BodyText", v).Analyzer("ik_smart")
  592. titleFunctionQuery := elastic.NewFunctionScoreQuery()
  593. titleFunctionQuery.Query(titleMatchq)
  594. titleFunctions := elastic.NewWeightFactorFunction(weight)
  595. titleFunctionQuery.AddScoreFunc(titleFunctions)
  596. titleFunctionQuery.BoostMode("replace")
  597. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  598. bodyFunctionQuery.Query(bodyMatchq)
  599. bodyFunctions := elastic.NewWeightFactorFunction(weight)
  600. bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  601. bodyFunctionQuery.BoostMode("replace")
  602. matchArr = append(matchArr, titleFunctionQuery)
  603. matchArr = append(matchArr, bodyFunctionQuery)
  604. }
  605. n++
  606. }
  607. boolquery.Should(matchArr...)
  608. highlight := elastic.NewHighlight()
  609. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  610. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  611. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  612. searchByMatch, err := request.Do(context.Background())
  613. //searchJson, err := json.Marshal(searchByMatch)
  614. if searchByMatch != nil {
  615. if searchByMatch.Hits != nil {
  616. for _, v := range searchByMatch.Hits.Hits {
  617. articleJson, err := v.Source.MarshalJSON()
  618. if err != nil {
  619. return nil, 0, err
  620. }
  621. article := new(models.CygxArticle)
  622. err = json.Unmarshal(articleJson, &article)
  623. if err != nil {
  624. return nil, 0, err
  625. }
  626. if _, ok := searchMap[article.ArticleId]; !ok {
  627. searchItem := new(models.SearchItem)
  628. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  629. if len(v.Highlight["BodyText"]) > 0 {
  630. searchItem.Body = v.Highlight["BodyText"]
  631. } else {
  632. bodyRune := []rune(article.BodyText)
  633. bodyRuneLen := len(bodyRune)
  634. if bodyRuneLen > 100 {
  635. bodyRuneLen = 100
  636. }
  637. body := string(bodyRune[:bodyRuneLen])
  638. fmt.Println(body)
  639. searchItem.Body = []string{body}
  640. }
  641. var title string
  642. if len(v.Highlight["Title"]) > 0 {
  643. title = v.Highlight["Title"][0]
  644. } else {
  645. title = article.Title
  646. }
  647. searchItem.Title = title
  648. searchItem.PublishDate = article.PublishDate
  649. result = append(result, searchItem)
  650. searchMap[article.ArticleId] = article.ArticleId
  651. }
  652. }
  653. }
  654. }
  655. total = searchByMatch.Hits.TotalHits.Value
  656. return
  657. }
  658. func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  659. client := utils.Client
  660. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  661. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  662. //artidArr := make([]elastic.Query, 0)
  663. //matchArr := make([]elastic.Query, 0)
  664. n := 0
  665. keyWordLen := len(keyWordArr)
  666. if keyWordLen <= 0 {
  667. keyWordArr = append(keyWordArr, keyWord)
  668. keyWordLen = len(keyWordArr)
  669. }
  670. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  671. //keyWordWeight := GetWeight(keyWordLen)
  672. for _, v := range keyWordArr {
  673. if v != "" {
  674. matchArr := make([]elastic.Query, 0)
  675. boolquery := elastic.NewBoolQuery()
  676. //weight := float64(keyWordWeight[k])
  677. //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  678. //bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  679. //bodyFunctionQuery.Query(multiMatch)
  680. //bodyFunctions := elastic.NewWeightFactorFunction(weight)
  681. //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  682. //bodyFunctionQuery.BoostMode("replace")
  683. //matchArr = append(matchArr, bodyFunctionQuery)
  684. //weight := float64(keyWordWeight[k])
  685. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  686. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  687. bodyFunctionQuery.Query(multiMatch)
  688. //bodyFunctions := elastic.NewWeightFactorFunction(weight)
  689. //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  690. //bodyFunctionQuery.BoostMode("replace")
  691. matchArr = append(matchArr, bodyFunctionQuery)
  692. boolquery.Should(matchArr...)
  693. highlight := elastic.NewHighlight()
  694. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  695. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  696. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  697. searchByMatch, err := request.Do(context.Background())
  698. if err != nil {
  699. return nil, 0, err
  700. }
  701. if searchByMatch != nil {
  702. if searchByMatch.Hits != nil {
  703. for _, v := range searchByMatch.Hits.Hits {
  704. var isAppend bool
  705. articleJson, err := v.Source.MarshalJSON()
  706. if err != nil {
  707. return nil, 0, err
  708. }
  709. article := new(models.CygxArticle)
  710. err = json.Unmarshal(articleJson, &article)
  711. if err != nil {
  712. return nil, 0, err
  713. }
  714. searchItem := new(models.SearchItem)
  715. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  716. if len(v.Highlight["BodyText"]) > 0 {
  717. searchItem.Body = v.Highlight["BodyText"]
  718. } else {
  719. bodyRune := []rune(article.BodyText)
  720. bodyRuneLen := len(bodyRune)
  721. if bodyRuneLen > 100 {
  722. bodyRuneLen = 100
  723. }
  724. body := string(bodyRune[:bodyRuneLen])
  725. searchItem.Body = []string{body}
  726. }
  727. var title string
  728. if len(v.Highlight["Title"]) > 0 {
  729. title = v.Highlight["Title"][0]
  730. } else {
  731. title = article.Title
  732. }
  733. searchItem.Title = title
  734. searchItem.PublishDate = article.PublishDate
  735. for _, v_result := range result {
  736. if v_result.ArticleId == searchItem.ArticleId {
  737. isAppend = true
  738. }
  739. }
  740. if !isAppend {
  741. result = append(result, searchItem)
  742. }
  743. }
  744. }
  745. //total += searchByMatch.Hits.TotalHits.Value
  746. }
  747. }
  748. n++
  749. }
  750. total = int64(len(result))
  751. //fmt.Println(result)
  752. //boolquery.Should(matchArr...)
  753. //highlight := elastic.NewHighlight()
  754. //highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  755. //highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  756. //request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  757. //searchByMatch, err := request.Do(context.Background())
  758. //if searchByMatch != nil {
  759. // if searchByMatch.Hits != nil {
  760. // for _, v := range searchByMatch.Hits.Hits {
  761. // articleJson, err := v.Source.MarshalJSON()
  762. // if err != nil {
  763. // return nil, 0, err
  764. // }
  765. // article := new(models.CygxArticle)
  766. // err = json.Unmarshal(articleJson, &article)
  767. // if err != nil {
  768. // return nil, 0, err
  769. // }
  770. // searchItem := new(models.SearchItem)
  771. // searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  772. // if len(v.Highlight["BodyText"]) > 0 {
  773. // searchItem.Body = v.Highlight["BodyText"]
  774. // } else {
  775. // bodyRune := []rune(article.BodyText)
  776. // bodyRuneLen := len(bodyRune)
  777. // if bodyRuneLen > 100 {
  778. // bodyRuneLen = 100
  779. // }
  780. // body := string(bodyRune[:bodyRuneLen])
  781. // searchItem.Body = []string{body}
  782. // }
  783. // var title string
  784. // if len(v.Highlight["Title"]) > 0 {
  785. // title = v.Highlight["Title"][0]
  786. // } else {
  787. // title = article.Title
  788. // }
  789. // searchItem.Title = title
  790. // searchItem.PublishDate = article.PublishDate
  791. //
  792. // result = append(result, searchItem)
  793. // }
  794. // }
  795. // total = searchByMatch.Hits.TotalHits.Value
  796. //}
  797. return
  798. }
  799. func EsMultiMatchFunctionScoreQueryFix(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
  800. client := utils.Client
  801. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  802. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  803. boolquery := elastic.NewBoolQuery()
  804. matchArr := make([]elastic.Query, 0)
  805. n := 0
  806. keyWordLen := len(keyWordArr)
  807. if keyWordLen <= 0 {
  808. keyWordArr = append(keyWordArr, keyWord)
  809. keyWordLen = len(keyWordArr)
  810. }
  811. fmt.Println(keyWordArr)
  812. keyWordWeight := GetWeight(keyWordLen)
  813. for k, v := range keyWordArr {
  814. if v != "" {
  815. weight := float64(keyWordWeight[k])
  816. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  817. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  818. bodyFunctionQuery.Query(multiMatch)
  819. bodyFunctions := elastic.NewWeightFactorFunction(weight)
  820. bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  821. bodyFunctionQuery.BoostMode("replace")
  822. matchArr = append(matchArr, bodyFunctionQuery)
  823. }
  824. n++
  825. }
  826. boolquery.Should(matchArr...)
  827. highlight := elastic.NewHighlight()
  828. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  829. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  830. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  831. searchByMatch, err := request.Do(context.Background())
  832. if searchByMatch != nil {
  833. matchResult, _ := json.Marshal(searchByMatch)
  834. utils.FileLog.Info("%s", string(matchResult))
  835. if searchByMatch.Hits != nil {
  836. for _, v := range searchByMatch.Hits.Hits {
  837. articleJson, err := v.Source.MarshalJSON()
  838. utils.FileLog.Info("%s", string(articleJson))
  839. if err != nil {
  840. return nil, 0, err
  841. }
  842. article := new(models.CygxArticle)
  843. err = json.Unmarshal(articleJson, &article)
  844. if err != nil {
  845. return nil, 0, err
  846. }
  847. searchItem := new(models.SearchItem)
  848. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  849. if len(v.Highlight["BodyText"]) > 0 {
  850. searchItem.Body = v.Highlight["BodyText"]
  851. } else {
  852. bodyRune := []rune(article.BodyText)
  853. bodyRuneLen := len(bodyRune)
  854. if bodyRuneLen > 100 {
  855. bodyRuneLen = 100
  856. }
  857. body := string(bodyRune[:bodyRuneLen])
  858. searchItem.Body = []string{body}
  859. }
  860. var title string
  861. if len(v.Highlight["Title"]) > 0 {
  862. title = v.Highlight["Title"][0]
  863. } else {
  864. title = article.Title
  865. }
  866. searchItem.Title = title
  867. searchItem.PublishDate = article.PublishDate
  868. result = append(result, searchItem)
  869. }
  870. }
  871. total = searchByMatch.Hits.TotalHits.Value
  872. }
  873. return
  874. }
  875. func GetWeight(length int) []int {
  876. steep := 10
  877. intArr := make([]int, 0)
  878. for i := 1; i <= length; i++ {
  879. if i == 1 {
  880. intArr = append(intArr, 1)
  881. } else {
  882. min := GetArrSum(intArr)
  883. maxVal := utils.GetRandInt(min, min+steep)
  884. intArr = append(intArr, maxVal)
  885. }
  886. }
  887. //数组排序
  888. sort.Slice(intArr, func(i, j int) bool {
  889. return intArr[i] > intArr[j]
  890. })
  891. return intArr
  892. }
  893. func GetArrSum(intArr []int) (sum int) {
  894. for _, val := range intArr {
  895. //累计求和
  896. sum += val
  897. }
  898. return
  899. }
  900. //func init() {
  901. // fmt.Println("start")
  902. // keyWord:="阿里巴巴"
  903. // keyWordArr, _ := GetIndustryMapNameSliceV2(keyWord)
  904. // keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  905. // fmt.Println(keyWordArr)
  906. // fmt.Println("end")
  907. //}
  908. func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
  909. client := utils.Client
  910. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  911. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  912. //artidArr := make([]elastic.Query, 0)
  913. //matchArr := make([]elastic.Query, 0)
  914. n := 0
  915. keyWordLen := len(keyWordArr)
  916. if keyWordLen <= 0 {
  917. keyWordArr = append(keyWordArr, keyWord)
  918. keyWordLen = len(keyWordArr)
  919. }
  920. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  921. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  922. //keyWordWeight := GetWeight(keyWordLen)
  923. for _, v := range keyWordArr {
  924. if v != "" {
  925. matchArr := make([]elastic.Query, 0)
  926. boolquery := elastic.NewBoolQuery()
  927. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  928. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  929. bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
  930. //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  931. multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart").Boost(100)
  932. bodyFunctionQuery.Query(multiMatch)
  933. matchArr = append(matchArr, bodyFunctionQuery)
  934. multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart").Boost(1)
  935. bodyFunctionQuery2.Query(multiMatch)
  936. matchArr = append(matchArr, bodyFunctionQuery2)
  937. //multiMatch = elastic.NewMultiMatchQuery(1, "IsSummary")
  938. bodyFunctionQuery3.Query(multiMatch)
  939. matchArr = append(matchArr, bodyFunctionQuery3)
  940. boolquery.Should(matchArr...)
  941. //multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart")
  942. //bodyFunctionQuery.Query(multiMatch)
  943. //matchArr = append(matchArr, bodyFunctionQuery)
  944. //boolquery.Should(matchArr...)
  945. highlight := elastic.NewHighlight()
  946. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  947. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  948. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
  949. if orderColumn == "Matching" {
  950. request = client.Search(indexName).Highlight(highlight).From(0).Size(pageSize).Query(boolquery)
  951. }
  952. searchByMatch, err := request.Do(context.Background())
  953. if err != nil {
  954. return nil, 0, err
  955. }
  956. if searchByMatch != nil {
  957. if searchByMatch.Hits != nil {
  958. for _, v := range searchByMatch.Hits.Hits {
  959. var isAppend bool
  960. articleJson, err := v.Source.MarshalJSON()
  961. if err != nil {
  962. return nil, 0, err
  963. }
  964. article := new(models.CygxArticleEs)
  965. err = json.Unmarshal(articleJson, &article)
  966. if err != nil {
  967. return nil, 0, err
  968. }
  969. searchItem := new(models.SearchItem)
  970. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  971. if len(v.Highlight["BodyText"]) > 0 {
  972. searchItem.Body = v.Highlight["BodyText"]
  973. } else {
  974. bodyRune := []rune(article.BodyText)
  975. bodyRuneLen := len(bodyRune)
  976. if bodyRuneLen > 100 {
  977. bodyRuneLen = 100
  978. }
  979. body := string(bodyRune[:bodyRuneLen])
  980. searchItem.Body = []string{body}
  981. }
  982. var title string
  983. if len(v.Highlight["Title"]) > 0 {
  984. title = v.Highlight["Title"][0]
  985. } else {
  986. title = article.Title
  987. }
  988. searchItem.Title = title
  989. searchItem.PublishDate = article.PublishDate
  990. searchItem.ExpertBackground = article.ExpertBackground
  991. searchItem.CategoryId = article.CategoryId
  992. for _, v_result := range result {
  993. if v_result.ArticleId == searchItem.ArticleId {
  994. isAppend = true
  995. }
  996. }
  997. if !isAppend {
  998. result = append(result, searchItem)
  999. }
  1000. }
  1001. }
  1002. //total += searchByMatch.Hits.TotalHits.Value
  1003. }
  1004. }
  1005. n++
  1006. }
  1007. total = int64(len(result))
  1008. return
  1009. }
  1010. func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  1011. client := utils.Client
  1012. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  1013. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  1014. boolquery := elastic.NewBoolQuery()
  1015. matchArr := make([]elastic.Query, 0)
  1016. //matchArr2 := make([]elastic.Query, 0)
  1017. n := 0
  1018. keyWordLen := len(keyWordArr)
  1019. if keyWordLen <= 0 {
  1020. keyWordArr = append(keyWordArr, keyWord)
  1021. keyWordLen = len(keyWordArr)
  1022. }
  1023. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  1024. for _, v := range keyWordArr {
  1025. if v != "" {
  1026. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  1027. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  1028. bodyFunctionQuery.Query(multiMatch)
  1029. matchArr = append(matchArr, bodyFunctionQuery)
  1030. }
  1031. n++
  1032. }
  1033. boolquery.Should(matchArr...)
  1034. highlight := elastic.NewHighlight()
  1035. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  1036. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1037. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).Size(pageSize).Query(boolquery)
  1038. searchByMatch, err := request.Do(context.Background())
  1039. if searchByMatch != nil {
  1040. matchResult, _ := json.Marshal(searchByMatch)
  1041. utils.FileLog.Info("%s", string(matchResult))
  1042. fmt.Println(len(searchByMatch.Hits.Hits))
  1043. if searchByMatch.Hits != nil {
  1044. for _, v := range searchByMatch.Hits.Hits {
  1045. articleJson, err := v.Source.MarshalJSON()
  1046. utils.FileLog.Info("%s", string(articleJson))
  1047. if err != nil {
  1048. return nil, 0, err
  1049. }
  1050. article := new(models.CygxArticleEs)
  1051. err = json.Unmarshal(articleJson, &article)
  1052. if err != nil {
  1053. return nil, 0, err
  1054. }
  1055. searchItem := new(models.SearchItem)
  1056. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1057. if len(v.Highlight["BodyText"]) > 0 {
  1058. searchItem.Body = v.Highlight["BodyText"]
  1059. } else {
  1060. bodyRune := []rune(article.BodyText)
  1061. bodyRuneLen := len(bodyRune)
  1062. if bodyRuneLen > 100 {
  1063. bodyRuneLen = 100
  1064. }
  1065. body := string(bodyRune[:bodyRuneLen])
  1066. searchItem.Body = []string{body}
  1067. }
  1068. var title string
  1069. if len(v.Highlight["Title"]) > 0 {
  1070. title = v.Highlight["Title"][0]
  1071. } else {
  1072. title = article.Title
  1073. }
  1074. searchItem.Title = title
  1075. searchItem.PublishDate = article.PublishDate
  1076. searchItem.ExpertBackground = article.ExpertBackground
  1077. searchItem.CategoryId = article.CategoryId
  1078. result = append(result, searchItem)
  1079. }
  1080. }
  1081. total = searchByMatch.Hits.TotalHits.Value
  1082. }
  1083. return
  1084. }
  1085. func EsSearchReport(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  1086. client := utils.Client
  1087. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  1088. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  1089. n := 0
  1090. keyWordLen := len(keyWordArr)
  1091. if keyWordLen <= 0 {
  1092. keyWordArr = append(keyWordArr, keyWord)
  1093. keyWordLen = len(keyWordArr)
  1094. }
  1095. for _, v := range keyWordArr {
  1096. fmt.Println(v)
  1097. }
  1098. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  1099. for _, v := range keyWordArr {
  1100. if v != "" {
  1101. matchArr := make([]elastic.Query, 0)
  1102. boolquery := elastic.NewBoolQuery()
  1103. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  1104. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  1105. multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart")
  1106. bodyFunctionQuery.Query(multiMatch)
  1107. matchArr = append(matchArr, bodyFunctionQuery)
  1108. multiMatch = elastic.NewMultiMatchQuery(1, "IsReport")
  1109. bodyFunctionQuery2.Query(multiMatch)
  1110. matchArr = append(matchArr, bodyFunctionQuery2)
  1111. boolquery.Must(matchArr...)
  1112. highlight := elastic.NewHighlight()
  1113. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1114. highlight = highlight.Fields(elastic.NewHighlighterField("Title"))
  1115. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
  1116. searchByMatch, err := request.Do(context.Background())
  1117. if err != nil {
  1118. return nil, 0, err
  1119. }
  1120. if searchByMatch != nil {
  1121. if searchByMatch.Hits != nil {
  1122. for _, v := range searchByMatch.Hits.Hits {
  1123. var isAppend bool
  1124. articleJson, err := v.Source.MarshalJSON()
  1125. if err != nil {
  1126. return nil, 0, err
  1127. }
  1128. article := new(models.CygxArticleEs)
  1129. err = json.Unmarshal(articleJson, &article)
  1130. if err != nil {
  1131. return nil, 0, err
  1132. }
  1133. searchItem := new(models.SearchItem)
  1134. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1135. var title string
  1136. if len(v.Highlight["Title"]) > 0 {
  1137. title = v.Highlight["Title"][0]
  1138. } else {
  1139. title = article.Title
  1140. }
  1141. searchItem.Title = title
  1142. searchItem.PublishDate = article.PublishDate
  1143. for _, v_result := range result {
  1144. if v_result.ArticleId == searchItem.ArticleId {
  1145. isAppend = true
  1146. }
  1147. }
  1148. if !isAppend {
  1149. result = append(result, searchItem)
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. n++
  1156. }
  1157. total = int64(len(result))
  1158. return
  1159. }
  1160. //分页
  1161. func EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  1162. client := utils.Client
  1163. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  1164. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  1165. boolquery := elastic.NewBoolQuery()
  1166. matchArr := make([]elastic.Query, 0)
  1167. n := 0
  1168. keyWordLen := len(keyWordArr)
  1169. if keyWordLen <= 0 {
  1170. keyWordArr = append(keyWordArr, keyWord)
  1171. keyWordLen = len(keyWordArr)
  1172. }
  1173. for _, v := range keyWordArr {
  1174. if v != "" {
  1175. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  1176. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  1177. bodyFunctionQuery.Query(multiMatch)
  1178. matchArr = append(matchArr, bodyFunctionQuery)
  1179. }
  1180. n++
  1181. }
  1182. boolquery.Should(matchArr...)
  1183. highlight := elastic.NewHighlight()
  1184. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  1185. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1186. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
  1187. searchByMatch, err := request.Do(context.Background())
  1188. if searchByMatch != nil {
  1189. if searchByMatch.Hits != nil {
  1190. for _, v := range searchByMatch.Hits.Hits {
  1191. articleJson, err := v.Source.MarshalJSON()
  1192. if err != nil {
  1193. return nil, 0, err
  1194. }
  1195. article := new(models.CygxArticleEs)
  1196. err = json.Unmarshal(articleJson, &article)
  1197. if err != nil {
  1198. return nil, 0, err
  1199. }
  1200. searchItem := new(models.SearchItem)
  1201. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1202. if len(v.Highlight["BodyText"]) > 0 {
  1203. searchItem.Body = v.Highlight["BodyText"]
  1204. } else {
  1205. bodyRune := []rune(article.BodyText)
  1206. bodyRuneLen := len(bodyRune)
  1207. if bodyRuneLen > 100 {
  1208. bodyRuneLen = 100
  1209. }
  1210. body := string(bodyRune[:bodyRuneLen])
  1211. searchItem.Body = []string{body}
  1212. }
  1213. var title string
  1214. if len(v.Highlight["Title"]) > 0 {
  1215. title = v.Highlight["Title"][0]
  1216. } else {
  1217. title = article.Title
  1218. }
  1219. searchItem.Title = title
  1220. searchItem.PublishDate = article.PublishDate
  1221. searchItem.ExpertBackground = article.ExpertBackground
  1222. searchItem.CategoryId = article.CategoryId
  1223. result = append(result, searchItem)
  1224. }
  1225. }
  1226. total = searchByMatch.Hits.TotalHits.Value
  1227. }
  1228. return
  1229. }
  1230. func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
  1231. client := utils.Client
  1232. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  1233. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  1234. keyWordLen := len(keyWordArr)
  1235. if keyWordLen <= 0 {
  1236. keyWordArr = append(keyWordArr, keyWord)
  1237. keyWordLen = len(keyWordArr)
  1238. }
  1239. var keyWords string
  1240. for _, v := range keyWordArr {
  1241. keyWords += v + " "
  1242. }
  1243. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  1244. //keyWordWeight := GetWeight(keyWordLen)
  1245. matchArr := make([]elastic.Query, 0)
  1246. boolquery := elastic.NewBoolQuery()
  1247. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  1248. bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
  1249. bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
  1250. multiMatch := elastic.NewMultiMatchQuery(keyWords, "Title").Analyzer("ik_smart").Boost(100)
  1251. bodyFunctionQuery.Query(multiMatch)
  1252. matchArr = append(matchArr, bodyFunctionQuery)
  1253. multiMatch = elastic.NewMultiMatchQuery(keyWords, "BodyText").Analyzer("ik_smart").Boost(1)
  1254. bodyFunctionQuery2.Query(multiMatch)
  1255. matchArr = append(matchArr, bodyFunctionQuery2)
  1256. bodyFunctionQuery3.Query(multiMatch)
  1257. matchArr = append(matchArr, bodyFunctionQuery3)
  1258. boolquery.Should(matchArr...)
  1259. highlight := elastic.NewHighlight()
  1260. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1261. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  1262. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
  1263. if orderColumn == "Matching" {
  1264. request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  1265. }
  1266. searchByMatch, err := request.Do(context.Background())
  1267. if err != nil {
  1268. return nil, 0, err
  1269. }
  1270. if searchByMatch != nil {
  1271. if searchByMatch.Hits != nil {
  1272. for _, v := range searchByMatch.Hits.Hits {
  1273. var isAppend bool
  1274. articleJson, err := v.Source.MarshalJSON()
  1275. if err != nil {
  1276. return nil, 0, err
  1277. }
  1278. article := new(models.CygxArticleEs)
  1279. err = json.Unmarshal(articleJson, &article)
  1280. if err != nil {
  1281. return nil, 0, err
  1282. }
  1283. searchItem := new(models.SearchItem)
  1284. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1285. if len(v.Highlight["BodyText"]) > 0 {
  1286. searchItem.Body = v.Highlight["BodyText"]
  1287. } else {
  1288. bodyRune := []rune(article.BodyText)
  1289. bodyRuneLen := len(bodyRune)
  1290. if bodyRuneLen > 100 {
  1291. bodyRuneLen = 100
  1292. }
  1293. body := string(bodyRune[:bodyRuneLen])
  1294. searchItem.Body = []string{body}
  1295. }
  1296. var title string
  1297. if len(v.Highlight["Title"]) > 0 {
  1298. title = v.Highlight["Title"][0]
  1299. } else {
  1300. title = article.Title
  1301. }
  1302. searchItem.Title = title
  1303. searchItem.PublishDate = article.PublishDate
  1304. searchItem.ExpertBackground = article.ExpertBackground
  1305. searchItem.CategoryId = article.CategoryId
  1306. for _, v_result := range result {
  1307. if v_result.ArticleId == searchItem.ArticleId {
  1308. isAppend = true
  1309. }
  1310. }
  1311. if !isAppend {
  1312. result = append(result, searchItem)
  1313. }
  1314. }
  1315. }
  1316. }
  1317. total += searchByMatch.Hits.TotalHits.Value
  1318. return
  1319. }