elastic.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta_gn/eta_api/models"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/olivere/elastic/v7"
  11. "strconv"
  12. "strings"
  13. )
  14. func EsCreateIndex(indexName, mappingJson string) (err error) {
  15. client := utils.EsClient
  16. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  17. if err != nil {
  18. return
  19. }
  20. if !exists {
  21. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  22. if err != nil {
  23. fmt.Println("CreateIndex Err:" + err.Error())
  24. return err
  25. }
  26. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  27. } else {
  28. fmt.Println(indexName + " 已存在")
  29. }
  30. return
  31. }
  32. func EsDeleteData(indexName, docId string) (err error) {
  33. client := utils.EsClient
  34. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  35. fmt.Println(resp)
  36. if err != nil {
  37. return
  38. }
  39. if resp.Status == 0 {
  40. fmt.Println("删除成功")
  41. } else {
  42. fmt.Println("AddData", resp.Status, resp.Result)
  43. }
  44. return
  45. }
  46. func MappingModify(indexName, mappingJson string) {
  47. client := utils.EsClient
  48. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  49. fmt.Println(err)
  50. fmt.Println(result)
  51. return
  52. }
  53. func EsAddOrEditEdbInfoData(indexName, docId string, item *data_manage.EdbInfoList) (err error) {
  54. defer func() {
  55. if err != nil {
  56. fmt.Println("EsAddOrEditData Err:", err.Error())
  57. }
  58. }()
  59. client := utils.EsClient
  60. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  61. if err != nil {
  62. fmt.Println("新增失败:", err.Error())
  63. return err
  64. }
  65. fmt.Println(resp)
  66. if resp.Status == 0 {
  67. fmt.Println("新增成功", resp.Result)
  68. err = nil
  69. } else {
  70. fmt.Println("AddData", resp.Status, resp.Result)
  71. }
  72. return
  73. }
  74. func SearchEdbInfoData(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType int8, frequency string, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList []int, edbType int) (total int64, list []*data_manage.EdbInfoList, err error) {
  75. list = make([]*data_manage.EdbInfoList, 0)
  76. defer func() {
  77. if err != nil {
  78. fmt.Println("EsAddOrEditData Err:", err.Error())
  79. }
  80. }()
  81. highlight := elastic.NewHighlight()
  82. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  83. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  84. mustMap := make([]interface{}, 0)
  85. mustNotMap := make([]interface{}, 0)
  86. switch filterSource {
  87. case 2:
  88. mustMap = []interface{}{
  89. map[string]interface{}{
  90. "term": map[string]interface{}{
  91. "Frequency.keyword": "月度",
  92. },
  93. },
  94. }
  95. case 3:
  96. case 4:
  97. mustMap = []interface{}{
  98. map[string]interface{}{
  99. "term": map[string]interface{}{
  100. "EdbType": 1,
  101. },
  102. },
  103. }
  104. case 5:
  105. mustMap = []interface{}{
  106. map[string]interface{}{
  107. "term": map[string]interface{}{
  108. "Source": 6,
  109. },
  110. },
  111. }
  112. case 6:
  113. mustNotMap = []interface{}{
  114. map[string]interface{}{
  115. "match": map[string]interface{}{
  116. "Frequency.keyword": "年度",
  117. },
  118. },
  119. }
  120. }
  121. if source > 0 {
  122. mustMap = append(mustMap, map[string]interface{}{
  123. "term": map[string]interface{}{
  124. "Source": source,
  125. },
  126. })
  127. }
  128. if frequency != "" {
  129. mustMap = append(mustMap, map[string]interface{}{
  130. "term": map[string]interface{}{
  131. "Frequency.keyword": frequency,
  132. },
  133. })
  134. }
  135. if len(noPermissionEdbInfoIdList) > 0 {
  136. mustNotMap = append(mustNotMap, map[string]interface{}{
  137. "terms": map[string]interface{}{
  138. "EdbInfoId": noPermissionEdbInfoIdList,
  139. },
  140. })
  141. }
  142. if len(noPermissionEdbClassifyIdList) > 0 {
  143. mustNotMap = append(mustNotMap, map[string]interface{}{
  144. "terms": map[string]interface{}{
  145. "ClassifyId": noPermissionEdbClassifyIdList,
  146. },
  147. })
  148. }
  149. if edbInfoType >= 0 {
  150. mustMap = append(mustMap, map[string]interface{}{
  151. "term": map[string]interface{}{
  152. "EdbInfoType": edbInfoType,
  153. },
  154. })
  155. }
  156. if edbType > 0 {
  157. mustMap = append(mustMap, map[string]interface{}{
  158. "term": map[string]interface{}{
  159. "EdbType": edbType,
  160. },
  161. })
  162. }
  163. keywordNameKey := `EdbName`
  164. if !utils.ContainsChinese(keywordStr) {
  165. keywordNameKey = `EdbNameEn`
  166. }
  167. shouldMap := map[string]interface{}{
  168. "should": []interface{}{
  169. map[string]interface{}{
  170. "match": map[string]interface{}{
  171. "EdbCode": keywordStr,
  172. },
  173. },
  174. map[string]interface{}{
  175. "match": map[string]interface{}{
  176. keywordNameKey: keywordStr,
  177. },
  178. },
  179. },
  180. }
  181. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  182. }
  183. func SearchEdbInfoDataBak(indexName, keywordStr string, from, size, filterSource, source int, frequency string) (total int64, list []*data_manage.EdbInfoList, err error) {
  184. list = make([]*data_manage.EdbInfoList, 0)
  185. defer func() {
  186. if err != nil {
  187. fmt.Println("EsAddOrEditData Err:", err.Error())
  188. }
  189. for _, v := range list {
  190. v.ConvertToResp()
  191. }
  192. }()
  193. client := utils.EsClient
  194. highlight := elastic.NewHighlight()
  195. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  196. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  197. mustMap := make([]interface{}, 0)
  198. mustNotMap := make([]interface{}, 0)
  199. switch filterSource {
  200. case 2:
  201. mustMap = []interface{}{
  202. map[string]interface{}{
  203. "term": map[string]interface{}{
  204. "Frequency.keyword": "月度",
  205. },
  206. },
  207. }
  208. case 3:
  209. case 4:
  210. mustMap = []interface{}{
  211. map[string]interface{}{
  212. "term": map[string]interface{}{
  213. "EdbType": 1,
  214. },
  215. },
  216. }
  217. case 5:
  218. mustMap = []interface{}{
  219. map[string]interface{}{
  220. "term": map[string]interface{}{
  221. "Source": 6,
  222. },
  223. },
  224. }
  225. case 6:
  226. mustNotMap = []interface{}{
  227. map[string]interface{}{
  228. "match": map[string]interface{}{
  229. "Frequency.keyword": "年度",
  230. },
  231. },
  232. }
  233. }
  234. if source > 0 {
  235. mustMap = append(mustMap, map[string]interface{}{
  236. "term": map[string]interface{}{
  237. "Source": source,
  238. },
  239. })
  240. }
  241. if frequency != "" {
  242. mustMap = append(mustMap, map[string]interface{}{
  243. "term": map[string]interface{}{
  244. "Frequency.keyword": frequency,
  245. },
  246. })
  247. }
  248. shouldMap := map[string]interface{}{
  249. "should": []interface{}{
  250. map[string]interface{}{
  251. "match": map[string]interface{}{
  252. "EdbCode": keywordStr,
  253. },
  254. },
  255. map[string]interface{}{
  256. "match": map[string]interface{}{
  257. "EdbName": keywordStr,
  258. },
  259. },
  260. map[string]interface{}{
  261. "match": map[string]interface{}{
  262. "EdbCode": map[string]interface{}{
  263. "query": keywordStr,
  264. "operator": "and",
  265. },
  266. },
  267. },
  268. map[string]interface{}{
  269. "match": map[string]interface{}{
  270. "EdbName": map[string]interface{}{
  271. "query": keywordStr,
  272. "operator": "and",
  273. },
  274. },
  275. },
  276. },
  277. }
  278. mustMap = append(mustMap, map[string]interface{}{
  279. "bool": shouldMap,
  280. })
  281. queryMap := map[string]interface{}{
  282. "query": map[string]interface{}{
  283. "bool": map[string]interface{}{
  284. "must": mustMap,
  285. "must_not": mustNotMap,
  286. },
  287. },
  288. }
  289. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  290. total, err = requestTotalHits.Do(context.Background())
  291. if err != nil {
  292. return
  293. }
  294. queryMap["from"] = from
  295. queryMap["size"] = size
  296. jsonBytes, _ := json.Marshal(queryMap)
  297. fmt.Println(string(jsonBytes))
  298. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  299. searchMap := make(map[string]string)
  300. searchResp, err := request.Do(context.Background())
  301. if err != nil {
  302. return
  303. }
  304. fmt.Println(searchResp)
  305. fmt.Println(searchResp.Status)
  306. if searchResp.Status != 0 {
  307. return
  308. }
  309. if searchResp.Hits != nil {
  310. for _, v := range searchResp.Hits.Hits {
  311. if _, ok := searchMap[v.Id]; !ok {
  312. itemJson, tmpErr := v.Source.MarshalJSON()
  313. if tmpErr != nil {
  314. err = tmpErr
  315. fmt.Println("movieJson err:", err)
  316. return
  317. }
  318. edbInfoItem := new(data_manage.EdbInfoList)
  319. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  320. if tmpErr != nil {
  321. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  322. err = tmpErr
  323. return
  324. }
  325. if len(v.Highlight["EdbCode"]) > 0 {
  326. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  327. }
  328. if len(v.Highlight["EdbName"]) > 0 {
  329. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  330. }
  331. list = append(list, edbInfoItem)
  332. searchMap[v.Id] = v.Id
  333. }
  334. }
  335. }
  336. return
  337. }
  338. func SearchAddPredictEdbInfoData(indexName, keywordStr string, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList []int, from, size, edbType int) (total int64, list []*data_manage.EdbInfoList, err error) {
  339. list = make([]*data_manage.EdbInfoList, 0)
  340. defer func() {
  341. if err != nil {
  342. fmt.Println("EsAddOrEditData Err:", err.Error())
  343. }
  344. for _, v := range list {
  345. v.ConvertToResp()
  346. }
  347. }()
  348. highlight := elastic.NewHighlight()
  349. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  350. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  351. mustMap := make([]interface{}, 0)
  352. mustNotMap := make([]interface{}, 0)
  353. mustNotMap = []interface{}{
  354. }
  355. mustMap = append(mustMap, map[string]interface{}{
  356. "term": map[string]interface{}{
  357. "EdbInfoType": 0,
  358. },
  359. })
  360. mustMap = append(mustMap, map[string]interface{}{
  361. "terms": map[string]interface{}{
  362. "Frequency.keyword": []string{"日度", "周度", "月度"},
  363. },
  364. })
  365. if edbType > 0 {
  366. mustMap = append(mustMap, map[string]interface{}{
  367. "term": map[string]interface{}{
  368. "EdbType": edbType,
  369. },
  370. })
  371. }
  372. keywordNameKey := `EdbName`
  373. if !utils.ContainsChinese(keywordStr) {
  374. keywordNameKey = `EdbNameEn`
  375. }
  376. shouldMap := map[string]interface{}{
  377. "should": []interface{}{
  378. map[string]interface{}{
  379. "match": map[string]interface{}{
  380. "EdbCode": keywordStr,
  381. },
  382. },
  383. map[string]interface{}{
  384. "match": map[string]interface{}{
  385. keywordNameKey: keywordStr,
  386. },
  387. },
  388. },
  389. }
  390. if len(noPermissionEdbInfoIdList) > 0 {
  391. mustNotMap = append(mustNotMap, map[string]interface{}{
  392. "terms": map[string]interface{}{
  393. "EdbInfoId": noPermissionEdbInfoIdList,
  394. },
  395. })
  396. }
  397. if len(noPermissionEdbClassifyIdList) > 0 {
  398. mustNotMap = append(mustNotMap, map[string]interface{}{
  399. "terms": map[string]interface{}{
  400. "ClassifyId": noPermissionEdbClassifyIdList,
  401. },
  402. })
  403. }
  404. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  405. }
  406. func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMap map[string]interface{}, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  407. list = make([]*data_manage.EdbInfoList, 0)
  408. defer func() {
  409. if err != nil {
  410. fmt.Println("EsAddOrEditData Err:", err.Error())
  411. }
  412. }()
  413. client := utils.EsClient
  414. highlight := elastic.NewHighlight()
  415. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  416. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  417. mustMap = append(mustMap, map[string]interface{}{
  418. "bool": shouldMap,
  419. })
  420. queryMap := map[string]interface{}{
  421. "query": map[string]interface{}{
  422. "bool": map[string]interface{}{
  423. "must": mustMap,
  424. "must_not": mustNotMap,
  425. },
  426. },
  427. }
  428. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  429. total, err = requestTotalHits.Do(context.Background())
  430. if err != nil {
  431. return
  432. }
  433. queryMap["from"] = from
  434. queryMap["size"] = size
  435. jsonBytes, _ := json.Marshal(queryMap)
  436. fmt.Println(string(jsonBytes))
  437. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  438. searchMap := make(map[string]string)
  439. searchResp, err := request.Do(context.Background())
  440. if err != nil {
  441. return
  442. }
  443. fmt.Println(searchResp)
  444. fmt.Println(searchResp.Status)
  445. if searchResp.Status != 0 {
  446. return
  447. }
  448. if searchResp.Hits != nil {
  449. for _, v := range searchResp.Hits.Hits {
  450. if _, ok := searchMap[v.Id]; !ok {
  451. itemJson, tmpErr := v.Source.MarshalJSON()
  452. if tmpErr != nil {
  453. err = tmpErr
  454. fmt.Println("movieJson err:", err)
  455. return
  456. }
  457. edbInfoItem := new(data_manage.EdbInfoList)
  458. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  459. if tmpErr != nil {
  460. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  461. err = tmpErr
  462. return
  463. }
  464. if len(v.Highlight["EdbCode"]) > 0 {
  465. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  466. }
  467. if len(v.Highlight["EdbName"]) > 0 {
  468. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  469. }
  470. list = append(list, edbInfoItem)
  471. searchMap[v.Id] = v.Id
  472. }
  473. }
  474. }
  475. return
  476. }
  477. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  478. defer func() {
  479. if err != nil {
  480. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  481. }
  482. }()
  483. client := utils.EsClient
  484. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  485. fmt.Println(resp)
  486. if err != nil {
  487. return
  488. }
  489. if resp.Status == 0 {
  490. fmt.Println("删除成功")
  491. } else {
  492. fmt.Println("AddData", resp.Status, resp.Result)
  493. }
  494. return
  495. }
  496. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  497. defer func() {
  498. if err != nil {
  499. fmt.Println("EsAddOrEditReport Err:", err.Error())
  500. }
  501. }()
  502. client := utils.EsClient
  503. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  504. if err != nil && !strings.Contains(err.Error(), "404") {
  505. fmt.Println("Get Err" + err.Error())
  506. return
  507. }
  508. if searchById != nil && searchById.Found {
  509. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  510. "ReportId": item.ReportId,
  511. "ReportChapterId": item.ReportChapterId,
  512. "Title": item.Title,
  513. "Abstract": item.Abstract,
  514. "BodyContent": item.BodyContent,
  515. "PublishTime": item.PublishTime,
  516. "PublishState": item.PublishState,
  517. "Author": item.Author,
  518. "ClassifyIdFirst": item.ClassifyIdFirst,
  519. "ClassifyNameFirst": item.ClassifyNameFirst,
  520. "ClassifyIdSecond": item.ClassifyIdSecond,
  521. "ClassifyNameSecond": item.ClassifyNameSecond,
  522. "Categories": item.Categories,
  523. "StageStr": item.StageStr,
  524. }).Do(context.Background())
  525. if err != nil {
  526. return err
  527. }
  528. if resp.Status == 0 {
  529. fmt.Println("修改成功" + docId)
  530. err = nil
  531. } else {
  532. fmt.Println("EditData", resp.Status, resp.Result)
  533. }
  534. } else {
  535. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  536. if err != nil {
  537. fmt.Println("新增失败:", err.Error())
  538. return err
  539. }
  540. if resp.Status == 0 && resp.Result == "created" {
  541. fmt.Println("新增成功" + docId)
  542. return nil
  543. } else {
  544. fmt.Println("AddData", resp.Status, resp.Result)
  545. }
  546. }
  547. return
  548. }
  549. type AnalyzeResp struct {
  550. Tokens []struct {
  551. EndOffset int64 `json:"end_offset"`
  552. Position int64 `json:"position"`
  553. StartOffset int64 `json:"start_offset"`
  554. Token string `json:"token"`
  555. Type string `json:"type"`
  556. } `json:"tokens"`
  557. }
  558. func Analyze(content string) (contentList []string, err error) {
  559. defer func() {
  560. if err != nil {
  561. fmt.Println("Analyze Err:", err.Error())
  562. }
  563. }()
  564. client := utils.EsClient
  565. queryMap := map[string]string{
  566. "text": content,
  567. "analyzer": "ik_max_word",
  568. }
  569. res, err := client.PerformRequest(
  570. context.Background(),
  571. elastic.PerformRequestOptions{
  572. Method: "GET",
  573. Path: "/_analyze",
  574. Body: queryMap,
  575. Stream: false,
  576. },
  577. )
  578. if res.StatusCode == 200 {
  579. var analyzeResp AnalyzeResp
  580. tmpErr := json.Unmarshal(res.Body, &analyzeResp)
  581. if tmpErr != nil {
  582. err = errors.New("返回数据转结构体失败:" + tmpErr.Error())
  583. return
  584. }
  585. for _, v := range analyzeResp.Tokens {
  586. contentList = append(contentList, v.Token)
  587. }
  588. } else {
  589. err = errors.New("分词失败,返回code异常:" + strconv.Itoa(res.StatusCode))
  590. }
  591. return
  592. }
  593. func EsAddOrEditChartInfoData(indexName, docId string, item *data_manage.ChartInfo) (err error) {
  594. defer func() {
  595. if err != nil {
  596. fmt.Println("EsAddOrEditData Err:", err.Error())
  597. }
  598. }()
  599. client := utils.EsClient
  600. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  601. if err != nil {
  602. fmt.Println("新增失败:", err.Error())
  603. return err
  604. }
  605. fmt.Println(resp)
  606. if resp.Status == 0 {
  607. fmt.Println("新增成功", resp.Result)
  608. err = nil
  609. } else {
  610. fmt.Println("AddData", resp.Status, resp.Result)
  611. }
  612. return
  613. }
  614. func EsDeleteDataV2(indexName, docId string) (err error) {
  615. defer func() {
  616. if err != nil {
  617. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  618. }
  619. }()
  620. client := utils.EsClient
  621. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  622. fmt.Println(resp)
  623. if err != nil {
  624. return
  625. }
  626. if resp.Status == 0 {
  627. fmt.Println("删除成功")
  628. } else {
  629. fmt.Println("AddData", resp.Status, resp.Result)
  630. }
  631. return
  632. }
  633. func SearchChartInfoData(indexName, keywordStr string, showSysId int, sourceList []int, noPermissionChartIdList []int, from, size int) (list []*data_manage.ChartInfo, total int64, err error) {
  634. list = make([]*data_manage.ChartInfo, 0)
  635. defer func() {
  636. if err != nil {
  637. fmt.Println("EsAddOrEditData Err:", err.Error())
  638. }
  639. }()
  640. client := utils.EsClient
  641. highlight := elastic.NewHighlight()
  642. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  643. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  644. mustMap := make([]interface{}, 0)
  645. mustNotMap := make([]interface{}, 0)
  646. if showSysId > 0 {
  647. mustMap = append(mustMap, map[string]interface{}{
  648. "term": map[string]interface{}{
  649. "SysUserId": showSysId,
  650. },
  651. })
  652. }
  653. mustMap = append(mustMap, map[string]interface{}{
  654. "terms": map[string]interface{}{
  655. "Source": sourceList,
  656. },
  657. })
  658. keywordNameKey := `ChartName`
  659. if !utils.ContainsChinese(keywordStr) {
  660. keywordNameKey = `ChartNameEn`
  661. }
  662. shouldMap := map[string]interface{}{
  663. "should": []interface{}{
  664. map[string]interface{}{
  665. "match": map[string]interface{}{
  666. keywordNameKey: keywordStr,
  667. },
  668. },
  669. map[string]interface{}{
  670. "match": map[string]interface{}{
  671. keywordNameKey: map[string]interface{}{
  672. "query": keywordStr,
  673. "operator": "and",
  674. },
  675. },
  676. },
  677. },
  678. }
  679. mustMap = append(mustMap, map[string]interface{}{
  680. "bool": shouldMap,
  681. })
  682. if len(noPermissionChartIdList) > 0 {
  683. mustNotMap = append(mustNotMap, map[string]interface{}{
  684. "terms": map[string]interface{}{
  685. "ChartInfoId": noPermissionChartIdList,
  686. },
  687. })
  688. }
  689. queryMap := map[string]interface{}{
  690. "query": map[string]interface{}{
  691. "bool": map[string]interface{}{
  692. "must": mustMap,
  693. "must_not": mustNotMap,
  694. },
  695. },
  696. }
  697. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  698. total, err = requestTotalHits.Do(context.Background())
  699. if err != nil {
  700. return
  701. }
  702. queryMap["from"] = from
  703. queryMap["size"] = size
  704. jsonBytes, _ := json.Marshal(queryMap)
  705. fmt.Println(string(jsonBytes))
  706. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  707. searchMap := make(map[string]string)
  708. searchResp, err := request.Do(context.Background())
  709. if err != nil {
  710. return
  711. }
  712. fmt.Println(searchResp)
  713. fmt.Println(searchResp.Status)
  714. if searchResp.Status != 0 {
  715. return
  716. }
  717. if searchResp.Hits != nil {
  718. for _, v := range searchResp.Hits.Hits {
  719. if _, ok := searchMap[v.Id]; !ok {
  720. itemJson, tmpErr := v.Source.MarshalJSON()
  721. if tmpErr != nil {
  722. err = tmpErr
  723. fmt.Println("movieJson err:", err)
  724. return
  725. }
  726. chartInfoItem := new(data_manage.ChartInfo)
  727. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  728. if err != nil {
  729. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  730. err = tmpErr
  731. return
  732. }
  733. if len(v.Highlight["ChartName"]) > 0 {
  734. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  735. }
  736. list = append(list, chartInfoItem)
  737. searchMap[v.Id] = v.Id
  738. }
  739. }
  740. }
  741. return
  742. }
  743. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  744. defer func() {
  745. if err != nil {
  746. fmt.Println("EsAddOrEditData Err:", err.Error())
  747. }
  748. }()
  749. client := utils.EsClient
  750. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  751. if err != nil {
  752. fmt.Println("新增失败:", err.Error())
  753. return err
  754. }
  755. fmt.Println(resp)
  756. if resp.Status == 0 {
  757. fmt.Println("新增成功", resp.Result)
  758. err = nil
  759. } else {
  760. fmt.Println("AddData", resp.Status, resp.Result)
  761. }
  762. return
  763. }
  764. func SearchMyChartInfoData(indexName, keywordStr string, adminId int, noPermissionChartIdList []int, from, size int) (list []*data_manage.MyChartList, total int64, err error) {
  765. list = make([]*data_manage.MyChartList, 0)
  766. defer func() {
  767. if err != nil {
  768. fmt.Println("EsAddOrEditData Err:", err.Error())
  769. }
  770. }()
  771. client := utils.EsClient
  772. highlight := elastic.NewHighlight()
  773. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  774. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  775. mustMap := make([]interface{}, 0)
  776. mustNotMap := make([]interface{}, 0)
  777. if adminId > 0 {
  778. mustMap = append(mustMap, map[string]interface{}{
  779. "term": map[string]interface{}{
  780. "AdminId": adminId,
  781. },
  782. })
  783. }
  784. keywordNameKey := `ChartName`
  785. if !utils.ContainsChinese(keywordStr) {
  786. keywordNameKey = `ChartNameEn`
  787. }
  788. shouldMap := map[string]interface{}{
  789. "should": []interface{}{
  790. map[string]interface{}{
  791. "match": map[string]interface{}{
  792. keywordNameKey: keywordStr,
  793. },
  794. },
  795. map[string]interface{}{
  796. "match": map[string]interface{}{
  797. keywordNameKey: map[string]interface{}{
  798. "query": keywordStr,
  799. "operator": "and",
  800. },
  801. },
  802. },
  803. },
  804. }
  805. mustMap = append(mustMap, map[string]interface{}{
  806. "bool": shouldMap,
  807. })
  808. if len(noPermissionChartIdList) > 0 {
  809. mustNotMap = append(mustNotMap, map[string]interface{}{
  810. "terms": map[string]interface{}{
  811. "ChartInfoId": noPermissionChartIdList,
  812. },
  813. })
  814. }
  815. queryMap := map[string]interface{}{
  816. "query": map[string]interface{}{
  817. "bool": map[string]interface{}{
  818. "must": mustMap,
  819. "must_not": mustNotMap,
  820. },
  821. },
  822. }
  823. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  824. total, err = requestTotalHits.Do(context.Background())
  825. if err != nil {
  826. return
  827. }
  828. queryMap["from"] = from
  829. queryMap["size"] = size
  830. jsonBytes, _ := json.Marshal(queryMap)
  831. fmt.Println(string(jsonBytes))
  832. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  833. searchMap := make(map[string]string)
  834. searchResp, err := request.Do(context.Background())
  835. if err != nil {
  836. return
  837. }
  838. fmt.Println(searchResp)
  839. fmt.Println(searchResp.Status)
  840. if searchResp.Status != 0 {
  841. return
  842. }
  843. if searchResp.Hits != nil {
  844. for _, v := range searchResp.Hits.Hits {
  845. if _, ok := searchMap[v.Id]; !ok {
  846. itemJson, tmpErr := v.Source.MarshalJSON()
  847. if tmpErr != nil {
  848. err = tmpErr
  849. fmt.Println("movieJson err:", err)
  850. return
  851. }
  852. chartInfoItem := new(data_manage.MyChartList)
  853. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  854. if err != nil {
  855. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  856. err = tmpErr
  857. return
  858. }
  859. if len(v.Highlight["ChartName"]) > 0 {
  860. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  861. }
  862. list = append(list, chartInfoItem)
  863. searchMap[v.Id] = v.Id
  864. }
  865. }
  866. }
  867. return
  868. }
  869. func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType uint8, frequency string, adminId int) (total int64, list []*data_manage.EdbInfoList, err error) {
  870. list = make([]*data_manage.EdbInfoList, 0)
  871. defer func() {
  872. if err != nil {
  873. fmt.Println("EsAddOrEditData Err:", err.Error())
  874. }
  875. for _, v := range list {
  876. v.ConvertToResp()
  877. }
  878. }()
  879. highlight := elastic.NewHighlight()
  880. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  881. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  882. mustMap := make([]interface{}, 0)
  883. mustNotMap := make([]interface{}, 0)
  884. switch filterSource {
  885. case 2:
  886. mustMap = []interface{}{
  887. map[string]interface{}{
  888. "term": map[string]interface{}{
  889. "Frequency.keyword": "月度",
  890. },
  891. },
  892. }
  893. case 3:
  894. case 4:
  895. mustMap = []interface{}{
  896. map[string]interface{}{
  897. "term": map[string]interface{}{
  898. "EdbType": 1,
  899. },
  900. },
  901. }
  902. case 5:
  903. mustMap = []interface{}{
  904. map[string]interface{}{
  905. "term": map[string]interface{}{
  906. "Source": 6,
  907. },
  908. },
  909. }
  910. case 6:
  911. mustNotMap = []interface{}{
  912. map[string]interface{}{
  913. "match": map[string]interface{}{
  914. "Frequency.keyword": "年度",
  915. },
  916. },
  917. }
  918. }
  919. if source > 0 {
  920. mustMap = append(mustMap, map[string]interface{}{
  921. "term": map[string]interface{}{
  922. "Source": source,
  923. },
  924. })
  925. }
  926. if frequency != "" {
  927. mustMap = append(mustMap, map[string]interface{}{
  928. "term": map[string]interface{}{
  929. "Frequency.keyword": frequency,
  930. },
  931. })
  932. }
  933. mustMap = append(mustMap, map[string]interface{}{
  934. "term": map[string]interface{}{
  935. "EdbInfoType": edbInfoType,
  936. },
  937. })
  938. keywordNameKey := `EdbName`
  939. if !utils.ContainsChinese(keywordStr) {
  940. keywordNameKey = `EdbNameEn`
  941. }
  942. shouldMap := map[string]interface{}{
  943. "should": []interface{}{
  944. map[string]interface{}{
  945. "match": map[string]interface{}{
  946. "EdbCode": keywordStr,
  947. },
  948. },
  949. map[string]interface{}{
  950. "match": map[string]interface{}{
  951. keywordNameKey: keywordStr,
  952. },
  953. },
  954. },
  955. }
  956. mustMap = append(mustMap, map[string]interface{}{
  957. "bool": shouldMap,
  958. })
  959. if adminId > 0 {
  960. mustMap = append(mustMap, map[string]interface{}{
  961. "term": map[string]interface{}{
  962. "SysUserId": adminId,
  963. },
  964. })
  965. }
  966. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  967. }