elastic.go 26 KB

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