elastic.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. "log"
  10. "os"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. )
  15. func NewClient() (client *elastic.Client, err error) {
  16. errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
  17. file := ""
  18. if utils.RunMode == "release" {
  19. //file = `/data/rdlucklog/hongze_cygx/eslog.log`
  20. file = `./rdlucklog/eslog.log`
  21. } else {
  22. file = `./rdlucklog/eslog.log`
  23. }
  24. logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
  25. client, err = elastic.NewClient(
  26. elastic.SetURL(ES_URL),
  27. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  28. elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
  29. elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
  30. return
  31. }
  32. //indexName:索引名称
  33. //mappingJson:表结构
  34. func EsCreateIndex(indexName, mappingJson string) (err error) {
  35. client, err := NewClient()
  36. if err != nil {
  37. return
  38. }
  39. //定义表结构
  40. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  41. if err != nil {
  42. return
  43. }
  44. if !exists {
  45. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  46. //BodyJson(bodyJson).Do(context.Background())
  47. if err != nil {
  48. fmt.Println("CreateIndex Err:" + err.Error())
  49. return err
  50. }
  51. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  52. } else {
  53. fmt.Println(indexName + " 已存在")
  54. }
  55. return
  56. }
  57. //新增和修改数据
  58. func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (err error) {
  59. defer func() {
  60. if err != nil {
  61. fmt.Println("EsAddOrEditData Err:", err.Error())
  62. }
  63. }()
  64. client, err := NewClient()
  65. if err != nil {
  66. return
  67. }
  68. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  69. if err != nil && !strings.Contains(err.Error(), "404") {
  70. fmt.Println("Get Err" + err.Error())
  71. return
  72. }
  73. if searchById != nil && searchById.Found {
  74. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  75. "BodyText": item.BodyText,
  76. "Title": item.Title,
  77. "PublishDate": item.PublishDate,
  78. }).Do(context.Background())
  79. if err != nil {
  80. return err
  81. }
  82. fmt.Println(resp.Status, resp.Result)
  83. if resp.Status == 0 {
  84. fmt.Println("修改成功")
  85. } else {
  86. fmt.Println("EditData", resp.Status, resp.Result)
  87. }
  88. } else {
  89. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  90. if err != nil {
  91. fmt.Println("新增失败:", err.Error())
  92. return err
  93. }
  94. if resp.Status == 0 && resp.Result == "created" {
  95. fmt.Println("新增成功")
  96. err = nil
  97. } else {
  98. fmt.Println("AddData", resp.Status, resp.Result)
  99. }
  100. }
  101. return
  102. }
  103. //删除数据
  104. func EsDeleteData(indexName, docId string) (err error) {
  105. client, err := NewClient()
  106. if err != nil {
  107. return
  108. }
  109. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  110. if err != nil {
  111. return
  112. }
  113. if resp.Status == 0 {
  114. fmt.Println("删除成功")
  115. } else {
  116. fmt.Println("AddData", resp.Status, resp.Result)
  117. }
  118. return
  119. }
  120. func MappingModify(indexName, mappingJson string) {
  121. client, err := NewClient()
  122. if err != nil {
  123. return
  124. }
  125. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  126. fmt.Println(err)
  127. fmt.Println(result)
  128. return
  129. }
  130. func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
  131. client, err := NewClient()
  132. pageSize := 20
  133. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  134. fmt.Println(keyWordArr)
  135. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  136. fmt.Println("-------------------------------")
  137. fmt.Println(keyWordArr)
  138. searchMap := make(map[int]int)
  139. boolquery := elastic.NewBoolQuery()
  140. keyLen := len(keyWordArr)
  141. n := 5.0 * float64(keyLen)
  142. matchArr := make([]elastic.Query, 0)
  143. //
  144. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  145. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  146. //
  147. //matchArr = append(matchArr, matchq1)
  148. //matchArr = append(matchArr, matchq2)
  149. for _, v := range keyWordArr {
  150. if v != "" {
  151. matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
  152. matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
  153. matchArr = append(matchArr, matchq1)
  154. matchArr = append(matchArr, matchq2)
  155. }
  156. n = n - 5
  157. }
  158. boolquery.Should(matchArr...)
  159. highlight := elastic.NewHighlight()
  160. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  161. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  162. request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
  163. searchByMatch, err := request.Do(context.Background())
  164. if searchByMatch.Hits != nil {
  165. for _, v := range searchByMatch.Hits.Hits {
  166. articleJson, err := v.Source.MarshalJSON()
  167. if err != nil {
  168. return nil, err
  169. }
  170. article := new(models.CygxArticle)
  171. err = json.Unmarshal(articleJson, &article)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if _, ok := searchMap[article.ArticleId]; !ok {
  176. searchItem := new(models.SearchItem)
  177. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  178. if len(v.Highlight["BodyText"]) > 0 {
  179. searchItem.Body = v.Highlight["BodyText"]
  180. } else {
  181. bodyRune := []rune(article.BodyText)
  182. bodyRuneLen := len(bodyRune)
  183. if bodyRuneLen > 100 {
  184. bodyRuneLen = 100
  185. }
  186. body := string(bodyRune[:bodyRuneLen])
  187. fmt.Println(body)
  188. searchItem.Body = []string{body}
  189. }
  190. var title string
  191. if len(v.Highlight["Title"]) > 0 {
  192. title = v.Highlight["Title"][0]
  193. } else {
  194. title = article.Title
  195. }
  196. searchItem.Title = title
  197. searchItem.PublishDate = article.PublishDate
  198. result = append(result, searchItem)
  199. searchMap[article.ArticleId] = article.ArticleId
  200. }
  201. }
  202. }
  203. return
  204. }
  205. func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
  206. client, err := NewClient()
  207. pageSize := 20
  208. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  209. fmt.Println(keyWordArr)
  210. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  211. fmt.Println("-------------------------------")
  212. fmt.Println(keyWordArr)
  213. searchMap := make(map[int]int)
  214. boolquery := elastic.NewBoolQuery()
  215. //keyLen := len(keyWordArr)
  216. //n := float64(keyLen)
  217. matchArr := make([]elastic.Query, 0)
  218. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  219. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  220. matchq1 := elastic.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart")
  221. matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord)
  222. matchArr = append(matchArr, matchq1)
  223. matchArr = append(matchArr, matchq2)
  224. //matchArr = append(matchArr, matchq2)
  225. //for _, v := range keyWordArr {
  226. // if v != "" {
  227. // matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
  228. // matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
  229. // matchArr = append(matchArr, matchq1)
  230. // matchArr = append(matchArr, matchq2)
  231. // }
  232. // n--
  233. //}
  234. //boolquery.Should(matchArr...)
  235. boolquery.Should(matchArr...)
  236. highlight := elastic.NewHighlight()
  237. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  238. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  239. request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
  240. searchByMatch, err := request.Do(context.Background())
  241. fmt.Println("err:", err, searchByMatch)
  242. return
  243. if searchByMatch.Hits != nil {
  244. for _, v := range searchByMatch.Hits.Hits {
  245. articleJson, err := v.Source.MarshalJSON()
  246. if err != nil {
  247. return nil, err
  248. }
  249. article := new(models.CygxArticle)
  250. err = json.Unmarshal(articleJson, &article)
  251. if err != nil {
  252. return nil, err
  253. }
  254. if _, ok := searchMap[article.ArticleId]; !ok {
  255. searchItem := new(models.SearchItem)
  256. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  257. searchItem.Body = v.Highlight["BodyText"]
  258. var title string
  259. if len(v.Highlight["Title"]) > 0 {
  260. title = v.Highlight["Title"][0]
  261. } else {
  262. title = article.Title
  263. }
  264. searchItem.Title = title
  265. searchItem.PublishDate = article.PublishDate
  266. result = append(result, searchItem)
  267. searchMap[article.ArticleId] = article.ArticleId
  268. }
  269. }
  270. }
  271. return
  272. }
  273. func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
  274. client, err := NewClient()
  275. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  276. fmt.Println(keyWordArr)
  277. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  278. fmt.Println("-------------------------------")
  279. fmt.Println(keyWordArr)
  280. searchMap := make(map[int]int)
  281. boolquery := elastic.NewBoolQuery()
  282. matchArr := make([]elastic.Query, 0)
  283. //
  284. //matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  285. //matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  286. //
  287. //matchArr = append(matchArr, matchq1)
  288. //matchArr = append(matchArr, matchq2)
  289. n := 0
  290. keyWordLen := len(keyWordArr)
  291. if keyWordLen <= 0 {
  292. keyWordArr = append(keyWordArr, keyWord)
  293. keyWordLen = len(keyWordArr)
  294. }
  295. keyWordWeight := GetWeight(keyWordLen)
  296. fmt.Println(keyWordArr)
  297. fmt.Println(keyWordWeight)
  298. for k, v := range keyWordArr {
  299. if v != "" {
  300. weight := float64(keyWordWeight[k])
  301. titleMatchq := elastic.NewMatchQuery("Title", v).Analyzer("ik_smart")
  302. bodyMatchq := elastic.NewMatchQuery("BodyText", v).Analyzer("ik_smart")
  303. titleFunctionQuery := elastic.NewFunctionScoreQuery()
  304. titleFunctionQuery.Query(titleMatchq)
  305. titleFunctions := elastic.NewWeightFactorFunction(weight)
  306. titleFunctionQuery.AddScoreFunc(titleFunctions)
  307. titleFunctionQuery.BoostMode("replace")
  308. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  309. bodyFunctionQuery.Query(bodyMatchq)
  310. bodyFunctions := elastic.NewWeightFactorFunction(weight)
  311. bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  312. bodyFunctionQuery.BoostMode("replace")
  313. matchArr = append(matchArr, titleFunctionQuery)
  314. matchArr = append(matchArr, bodyFunctionQuery)
  315. }
  316. n++
  317. }
  318. boolquery.Should(matchArr...)
  319. highlight := elastic.NewHighlight()
  320. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  321. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  322. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  323. searchByMatch, err := request.Do(context.Background())
  324. //searchJson, err := json.Marshal(searchByMatch)
  325. if searchByMatch != nil {
  326. if searchByMatch.Hits != nil {
  327. for _, v := range searchByMatch.Hits.Hits {
  328. articleJson, err := v.Source.MarshalJSON()
  329. if err != nil {
  330. return nil, 0, err
  331. }
  332. article := new(models.CygxArticle)
  333. err = json.Unmarshal(articleJson, &article)
  334. if err != nil {
  335. return nil, 0, err
  336. }
  337. if _, ok := searchMap[article.ArticleId]; !ok {
  338. searchItem := new(models.SearchItem)
  339. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  340. if len(v.Highlight["BodyText"]) > 0 {
  341. searchItem.Body = v.Highlight["BodyText"]
  342. } else {
  343. bodyRune := []rune(article.BodyText)
  344. bodyRuneLen := len(bodyRune)
  345. if bodyRuneLen > 100 {
  346. bodyRuneLen = 100
  347. }
  348. body := string(bodyRune[:bodyRuneLen])
  349. fmt.Println(body)
  350. searchItem.Body = []string{body}
  351. }
  352. var title string
  353. if len(v.Highlight["Title"]) > 0 {
  354. title = v.Highlight["Title"][0]
  355. } else {
  356. title = article.Title
  357. }
  358. searchItem.Title = title
  359. searchItem.PublishDate = article.PublishDate
  360. result = append(result, searchItem)
  361. searchMap[article.ArticleId] = article.ArticleId
  362. }
  363. }
  364. }
  365. }
  366. total = searchByMatch.Hits.TotalHits.Value
  367. return
  368. }
  369. func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  370. client, err := NewClient()
  371. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  372. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  373. //artidArr := make([]elastic.Query, 0)
  374. //matchArr := make([]elastic.Query, 0)
  375. n := 0
  376. keyWordLen := len(keyWordArr)
  377. if keyWordLen <= 0 {
  378. keyWordArr = append(keyWordArr, keyWord)
  379. keyWordLen = len(keyWordArr)
  380. }
  381. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  382. //keyWordWeight := GetWeight(keyWordLen)
  383. for _, v := range keyWordArr {
  384. if v != "" {
  385. matchArr := make([]elastic.Query, 0)
  386. boolquery := elastic.NewBoolQuery()
  387. //weight := float64(keyWordWeight[k])
  388. //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  389. //bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  390. //bodyFunctionQuery.Query(multiMatch)
  391. //bodyFunctions := elastic.NewWeightFactorFunction(weight)
  392. //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  393. //bodyFunctionQuery.BoostMode("replace")
  394. //matchArr = append(matchArr, bodyFunctionQuery)
  395. //weight := float64(keyWordWeight[k])
  396. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  397. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  398. bodyFunctionQuery.Query(multiMatch)
  399. //bodyFunctions := elastic.NewWeightFactorFunction(weight)
  400. //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  401. //bodyFunctionQuery.BoostMode("replace")
  402. matchArr = append(matchArr, bodyFunctionQuery)
  403. boolquery.Should(matchArr...)
  404. highlight := elastic.NewHighlight()
  405. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  406. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  407. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  408. searchByMatch, err := request.Do(context.Background())
  409. if err != nil {
  410. return nil, 0, err
  411. }
  412. if searchByMatch != nil {
  413. if searchByMatch.Hits != nil {
  414. for _, v := range searchByMatch.Hits.Hits {
  415. var isAppend bool
  416. articleJson, err := v.Source.MarshalJSON()
  417. if err != nil {
  418. return nil, 0, err
  419. }
  420. article := new(models.CygxArticle)
  421. err = json.Unmarshal(articleJson, &article)
  422. if err != nil {
  423. return nil, 0, err
  424. }
  425. searchItem := new(models.SearchItem)
  426. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  427. if len(v.Highlight["BodyText"]) > 0 {
  428. searchItem.Body = v.Highlight["BodyText"]
  429. } else {
  430. bodyRune := []rune(article.BodyText)
  431. bodyRuneLen := len(bodyRune)
  432. if bodyRuneLen > 100 {
  433. bodyRuneLen = 100
  434. }
  435. body := string(bodyRune[:bodyRuneLen])
  436. searchItem.Body = []string{body}
  437. }
  438. var title string
  439. if len(v.Highlight["Title"]) > 0 {
  440. title = v.Highlight["Title"][0]
  441. } else {
  442. title = article.Title
  443. }
  444. searchItem.Title = title
  445. searchItem.PublishDate = article.PublishDate
  446. for _, v_result := range result {
  447. if v_result.ArticleId == searchItem.ArticleId {
  448. isAppend = true
  449. }
  450. }
  451. if !isAppend {
  452. result = append(result, searchItem)
  453. }
  454. }
  455. }
  456. //total += searchByMatch.Hits.TotalHits.Value
  457. }
  458. }
  459. n++
  460. }
  461. total = int64(len(result))
  462. //fmt.Println(result)
  463. //boolquery.Should(matchArr...)
  464. //highlight := elastic.NewHighlight()
  465. //highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  466. //highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  467. //request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  468. //searchByMatch, err := request.Do(context.Background())
  469. //if searchByMatch != nil {
  470. // if searchByMatch.Hits != nil {
  471. // for _, v := range searchByMatch.Hits.Hits {
  472. // articleJson, err := v.Source.MarshalJSON()
  473. // if err != nil {
  474. // return nil, 0, err
  475. // }
  476. // article := new(models.CygxArticle)
  477. // err = json.Unmarshal(articleJson, &article)
  478. // if err != nil {
  479. // return nil, 0, err
  480. // }
  481. // searchItem := new(models.SearchItem)
  482. // searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  483. // if len(v.Highlight["BodyText"]) > 0 {
  484. // searchItem.Body = v.Highlight["BodyText"]
  485. // } else {
  486. // bodyRune := []rune(article.BodyText)
  487. // bodyRuneLen := len(bodyRune)
  488. // if bodyRuneLen > 100 {
  489. // bodyRuneLen = 100
  490. // }
  491. // body := string(bodyRune[:bodyRuneLen])
  492. // searchItem.Body = []string{body}
  493. // }
  494. // var title string
  495. // if len(v.Highlight["Title"]) > 0 {
  496. // title = v.Highlight["Title"][0]
  497. // } else {
  498. // title = article.Title
  499. // }
  500. // searchItem.Title = title
  501. // searchItem.PublishDate = article.PublishDate
  502. //
  503. // result = append(result, searchItem)
  504. // }
  505. // }
  506. // total = searchByMatch.Hits.TotalHits.Value
  507. //}
  508. return
  509. }
  510. func EsMultiMatchFunctionScoreQueryFix(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
  511. client, err := NewClient()
  512. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  513. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  514. boolquery := elastic.NewBoolQuery()
  515. matchArr := make([]elastic.Query, 0)
  516. n := 0
  517. keyWordLen := len(keyWordArr)
  518. if keyWordLen <= 0 {
  519. keyWordArr = append(keyWordArr, keyWord)
  520. keyWordLen = len(keyWordArr)
  521. }
  522. fmt.Println(keyWordArr)
  523. keyWordWeight := GetWeight(keyWordLen)
  524. for k, v := range keyWordArr {
  525. if v != "" {
  526. weight := float64(keyWordWeight[k])
  527. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  528. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  529. bodyFunctionQuery.Query(multiMatch)
  530. bodyFunctions := elastic.NewWeightFactorFunction(weight)
  531. bodyFunctionQuery.AddScoreFunc(bodyFunctions)
  532. bodyFunctionQuery.BoostMode("replace")
  533. matchArr = append(matchArr, bodyFunctionQuery)
  534. }
  535. n++
  536. }
  537. boolquery.Should(matchArr...)
  538. highlight := elastic.NewHighlight()
  539. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  540. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  541. request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  542. searchByMatch, err := request.Do(context.Background())
  543. if searchByMatch != nil {
  544. matchResult, _ := json.Marshal(searchByMatch)
  545. utils.FileLog.Info("%s", string(matchResult))
  546. if searchByMatch.Hits != nil {
  547. for _, v := range searchByMatch.Hits.Hits {
  548. articleJson, err := v.Source.MarshalJSON()
  549. utils.FileLog.Info("%s", string(articleJson))
  550. if err != nil {
  551. return nil, 0, err
  552. }
  553. article := new(models.CygxArticle)
  554. err = json.Unmarshal(articleJson, &article)
  555. if err != nil {
  556. return nil, 0, err
  557. }
  558. searchItem := new(models.SearchItem)
  559. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  560. if len(v.Highlight["BodyText"]) > 0 {
  561. searchItem.Body = v.Highlight["BodyText"]
  562. } else {
  563. bodyRune := []rune(article.BodyText)
  564. bodyRuneLen := len(bodyRune)
  565. if bodyRuneLen > 100 {
  566. bodyRuneLen = 100
  567. }
  568. body := string(bodyRune[:bodyRuneLen])
  569. searchItem.Body = []string{body}
  570. }
  571. var title string
  572. if len(v.Highlight["Title"]) > 0 {
  573. title = v.Highlight["Title"][0]
  574. } else {
  575. title = article.Title
  576. }
  577. searchItem.Title = title
  578. searchItem.PublishDate = article.PublishDate
  579. result = append(result, searchItem)
  580. }
  581. }
  582. total = searchByMatch.Hits.TotalHits.Value
  583. }
  584. return
  585. }
  586. func GetWeight(length int) []int {
  587. steep := 10
  588. intArr := make([]int, 0)
  589. for i := 1; i <= length; i++ {
  590. if i == 1 {
  591. intArr = append(intArr, 1)
  592. } else {
  593. min := GetArrSum(intArr)
  594. maxVal := utils.GetRandInt(min, min+steep)
  595. intArr = append(intArr, maxVal)
  596. }
  597. }
  598. //数组排序
  599. sort.Slice(intArr, func(i, j int) bool {
  600. return intArr[i] > intArr[j]
  601. })
  602. return intArr
  603. }
  604. func GetArrSum(intArr []int) (sum int) {
  605. for _, val := range intArr {
  606. //累计求和
  607. sum += val
  608. }
  609. return
  610. }
  611. //func init() {
  612. // fmt.Println("start")
  613. // keyWord:="阿里巴巴"
  614. // keyWordArr, _ := GetIndustryMapNameSliceV2(keyWord)
  615. // keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  616. // fmt.Println(keyWordArr)
  617. // fmt.Println("end")
  618. //}
  619. func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
  620. client, err := NewClient()
  621. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  622. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  623. //artidArr := make([]elastic.Query, 0)
  624. //matchArr := make([]elastic.Query, 0)
  625. n := 0
  626. keyWordLen := len(keyWordArr)
  627. if keyWordLen <= 0 {
  628. keyWordArr = append(keyWordArr, keyWord)
  629. keyWordLen = len(keyWordArr)
  630. }
  631. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  632. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  633. //keyWordWeight := GetWeight(keyWordLen)
  634. for _, v := range keyWordArr {
  635. if v != "" {
  636. matchArr := make([]elastic.Query, 0)
  637. boolquery := elastic.NewBoolQuery()
  638. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  639. //if orderColumn == "PublishDate" {
  640. // multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  641. // bodyFunctionQuery.Query(multiMatch)
  642. //} else {
  643. // multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  644. // bodyFunctionQuery.Query(multiMatch)
  645. //}
  646. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
  647. bodyFunctionQuery.Query(multiMatch)
  648. matchArr = append(matchArr, bodyFunctionQuery)
  649. boolquery.Should(matchArr...)
  650. highlight := elastic.NewHighlight()
  651. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  652. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  653. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
  654. if orderColumn == "Matching" {
  655. request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
  656. }
  657. searchByMatch, err := request.Do(context.Background())
  658. if err != nil {
  659. return nil, 0, err
  660. }
  661. if searchByMatch != nil {
  662. if searchByMatch.Hits != nil {
  663. for _, v := range searchByMatch.Hits.Hits {
  664. var isAppend bool
  665. articleJson, err := v.Source.MarshalJSON()
  666. if err != nil {
  667. return nil, 0, err
  668. }
  669. article := new(models.CygxArticle)
  670. err = json.Unmarshal(articleJson, &article)
  671. if err != nil {
  672. return nil, 0, err
  673. }
  674. searchItem := new(models.SearchItem)
  675. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  676. if len(v.Highlight["BodyText"]) > 0 {
  677. searchItem.Body = v.Highlight["BodyText"]
  678. } else {
  679. bodyRune := []rune(article.BodyText)
  680. bodyRuneLen := len(bodyRune)
  681. if bodyRuneLen > 100 {
  682. bodyRuneLen = 100
  683. }
  684. body := string(bodyRune[:bodyRuneLen])
  685. searchItem.Body = []string{body}
  686. }
  687. var title string
  688. if len(v.Highlight["Title"]) > 0 {
  689. title = v.Highlight["Title"][0]
  690. } else {
  691. title = article.Title
  692. }
  693. searchItem.Title = title
  694. searchItem.PublishDate = article.PublishDate
  695. for _, v_result := range result {
  696. if v_result.ArticleId == searchItem.ArticleId {
  697. isAppend = true
  698. }
  699. }
  700. if !isAppend {
  701. result = append(result, searchItem)
  702. }
  703. }
  704. }
  705. //total += searchByMatch.Hits.TotalHits.Value
  706. }
  707. }
  708. n++
  709. }
  710. total = int64(len(result))
  711. return
  712. }
  713. func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
  714. client, err := NewClient()
  715. keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
  716. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  717. boolquery := elastic.NewBoolQuery()
  718. matchArr := make([]elastic.Query, 0)
  719. n := 0
  720. keyWordLen := len(keyWordArr)
  721. if keyWordLen <= 0 {
  722. keyWordArr = append(keyWordArr, keyWord)
  723. keyWordLen = len(keyWordArr)
  724. }
  725. fmt.Println(keyWordArr)
  726. utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
  727. for _, v := range keyWordArr {
  728. if v != "" {
  729. multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
  730. bodyFunctionQuery := elastic.NewFunctionScoreQuery()
  731. bodyFunctionQuery.Query(multiMatch)
  732. matchArr = append(matchArr, bodyFunctionQuery)
  733. }
  734. n++
  735. }
  736. boolquery.Should(matchArr...)
  737. highlight := elastic.NewHighlight()
  738. highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  739. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  740. request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).Query(boolquery)
  741. searchByMatch, err := request.Do(context.Background())
  742. if searchByMatch != nil {
  743. matchResult, _ := json.Marshal(searchByMatch)
  744. utils.FileLog.Info("%s", string(matchResult))
  745. fmt.Println(len(searchByMatch.Hits.Hits))
  746. if searchByMatch.Hits != nil {
  747. for _, v := range searchByMatch.Hits.Hits {
  748. articleJson, err := v.Source.MarshalJSON()
  749. utils.FileLog.Info("%s", string(articleJson))
  750. if err != nil {
  751. return nil, 0, err
  752. }
  753. article := new(models.CygxArticle)
  754. err = json.Unmarshal(articleJson, &article)
  755. if err != nil {
  756. return nil, 0, err
  757. }
  758. searchItem := new(models.SearchItem)
  759. searchItem.ArticleId, _ = strconv.Atoi(v.Id)
  760. if len(v.Highlight["BodyText"]) > 0 {
  761. searchItem.Body = v.Highlight["BodyText"]
  762. } else {
  763. bodyRune := []rune(article.BodyText)
  764. bodyRuneLen := len(bodyRune)
  765. if bodyRuneLen > 100 {
  766. bodyRuneLen = 100
  767. }
  768. body := string(bodyRune[:bodyRuneLen])
  769. searchItem.Body = []string{body}
  770. }
  771. var title string
  772. if len(v.Highlight["Title"]) > 0 {
  773. title = v.Highlight["Title"][0]
  774. } else {
  775. title = article.Title
  776. }
  777. searchItem.Title = title
  778. searchItem.PublishDate = article.PublishDate
  779. result = append(result, searchItem)
  780. }
  781. }
  782. total = searchByMatch.Hits.TotalHits.Value
  783. }
  784. return
  785. }