elastic.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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. }
  1021. mustMap = append(mustMap, map[string]interface{}{
  1022. "bool": shouldMap,
  1023. })
  1024. mustNotMap = append(mustNotMap, map[string]interface{}{
  1025. "bool": shouldNotMap,
  1026. })
  1027. queryMap := map[string]interface{}{
  1028. "query": map[string]interface{}{
  1029. "bool": map[string]interface{}{
  1030. "must": mustMap,
  1031. },
  1032. },
  1033. }
  1034. //把第一次键入词的筛选条件过滤掉
  1035. if ikType == 2 {
  1036. queryMap = map[string]interface{}{
  1037. "query": map[string]interface{}{
  1038. "bool": map[string]interface{}{
  1039. "must": mustMap,
  1040. "must_not": mustNotMap,
  1041. },
  1042. },
  1043. }
  1044. }
  1045. if orderColumn == "Matching" {
  1046. queryMap["sort"] = sortMap
  1047. }
  1048. queryMap["from"] = startSize
  1049. queryMap["size"] = pageSize
  1050. queryMap["highlight"] = highlightMap
  1051. jsonBytes, _ := json.Marshal(queryMap)
  1052. fmt.Println(string(jsonBytes))
  1053. //utils.FileLog.Info(string(jsonBytes))
  1054. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1055. searchByMatch, err := request.Do(context.Background())
  1056. if searchByMatch != nil {
  1057. if searchByMatch.Hits != nil {
  1058. for _, v := range searchByMatch.Hits.Hits {
  1059. var isAppend bool
  1060. articleJson, err := v.Source.MarshalJSON()
  1061. if err != nil {
  1062. return nil, 0, err
  1063. }
  1064. article := new(models.CygxArticleEs)
  1065. err = json.Unmarshal(articleJson, &article)
  1066. if err != nil {
  1067. return nil, 0, err
  1068. }
  1069. searchItem := new(models.SearchItem)
  1070. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1071. if len(v.Highlight["Annotation"]) > 0 {
  1072. for _, vText := range v.Highlight["Annotation"] {
  1073. searchItem.Body = append(searchItem.Body, vText)
  1074. }
  1075. }
  1076. if len(v.Highlight["Abstract"]) > 0 {
  1077. for _, vText := range v.Highlight["Abstract"] {
  1078. searchItem.Body = append(searchItem.Body, vText)
  1079. }
  1080. }
  1081. if len(v.Highlight["BodyText"]) > 0 {
  1082. for _, vText := range v.Highlight["BodyText"] {
  1083. searchItem.Body = append(searchItem.Body, vText)
  1084. }
  1085. }
  1086. if len(searchItem.Body) == 0 {
  1087. bodyRune := []rune(article.BodyText)
  1088. bodyRuneLen := len(bodyRune)
  1089. if bodyRuneLen > 100 {
  1090. bodyRuneLen = 100
  1091. }
  1092. body := string(bodyRune[:bodyRuneLen])
  1093. searchItem.Body = []string{body}
  1094. }
  1095. //if len(v.Highlight["BodyText"]) > 0 {
  1096. // searchItem.Body = v.Highlight["BodyText"]
  1097. //} else {
  1098. // bodyRune := []rune(article.BodyText)
  1099. // bodyRuneLen := len(bodyRune)
  1100. // if bodyRuneLen > 100 {
  1101. // bodyRuneLen = 100
  1102. // }
  1103. // body := string(bodyRune[:bodyRuneLen])
  1104. // searchItem.Body = []string{body}
  1105. //}
  1106. var title string
  1107. if len(v.Highlight["Title"]) > 0 {
  1108. title = v.Highlight["Title"][0]
  1109. } else {
  1110. title = article.Title
  1111. }
  1112. searchItem.Title = title
  1113. searchItem.PublishDate = article.PublishDate
  1114. searchItem.ExpertBackground = article.ExpertBackground
  1115. searchItem.CategoryId = article.CategoryId
  1116. for _, v_result := range result {
  1117. if v_result.ArticleId == searchItem.ArticleId {
  1118. isAppend = true
  1119. }
  1120. }
  1121. if !isAppend {
  1122. result = append(result, searchItem)
  1123. }
  1124. }
  1125. }
  1126. total = searchByMatch.Hits.TotalHits.Value
  1127. }
  1128. return
  1129. }
  1130. func EsArticleSearchBody(keyWord string, startSize, pageSize int, orderColumn string, searchType int) (result []*models.SearchItem, total int64, err error) {
  1131. if keyWord == "" {
  1132. return
  1133. }
  1134. indexName := utils.IndexName
  1135. client := utils.Client
  1136. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  1137. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  1138. mustMap := make([]interface{}, 0)
  1139. shouldMap := make(map[string]interface{}, 0)
  1140. shouldMapquery := make([]interface{}, 0)
  1141. mustNotMap := make([]interface{}, 0)
  1142. shouldNotMap := make(map[string]interface{}, 0)
  1143. shouldNotMapquery := make([]interface{}, 0)
  1144. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  1145. //keyWordWeight := GetWeight(keyWordLen)
  1146. var boost int
  1147. //如果是 2:查询标题,摘要,核心观点的词
  1148. if searchType == 1 {
  1149. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1150. "function_score": map[string]interface{}{
  1151. "query": map[string]interface{}{
  1152. "multi_match": map[string]interface{}{
  1153. "boost": boost, //给查询的值赋予权重
  1154. "fields": []interface{}{"Title"},
  1155. "query": keyWord,
  1156. },
  1157. },
  1158. },
  1159. })
  1160. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1161. "function_score": map[string]interface{}{
  1162. "query": map[string]interface{}{
  1163. "multi_match": map[string]interface{}{
  1164. "boost": boost, //给查询的值赋予权重
  1165. "fields": []interface{}{"Abstract"},
  1166. "query": keyWord,
  1167. },
  1168. },
  1169. },
  1170. })
  1171. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1172. "function_score": map[string]interface{}{
  1173. "query": map[string]interface{}{
  1174. "multi_match": map[string]interface{}{
  1175. "boost": boost, //给查询的值赋予权重
  1176. "fields": []interface{}{"Annotation"},
  1177. "query": keyWord,
  1178. },
  1179. },
  1180. },
  1181. })
  1182. }
  1183. //如果是 2:查询body的相关词
  1184. if searchType == 2 {
  1185. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1186. "function_score": map[string]interface{}{
  1187. "query": map[string]interface{}{
  1188. "multi_match": map[string]interface{}{
  1189. "boost": boost, //给查询的值赋予权重
  1190. "fields": []interface{}{"Title"},
  1191. "query": keyWord,
  1192. },
  1193. },
  1194. },
  1195. })
  1196. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1197. "function_score": map[string]interface{}{
  1198. "query": map[string]interface{}{
  1199. "multi_match": map[string]interface{}{
  1200. "boost": boost, //给查询的值赋予权重
  1201. "fields": []interface{}{"Abstract"},
  1202. "query": keyWord,
  1203. },
  1204. },
  1205. },
  1206. })
  1207. shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1208. "function_score": map[string]interface{}{
  1209. "query": map[string]interface{}{
  1210. "multi_match": map[string]interface{}{
  1211. "boost": boost, //给查询的值赋予权重
  1212. "fields": []interface{}{"Annotation"},
  1213. "query": keyWord,
  1214. },
  1215. },
  1216. },
  1217. })
  1218. //shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
  1219. // "function_score": map[string]interface{}{
  1220. // "query": map[string]interface{}{
  1221. // "multi_match": map[string]interface{}{
  1222. // //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  1223. // "boost": boost, //给查询的值赋予权重
  1224. // "fields": []interface{}{"BodyText"},
  1225. // "query": keyWord,
  1226. // },
  1227. // },
  1228. // },
  1229. //})
  1230. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  1231. "function_score": map[string]interface{}{
  1232. "query": map[string]interface{}{
  1233. "multi_match": map[string]interface{}{
  1234. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  1235. "boost": boost, //给查询的值赋予权重
  1236. "fields": []interface{}{"BodyText"},
  1237. "query": keyWord,
  1238. },
  1239. },
  1240. },
  1241. })
  1242. }
  1243. shouldMap = map[string]interface{}{
  1244. "should": shouldMapquery,
  1245. }
  1246. shouldNotMap = map[string]interface{}{
  1247. "should": shouldNotMapquery,
  1248. }
  1249. //排序
  1250. sortMap := make([]interface{}, 0)
  1251. //时间
  1252. sortMap = append(sortMap, map[string]interface{}{
  1253. "PublishDate": map[string]interface{}{
  1254. "order": "desc",
  1255. },
  1256. })
  1257. //高亮
  1258. highlightMap := make(map[string]interface{}, 0)
  1259. highlightMap = map[string]interface{}{
  1260. "fields": map[string]interface{}{
  1261. "BodyText": map[string]interface{}{},
  1262. "Title": map[string]interface{}{},
  1263. "Abstract": map[string]interface{}{},
  1264. "Annotation": map[string]interface{}{},
  1265. },
  1266. //样式 红色
  1267. "post_tags": []interface{}{"</font>"},
  1268. "pre_tags": []interface{}{"<font color='red'>"},
  1269. }
  1270. mustMap = append(mustMap, map[string]interface{}{
  1271. "bool": shouldMap,
  1272. })
  1273. mustNotMap = append(mustNotMap, map[string]interface{}{
  1274. "bool": shouldNotMap,
  1275. })
  1276. queryMap := map[string]interface{}{
  1277. "query": map[string]interface{}{
  1278. "bool": map[string]interface{}{
  1279. "must": mustMap,
  1280. },
  1281. },
  1282. }
  1283. //把第一次的筛选条件过滤掉
  1284. if searchType == 2 {
  1285. queryMap = map[string]interface{}{
  1286. "query": map[string]interface{}{
  1287. "bool": map[string]interface{}{
  1288. "must": mustMap,
  1289. "must_not": mustNotMap,
  1290. },
  1291. },
  1292. }
  1293. }
  1294. if orderColumn == "Matching" {
  1295. queryMap["sort"] = sortMap
  1296. }
  1297. queryMap["from"] = startSize
  1298. queryMap["size"] = pageSize
  1299. queryMap["highlight"] = highlightMap
  1300. jsonBytes, _ := json.Marshal(queryMap)
  1301. fmt.Println(string(jsonBytes))
  1302. //utils.FileLog.Info(string(jsonBytes))
  1303. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1304. searchByMatch, err := request.Do(context.Background())
  1305. if searchByMatch != nil {
  1306. if searchByMatch.Hits != nil {
  1307. for _, v := range searchByMatch.Hits.Hits {
  1308. var isAppend bool
  1309. articleJson, err := v.Source.MarshalJSON()
  1310. if err != nil {
  1311. return nil, 0, err
  1312. }
  1313. article := new(models.CygxArticleEs)
  1314. err = json.Unmarshal(articleJson, &article)
  1315. if err != nil {
  1316. return nil, 0, err
  1317. }
  1318. searchItem := new(models.SearchItem)
  1319. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  1320. if len(v.Highlight["Annotation"]) > 0 {
  1321. for _, vText := range v.Highlight["Annotation"] {
  1322. searchItem.Body = append(searchItem.Body, vText)
  1323. }
  1324. }
  1325. if len(v.Highlight["Abstract"]) > 0 {
  1326. for _, vText := range v.Highlight["Abstract"] {
  1327. searchItem.Body = append(searchItem.Body, vText)
  1328. }
  1329. }
  1330. if len(v.Highlight["BodyText"]) > 0 {
  1331. for _, vText := range v.Highlight["BodyText"] {
  1332. searchItem.Body = append(searchItem.Body, vText)
  1333. }
  1334. }
  1335. if len(searchItem.Body) == 0 {
  1336. bodyRune := []rune(article.BodyText)
  1337. bodyRuneLen := len(bodyRune)
  1338. if bodyRuneLen > 100 {
  1339. bodyRuneLen = 100
  1340. }
  1341. body := string(bodyRune[:bodyRuneLen])
  1342. searchItem.Body = []string{body}
  1343. }
  1344. //if len(v.Highlight["BodyText"]) > 0 {
  1345. // searchItem.Body = v.Highlight["BodyText"]
  1346. //} else {
  1347. // bodyRune := []rune(article.BodyText)
  1348. // bodyRuneLen := len(bodyRune)
  1349. // if bodyRuneLen > 100 {
  1350. // bodyRuneLen = 100
  1351. // }
  1352. // body := string(bodyRune[:bodyRuneLen])
  1353. // searchItem.Body = []string{body}
  1354. //}
  1355. var title string
  1356. if len(v.Highlight["Title"]) > 0 {
  1357. title = v.Highlight["Title"][0]
  1358. } else {
  1359. title = article.Title
  1360. }
  1361. searchItem.Title = title
  1362. searchItem.PublishDate = article.PublishDate
  1363. searchItem.ExpertBackground = article.ExpertBackground
  1364. searchItem.CategoryId = article.CategoryId
  1365. for _, v_result := range result {
  1366. if v_result.ArticleId == searchItem.ArticleId {
  1367. isAppend = true
  1368. }
  1369. }
  1370. if !isAppend {
  1371. result = append(result, searchItem)
  1372. }
  1373. }
  1374. }
  1375. total = searchByMatch.Hits.TotalHits.Value
  1376. }
  1377. return
  1378. }