elastic.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. fmt.Println(keyWordArr)
  862. //如果没有联想词,而且查询的还是联想词就返回
  863. if ikType == 2 && keyWordLen == 1 {
  864. return
  865. }
  866. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  867. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  868. mustMap := make([]interface{}, 0)
  869. shouldMap := make(map[string]interface{}, 0)
  870. shouldMapquery := make([]interface{}, 0)
  871. mustNotMap := make([]interface{}, 0)
  872. shouldNotMap := make(map[string]interface{}, 0)
  873. shouldNotMapquery := make([]interface{}, 0)
  874. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  875. //keyWordWeight := GetWeight(keyWordLen)
  876. var boost int
  877. //lenkeyWordArr := len(keyWordArr)
  878. for k, v := range keyWordArr {
  879. if k == 0 {
  880. boost = 2 * 1000
  881. } else {
  882. boost = 1
  883. }
  884. //如果是 2:查询除了查询键入词之外的联想词
  885. if k == 0 && ikType == 2 {
  886. if v != "" {
  887. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  888. "function_score": map[string]interface{}{
  889. "query": map[string]interface{}{
  890. "multi_match": map[string]interface{}{
  891. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  892. "boost": boost, //给查询的值赋予权重
  893. "fields": []interface{}{"Title"},
  894. "query": v,
  895. },
  896. },
  897. },
  898. })
  899. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  900. "function_score": map[string]interface{}{
  901. "query": map[string]interface{}{
  902. "multi_match": map[string]interface{}{
  903. "boost": boost, //给查询的值赋予权重
  904. "fields": []interface{}{"Abstract"},
  905. "query": v,
  906. },
  907. },
  908. },
  909. })
  910. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  911. "function_score": map[string]interface{}{
  912. "query": map[string]interface{}{
  913. "multi_match": map[string]interface{}{
  914. "boost": boost, //给查询的值赋予权重
  915. "fields": []interface{}{"Annotation"},
  916. "query": v,
  917. },
  918. },
  919. },
  920. })
  921. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  922. "function_score": map[string]interface{}{
  923. "query": map[string]interface{}{
  924. "multi_match": map[string]interface{}{
  925. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  926. "boost": boost, //给查询的值赋予权重
  927. "fields": []interface{}{"BodyText"},
  928. "query": v,
  929. },
  930. },
  931. },
  932. })
  933. }
  934. continue
  935. }
  936. //如果是 1:查询键入词
  937. if k > 0 && ikType == 1 {
  938. continue
  939. }
  940. if v != "" {
  941. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  942. "function_score": map[string]interface{}{
  943. "query": map[string]interface{}{
  944. "multi_match": map[string]interface{}{
  945. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  946. "boost": boost, //给查询的值赋予权重
  947. "fields": []interface{}{"Title"},
  948. "query": v,
  949. },
  950. },
  951. },
  952. })
  953. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  954. "function_score": map[string]interface{}{
  955. "query": map[string]interface{}{
  956. "multi_match": map[string]interface{}{
  957. "boost": boost, //给查询的值赋予权重
  958. "fields": []interface{}{"Abstract"},
  959. "query": v,
  960. },
  961. },
  962. },
  963. })
  964. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  965. "function_score": map[string]interface{}{
  966. "query": map[string]interface{}{
  967. "multi_match": map[string]interface{}{
  968. "boost": boost, //给查询的值赋予权重
  969. "fields": []interface{}{"Annotation"},
  970. "query": v,
  971. },
  972. },
  973. },
  974. })
  975. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  976. "function_score": map[string]interface{}{
  977. "query": map[string]interface{}{
  978. "multi_match": map[string]interface{}{
  979. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  980. "boost": boost, //给查询的值赋予权重
  981. "fields": []interface{}{"BodyText"},
  982. "query": v,
  983. },
  984. },
  985. },
  986. })
  987. }
  988. }
  989. shouldMap = map[string]interface{}{
  990. "should": shouldMapquery,
  991. }
  992. shouldNotMap = map[string]interface{}{
  993. "should": shouldNotMapquery,
  994. }
  995. //排序
  996. sortMap := make([]interface{}, 0)
  997. //时间
  998. sortMap = append(sortMap, map[string]interface{}{
  999. "PublishDate": map[string]interface{}{
  1000. "order": "desc",
  1001. },
  1002. })
  1003. //sortMap = append(sortMap, map[string]interface{}{
  1004. // "_score": map[string]interface{}{
  1005. // "order": "desc",
  1006. // },
  1007. //})
  1008. //高亮
  1009. highlightMap := make(map[string]interface{}, 0)
  1010. highlightMap = map[string]interface{}{
  1011. "fields": map[string]interface{}{
  1012. "BodyText": map[string]interface{}{},
  1013. "Title": map[string]interface{}{},
  1014. "Abstract": map[string]interface{}{},
  1015. "Annotation": map[string]interface{}{},
  1016. },
  1017. //样式 红色
  1018. "post_tags": []interface{}{"</font>"},
  1019. "pre_tags": []interface{}{"<font color='red'>"},
  1020. "fragment_size": 50,
  1021. }
  1022. mustMap = append(mustMap, map[string]interface{}{
  1023. "bool": shouldMap,
  1024. })
  1025. mustNotMap = append(mustNotMap, map[string]interface{}{
  1026. "bool": shouldNotMap,
  1027. })
  1028. queryMap := map[string]interface{}{
  1029. "query": map[string]interface{}{
  1030. "bool": map[string]interface{}{
  1031. "must": mustMap,
  1032. },
  1033. },
  1034. }
  1035. //把第一次键入词的筛选条件过滤掉
  1036. if ikType == 2 {
  1037. queryMap = map[string]interface{}{
  1038. "query": map[string]interface{}{
  1039. "bool": map[string]interface{}{
  1040. "must": mustMap,
  1041. "must_not": mustNotMap,
  1042. },
  1043. },
  1044. }
  1045. }
  1046. if orderColumn == "Matching" {
  1047. queryMap["sort"] = sortMap
  1048. }
  1049. queryMap["from"] = startSize
  1050. queryMap["size"] = pageSize
  1051. queryMap["highlight"] = highlightMap
  1052. jsonBytes, _ := json.Marshal(queryMap)
  1053. fmt.Println(string(jsonBytes))
  1054. //utils.FileLog.Info(string(jsonBytes))
  1055. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1056. searchByMatch, err := request.Do(context.Background())
  1057. if searchByMatch != nil {
  1058. if searchByMatch.Hits != nil {
  1059. for _, v := range searchByMatch.Hits.Hits {
  1060. var isAppend bool
  1061. articleJson, err := v.Source.MarshalJSON()
  1062. if err != nil {
  1063. return nil, 0, err
  1064. }
  1065. article := new(models.CygxArticleEs)
  1066. err = json.Unmarshal(articleJson, &article)
  1067. if err != nil {
  1068. return nil, 0, err
  1069. }
  1070. searchItem := new(models.SearchItem)
  1071. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1072. if len(v.Highlight["Annotation"]) > 0 {
  1073. for _, vText := range v.Highlight["Annotation"] {
  1074. searchItem.Body = append(searchItem.Body, vText)
  1075. }
  1076. }
  1077. if len(v.Highlight["Abstract"]) > 0 {
  1078. for _, vText := range v.Highlight["Abstract"] {
  1079. searchItem.Body = append(searchItem.Body, vText)
  1080. }
  1081. }
  1082. if len(v.Highlight["BodyText"]) > 0 {
  1083. for _, vText := range v.Highlight["BodyText"] {
  1084. searchItem.Body = append(searchItem.Body, vText)
  1085. }
  1086. }
  1087. if len(searchItem.Body) == 0 {
  1088. bodyRune := []rune(article.BodyText)
  1089. bodyRuneLen := len(bodyRune)
  1090. if bodyRuneLen > 100 {
  1091. bodyRuneLen = 100
  1092. }
  1093. body := string(bodyRune[:bodyRuneLen])
  1094. searchItem.Body = []string{body}
  1095. }
  1096. //if len(v.Highlight["BodyText"]) > 0 {
  1097. // searchItem.Body = v.Highlight["BodyText"]
  1098. //} else {
  1099. // bodyRune := []rune(article.BodyText)
  1100. // bodyRuneLen := len(bodyRune)
  1101. // if bodyRuneLen > 100 {
  1102. // bodyRuneLen = 100
  1103. // }
  1104. // body := string(bodyRune[:bodyRuneLen])
  1105. // searchItem.Body = []string{body}
  1106. //}
  1107. var title string
  1108. if len(v.Highlight["Title"]) > 0 {
  1109. title = v.Highlight["Title"][0]
  1110. } else {
  1111. title = article.Title
  1112. }
  1113. searchItem.Title = title
  1114. searchItem.PublishDate = article.PublishDate
  1115. searchItem.ExpertBackground = article.ExpertBackground
  1116. searchItem.CategoryId = article.CategoryId
  1117. for _, v_result := range result {
  1118. if v_result.ArticleId == searchItem.ArticleId {
  1119. isAppend = true
  1120. }
  1121. }
  1122. if !isAppend {
  1123. result = append(result, searchItem)
  1124. }
  1125. }
  1126. }
  1127. total = searchByMatch.Hits.TotalHits.Value
  1128. }
  1129. return
  1130. }
  1131. func EsArticleSearchBody(keyWord string, startSize, pageSize int, orderColumn string, searchType int) (result []*models.SearchItem, total int64, err error) {
  1132. if keyWord == "" {
  1133. return
  1134. }
  1135. indexName := utils.IndexName
  1136. client := utils.Client
  1137. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  1138. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  1139. mustMap := make([]interface{}, 0)
  1140. shouldMap := make(map[string]interface{}, 0)
  1141. shouldMapquery := make([]interface{}, 0)
  1142. mustNotMap := make([]interface{}, 0)
  1143. shouldNotMap := make(map[string]interface{}, 0)
  1144. shouldNotMapquery := make([]interface{}, 0)
  1145. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  1146. //keyWordWeight := GetWeight(keyWordLen)
  1147. var boost int
  1148. //如果是 2:查询标题,摘要,核心观点的词
  1149. if searchType == 1 {
  1150. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1151. "function_score": map[string]interface{}{
  1152. "query": map[string]interface{}{
  1153. "multi_match": map[string]interface{}{
  1154. "boost": boost, //给查询的值赋予权重
  1155. "fields": []interface{}{"Title"},
  1156. "query": keyWord,
  1157. },
  1158. },
  1159. },
  1160. })
  1161. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1162. "function_score": map[string]interface{}{
  1163. "query": map[string]interface{}{
  1164. "multi_match": map[string]interface{}{
  1165. "boost": boost, //给查询的值赋予权重
  1166. "fields": []interface{}{"Abstract"},
  1167. "query": keyWord,
  1168. },
  1169. },
  1170. },
  1171. })
  1172. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1173. "function_score": map[string]interface{}{
  1174. "query": map[string]interface{}{
  1175. "multi_match": map[string]interface{}{
  1176. "boost": boost, //给查询的值赋予权重
  1177. "fields": []interface{}{"Annotation"},
  1178. "query": keyWord,
  1179. },
  1180. },
  1181. },
  1182. })
  1183. }
  1184. //如果是 2:查询body的相关词
  1185. if searchType == 2 {
  1186. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1187. "function_score": map[string]interface{}{
  1188. "query": map[string]interface{}{
  1189. "multi_match": map[string]interface{}{
  1190. "boost": boost, //给查询的值赋予权重
  1191. "fields": []interface{}{"Title"},
  1192. "query": keyWord,
  1193. },
  1194. },
  1195. },
  1196. })
  1197. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1198. "function_score": map[string]interface{}{
  1199. "query": map[string]interface{}{
  1200. "multi_match": map[string]interface{}{
  1201. "boost": boost, //给查询的值赋予权重
  1202. "fields": []interface{}{"Abstract"},
  1203. "query": keyWord,
  1204. },
  1205. },
  1206. },
  1207. })
  1208. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1209. "function_score": map[string]interface{}{
  1210. "query": map[string]interface{}{
  1211. "multi_match": map[string]interface{}{
  1212. "boost": boost, //给查询的值赋予权重
  1213. "fields": []interface{}{"Annotation"},
  1214. "query": keyWord,
  1215. },
  1216. },
  1217. },
  1218. })
  1219. //shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1220. // "function_score": map[string]interface{}{
  1221. // "query": map[string]interface{}{
  1222. // "multi_match": map[string]interface{}{
  1223. // //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  1224. // "boost": boost, //给查询的值赋予权重
  1225. // "fields": []interface{}{"BodyText"},
  1226. // "query": keyWord,
  1227. // },
  1228. // },
  1229. // },
  1230. //})
  1231. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1232. "function_score": map[string]interface{}{
  1233. "query": map[string]interface{}{
  1234. "multi_match": map[string]interface{}{
  1235. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  1236. "boost": boost, //给查询的值赋予权重
  1237. "fields": []interface{}{"BodyText"},
  1238. "query": keyWord,
  1239. },
  1240. },
  1241. },
  1242. })
  1243. }
  1244. shouldMap = map[string]interface{}{
  1245. "should": shouldMapquery,
  1246. }
  1247. shouldNotMap = map[string]interface{}{
  1248. "should": shouldNotMapquery,
  1249. }
  1250. //排序
  1251. sortMap := make([]interface{}, 0)
  1252. //时间
  1253. sortMap = append(sortMap, map[string]interface{}{
  1254. "PublishDate": map[string]interface{}{
  1255. "order": "desc",
  1256. },
  1257. })
  1258. //高亮
  1259. highlightMap := make(map[string]interface{}, 0)
  1260. highlightMap = map[string]interface{}{
  1261. "fields": map[string]interface{}{
  1262. "BodyText": map[string]interface{}{},
  1263. "Title": map[string]interface{}{},
  1264. "Abstract": map[string]interface{}{},
  1265. "Annotation": map[string]interface{}{},
  1266. },
  1267. //样式 红色
  1268. "post_tags": []interface{}{"</font>"},
  1269. "pre_tags": []interface{}{"<font color='red'>"},
  1270. "fragment_size": 50,
  1271. }
  1272. mustMap = append(mustMap, map[string]interface{}{
  1273. "bool": shouldMap,
  1274. })
  1275. mustNotMap = append(mustNotMap, map[string]interface{}{
  1276. "bool": shouldNotMap,
  1277. })
  1278. queryMap := map[string]interface{}{
  1279. "query": map[string]interface{}{
  1280. "bool": map[string]interface{}{
  1281. "must": mustMap,
  1282. },
  1283. },
  1284. }
  1285. //把第一次的筛选条件过滤掉
  1286. if searchType == 2 {
  1287. queryMap = map[string]interface{}{
  1288. "query": map[string]interface{}{
  1289. "bool": map[string]interface{}{
  1290. "must": mustMap,
  1291. "must_not": mustNotMap,
  1292. },
  1293. },
  1294. }
  1295. }
  1296. if orderColumn == "Matching" {
  1297. queryMap["sort"] = sortMap
  1298. }
  1299. queryMap["from"] = startSize
  1300. queryMap["size"] = pageSize
  1301. queryMap["highlight"] = highlightMap
  1302. jsonBytes, _ := json.Marshal(queryMap)
  1303. fmt.Println(string(jsonBytes))
  1304. //utils.FileLog.Info(string(jsonBytes))
  1305. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1306. searchByMatch, err := request.Do(context.Background())
  1307. if searchByMatch != nil {
  1308. if searchByMatch.Hits != nil {
  1309. for _, v := range searchByMatch.Hits.Hits {
  1310. var isAppend bool
  1311. articleJson, err := v.Source.MarshalJSON()
  1312. if err != nil {
  1313. return nil, 0, err
  1314. }
  1315. article := new(models.CygxArticleEs)
  1316. err = json.Unmarshal(articleJson, &article)
  1317. if err != nil {
  1318. return nil, 0, err
  1319. }
  1320. searchItem := new(models.SearchItem)
  1321. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1322. if len(v.Highlight["Annotation"]) > 0 {
  1323. for _, vText := range v.Highlight["Annotation"] {
  1324. searchItem.Body = append(searchItem.Body, vText)
  1325. }
  1326. }
  1327. if len(v.Highlight["Abstract"]) > 0 {
  1328. for _, vText := range v.Highlight["Abstract"] {
  1329. searchItem.Body = append(searchItem.Body, vText)
  1330. }
  1331. }
  1332. if len(v.Highlight["BodyText"]) > 0 {
  1333. for _, vText := range v.Highlight["BodyText"] {
  1334. searchItem.Body = append(searchItem.Body, vText)
  1335. }
  1336. }
  1337. if len(searchItem.Body) == 0 {
  1338. bodyRune := []rune(article.BodyText)
  1339. bodyRuneLen := len(bodyRune)
  1340. if bodyRuneLen > 100 {
  1341. bodyRuneLen = 100
  1342. }
  1343. body := string(bodyRune[:bodyRuneLen])
  1344. searchItem.Body = []string{body}
  1345. }
  1346. //if len(v.Highlight["BodyText"]) > 0 {
  1347. // searchItem.Body = v.Highlight["BodyText"]
  1348. //} else {
  1349. // bodyRune := []rune(article.BodyText)
  1350. // bodyRuneLen := len(bodyRune)
  1351. // if bodyRuneLen > 100 {
  1352. // bodyRuneLen = 100
  1353. // }
  1354. // body := string(bodyRune[:bodyRuneLen])
  1355. // searchItem.Body = []string{body}
  1356. //}
  1357. var title string
  1358. if len(v.Highlight["Title"]) > 0 {
  1359. title = v.Highlight["Title"][0]
  1360. } else {
  1361. title = article.Title
  1362. }
  1363. searchItem.Title = title
  1364. searchItem.PublishDate = article.PublishDate
  1365. searchItem.ExpertBackground = article.ExpertBackground
  1366. searchItem.CategoryId = article.CategoryId
  1367. for _, v_result := range result {
  1368. if v_result.ArticleId == searchItem.ArticleId {
  1369. isAppend = true
  1370. }
  1371. }
  1372. if !isAppend {
  1373. result = append(result, searchItem)
  1374. }
  1375. }
  1376. }
  1377. total = searchByMatch.Hits.TotalHits.Value
  1378. }
  1379. return
  1380. }