elastic.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_forum_admin/models"
  7. "eta/eta_forum_admin/utils"
  8. "fmt"
  9. "github.com/olivere/elastic/v7"
  10. "strconv"
  11. )
  12. // indexName:索引名称
  13. // mappingJson:表结构
  14. func EsCreateIndex(indexName, mappingJson string) (err error) {
  15. client := utils.EsClient
  16. //定义表结构
  17. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  18. if err != nil {
  19. return
  20. }
  21. if !exists {
  22. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  23. //BodyJson(bodyJson).Do(context.Background())
  24. if err != nil {
  25. fmt.Println("CreateIndex Err:" + err.Error())
  26. return err
  27. }
  28. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  29. } else {
  30. fmt.Println(indexName + " 已存在")
  31. }
  32. return
  33. }
  34. // 删除数据
  35. func EsDeleteData(indexName, docId string) (err error) {
  36. client := utils.EsClient
  37. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  38. fmt.Println(resp)
  39. if err != nil {
  40. return
  41. }
  42. if resp.Status == 0 {
  43. fmt.Println("删除成功")
  44. } else {
  45. fmt.Println("AddData", resp.Status, resp.Result)
  46. }
  47. return
  48. }
  49. func MappingModify(indexName, mappingJson string) {
  50. client := utils.EsClient
  51. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  52. fmt.Println(err)
  53. fmt.Println(result)
  54. return
  55. }
  56. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  57. func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
  58. defer func() {
  59. if err != nil {
  60. fmt.Println("EsAddOrEditData Err:", err.Error())
  61. }
  62. }()
  63. client := utils.EsClient
  64. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  65. if err != nil {
  66. fmt.Println("新增失败:", err.Error())
  67. return err
  68. }
  69. fmt.Println(resp)
  70. if resp.Status == 0 {
  71. fmt.Println("新增成功", resp.Result)
  72. err = nil
  73. } else {
  74. fmt.Println("AddData", resp.Status, resp.Result)
  75. }
  76. return
  77. }
  78. // SearchEdbInfoData 查询es中的指标数据
  79. func SearchEdbInfoData(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType int8, frequency string, noPermissionEdbInfoIdList []int) (total int64, list []*models.EdbInfoList, err error) {
  80. list = make([]*models.EdbInfoList, 0)
  81. defer func() {
  82. if err != nil {
  83. fmt.Println("EsAddOrEditData Err:", err.Error())
  84. }
  85. }()
  86. highlight := elastic.NewHighlight()
  87. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  88. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  89. //var source map[string]interface{}
  90. //source := map[string]interface{}{
  91. // "query": map[string]interface{}{
  92. // "match_all": map[string]interface{}{},
  93. // },
  94. //}
  95. mustMap := make([]interface{}, 0)
  96. mustNotMap := make([]interface{}, 0)
  97. //source := map[string]interface{}{
  98. // "query": map[string]interface{}{
  99. // "bool": map[string]interface{}{
  100. // "must": map[string]interface{}{
  101. // "query_string": map[string]interface{}{
  102. // "query": keywordStr,
  103. // "fields": []string{"EdbCode", "EdbName"},
  104. // },
  105. // },
  106. // },
  107. // },
  108. //}
  109. switch filterSource {
  110. case 2:
  111. //source = map[string]interface{}{
  112. // "query": map[string]interface{}{
  113. // "bool": map[string]interface{}{
  114. // "must": map[string]interface{}{
  115. // "query_string": map[string]interface{}{
  116. // "query": keywordStr,
  117. // },
  118. // },
  119. // "filter": []interface{}{
  120. // map[string]interface{}{
  121. // "term": map[string]interface{}{
  122. // "Frequency.keyword": "月度",
  123. // },
  124. // }},
  125. // },
  126. // },
  127. //}
  128. mustMap = []interface{}{
  129. map[string]interface{}{
  130. "term": map[string]interface{}{
  131. "Frequency.keyword": "月度",
  132. //"Frequency.keyword": "月度",
  133. },
  134. },
  135. }
  136. case 3:
  137. //source = map[string]interface{}{
  138. // "query": map[string]interface{}{
  139. // "bool": map[string]interface{}{
  140. // "must": map[string]interface{}{
  141. // "query_string": map[string]interface{}{
  142. // "query": keywordStr,
  143. // },
  144. // },
  145. // "must_not": []interface{}{
  146. // map[string]interface{}{
  147. // "match": map[string]interface{}{
  148. // "Frequency.keyword": "日度",
  149. // },
  150. // }},
  151. // },
  152. // },
  153. //}
  154. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  155. //mustNotMap = []interface{}{
  156. // map[string]interface{}{
  157. // "match": map[string]interface{}{
  158. // "Frequency.keyword": "日度",
  159. // //"Frequency.keyword": "月度",
  160. // },
  161. // },
  162. //}
  163. case 4:
  164. //source = map[string]interface{}{
  165. // "query": map[string]interface{}{
  166. // "bool": map[string]interface{}{
  167. // "must": map[string]interface{}{
  168. // "query_string": map[string]interface{}{
  169. // "query": keywordStr,
  170. // },
  171. // },
  172. // "filter": []interface{}{
  173. // map[string]interface{}{
  174. // "term": map[string]interface{}{
  175. // "EdbType": 1,
  176. // },
  177. // }},
  178. // },
  179. // },
  180. //}
  181. mustMap = []interface{}{
  182. map[string]interface{}{
  183. "term": map[string]interface{}{
  184. "EdbType": 1,
  185. },
  186. },
  187. }
  188. case 5:
  189. mustMap = []interface{}{
  190. map[string]interface{}{
  191. "term": map[string]interface{}{
  192. "Source": 6,
  193. },
  194. },
  195. }
  196. case 6:
  197. mustNotMap = []interface{}{
  198. map[string]interface{}{
  199. "match": map[string]interface{}{
  200. "Frequency.keyword": "年度",
  201. },
  202. },
  203. }
  204. }
  205. //指标来源
  206. if source > 0 {
  207. mustMap = append(mustMap, map[string]interface{}{
  208. "term": map[string]interface{}{
  209. "Source": source,
  210. //"Frequency.keyword": "月度",
  211. },
  212. })
  213. }
  214. if frequency != "" {
  215. mustMap = append(mustMap, map[string]interface{}{
  216. "term": map[string]interface{}{
  217. "Frequency.keyword": frequency,
  218. //"Frequency.keyword": "月度",
  219. },
  220. })
  221. }
  222. // noPermissionEdbInfoIdList 无权限指标id
  223. if len(noPermissionEdbInfoIdList) > 0 {
  224. mustNotMap = append(mustNotMap, map[string]interface{}{
  225. "terms": map[string]interface{}{
  226. "EdbInfoId": noPermissionEdbInfoIdList,
  227. //"Frequency.keyword": "月度",
  228. },
  229. })
  230. }
  231. // 指标类型:普通指标、预测指标(小于0 代表不区分指标是普通还是预测)
  232. if edbInfoType >= 0 {
  233. mustMap = append(mustMap, map[string]interface{}{
  234. "term": map[string]interface{}{
  235. "EdbInfoType": edbInfoType,
  236. },
  237. })
  238. }
  239. //普通指标
  240. //mustMap = append(mustMap, map[string]interface{}{
  241. // "term": map[string]interface{}{
  242. // "EdbInfoType": 0,
  243. // //"Frequency.keyword": "月度",
  244. // },
  245. //})
  246. //关键字匹配
  247. shouldMap := map[string]interface{}{
  248. "should": []interface{}{
  249. map[string]interface{}{
  250. "match": map[string]interface{}{
  251. "EdbCode": keywordStr,
  252. //"Frequency.keyword": "月度",
  253. },
  254. },
  255. map[string]interface{}{
  256. "match": map[string]interface{}{
  257. "EdbName": keywordStr,
  258. //"Frequency.keyword": "月度",
  259. },
  260. },
  261. map[string]interface{}{
  262. "match": map[string]interface{}{
  263. "EdbNameEn": keywordStr,
  264. //"Frequency.keyword": "月度",
  265. },
  266. },
  267. },
  268. }
  269. mustMap = append(mustMap, map[string]interface{}{
  270. "bool": shouldMap,
  271. })
  272. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  273. }
  274. func SearchEdbInfoDataBak(indexName, keywordStr string, from, size, filterSource, source int, frequency string) (total int64, list []*models.EdbInfoList, err error) {
  275. list = make([]*models.EdbInfoList, 0)
  276. defer func() {
  277. if err != nil {
  278. fmt.Println("EsAddOrEditData Err:", err.Error())
  279. }
  280. }()
  281. client := utils.EsClient
  282. //queryString := elastic.NewQueryStringQuery(keywordStr)
  283. //boolQueryJson, err := json.Marshal(queryString)
  284. //if err != nil {
  285. // fmt.Println("boolQueryJson err:", err)
  286. //} else {
  287. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  288. //}
  289. highlight := elastic.NewHighlight()
  290. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  291. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  292. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  293. //var source map[string]interface{}
  294. //source := map[string]interface{}{
  295. // "query": map[string]interface{}{
  296. // "match_all": map[string]interface{}{},
  297. // },
  298. //}
  299. mustMap := make([]interface{}, 0)
  300. mustNotMap := make([]interface{}, 0)
  301. //source := map[string]interface{}{
  302. // "query": map[string]interface{}{
  303. // "bool": map[string]interface{}{
  304. // "must": map[string]interface{}{
  305. // "query_string": map[string]interface{}{
  306. // "query": keywordStr,
  307. // "fields": []string{"EdbCode", "EdbName"},
  308. // },
  309. // },
  310. // },
  311. // },
  312. //}
  313. switch filterSource {
  314. case 2:
  315. //source = map[string]interface{}{
  316. // "query": map[string]interface{}{
  317. // "bool": map[string]interface{}{
  318. // "must": map[string]interface{}{
  319. // "query_string": map[string]interface{}{
  320. // "query": keywordStr,
  321. // },
  322. // },
  323. // "filter": []interface{}{
  324. // map[string]interface{}{
  325. // "term": map[string]interface{}{
  326. // "Frequency.keyword": "月度",
  327. // },
  328. // }},
  329. // },
  330. // },
  331. //}
  332. mustMap = []interface{}{
  333. map[string]interface{}{
  334. "term": map[string]interface{}{
  335. "Frequency.keyword": "月度",
  336. //"Frequency.keyword": "月度",
  337. },
  338. },
  339. }
  340. case 3:
  341. //source = map[string]interface{}{
  342. // "query": map[string]interface{}{
  343. // "bool": map[string]interface{}{
  344. // "must": map[string]interface{}{
  345. // "query_string": map[string]interface{}{
  346. // "query": keywordStr,
  347. // },
  348. // },
  349. // "must_not": []interface{}{
  350. // map[string]interface{}{
  351. // "match": map[string]interface{}{
  352. // "Frequency.keyword": "日度",
  353. // },
  354. // }},
  355. // },
  356. // },
  357. //}
  358. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  359. //mustNotMap = []interface{}{
  360. // map[string]interface{}{
  361. // "match": map[string]interface{}{
  362. // "Frequency.keyword": "日度",
  363. // //"Frequency.keyword": "月度",
  364. // },
  365. // },
  366. //}
  367. case 4:
  368. //source = map[string]interface{}{
  369. // "query": map[string]interface{}{
  370. // "bool": map[string]interface{}{
  371. // "must": map[string]interface{}{
  372. // "query_string": map[string]interface{}{
  373. // "query": keywordStr,
  374. // },
  375. // },
  376. // "filter": []interface{}{
  377. // map[string]interface{}{
  378. // "term": map[string]interface{}{
  379. // "EdbType": 1,
  380. // },
  381. // }},
  382. // },
  383. // },
  384. //}
  385. mustMap = []interface{}{
  386. map[string]interface{}{
  387. "term": map[string]interface{}{
  388. "EdbType": 1,
  389. },
  390. },
  391. }
  392. case 5:
  393. mustMap = []interface{}{
  394. map[string]interface{}{
  395. "term": map[string]interface{}{
  396. "Source": 6,
  397. },
  398. },
  399. }
  400. case 6:
  401. mustNotMap = []interface{}{
  402. map[string]interface{}{
  403. "match": map[string]interface{}{
  404. "Frequency.keyword": "年度",
  405. },
  406. },
  407. }
  408. }
  409. //指标来源
  410. if source > 0 {
  411. mustMap = append(mustMap, map[string]interface{}{
  412. "term": map[string]interface{}{
  413. "Source": source,
  414. //"Frequency.keyword": "月度",
  415. },
  416. })
  417. }
  418. if frequency != "" {
  419. mustMap = append(mustMap, map[string]interface{}{
  420. "term": map[string]interface{}{
  421. "Frequency.keyword": frequency,
  422. //"Frequency.keyword": "月度",
  423. },
  424. })
  425. }
  426. //普通指标
  427. //mustMap = append(mustMap, map[string]interface{}{
  428. // "term": map[string]interface{}{
  429. // "EdbInfoType": 0,
  430. // //"Frequency.keyword": "月度",
  431. // },
  432. //})
  433. //关键字匹配
  434. shouldMap := map[string]interface{}{
  435. "should": []interface{}{
  436. map[string]interface{}{
  437. "match": map[string]interface{}{
  438. "EdbCode": keywordStr,
  439. //"Frequency.keyword": "月度",
  440. },
  441. },
  442. map[string]interface{}{
  443. "match": map[string]interface{}{
  444. "EdbName": keywordStr,
  445. //"Frequency.keyword": "月度",
  446. },
  447. },
  448. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  449. map[string]interface{}{
  450. "match": map[string]interface{}{
  451. "EdbCode": map[string]interface{}{
  452. "query": keywordStr,
  453. "operator": "and",
  454. },
  455. //"Frequency.keyword": "月度",
  456. },
  457. },
  458. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  459. map[string]interface{}{
  460. "match": map[string]interface{}{
  461. "EdbName": map[string]interface{}{
  462. "query": keywordStr,
  463. "operator": "and",
  464. },
  465. //"Frequency.keyword": "月度",
  466. },
  467. },
  468. },
  469. }
  470. mustMap = append(mustMap, map[string]interface{}{
  471. "bool": shouldMap,
  472. })
  473. queryMap := map[string]interface{}{
  474. "query": map[string]interface{}{
  475. "bool": map[string]interface{}{
  476. "must": mustMap,
  477. "must_not": mustNotMap,
  478. //"should": shouldMap,
  479. },
  480. },
  481. }
  482. //根据条件数量统计
  483. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  484. total, err = requestTotalHits.Do(context.Background())
  485. if err != nil {
  486. return
  487. }
  488. queryMap["from"] = from
  489. queryMap["size"] = size
  490. jsonBytes, _ := json.Marshal(queryMap)
  491. fmt.Println(string(jsonBytes))
  492. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  493. //switch filterSource {
  494. //case 2:
  495. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  496. //case 3:
  497. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  498. //case 4:
  499. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  500. //}
  501. //queryString := elastic.RawStringQuery(queryStr)
  502. //fmt.Println("queryString:", queryString)
  503. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  504. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  505. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  506. //requestJson, err := json.Marshal(request)
  507. //if err != nil {
  508. // fmt.Println("requestJson err:", err)
  509. //}
  510. //fmt.Println("requestJson ", string(requestJson))
  511. searchMap := make(map[string]string)
  512. searchResp, err := request.Do(context.Background())
  513. if err != nil {
  514. return
  515. }
  516. fmt.Println(searchResp)
  517. fmt.Println(searchResp.Status)
  518. if searchResp.Status != 0 {
  519. return
  520. }
  521. if searchResp.Hits != nil {
  522. for _, v := range searchResp.Hits.Hits {
  523. if _, ok := searchMap[v.Id]; !ok {
  524. itemJson, tmpErr := v.Source.MarshalJSON()
  525. if tmpErr != nil {
  526. err = tmpErr
  527. fmt.Println("movieJson err:", err)
  528. return
  529. }
  530. edbInfoItem := new(models.EdbInfoList)
  531. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  532. if tmpErr != nil {
  533. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  534. err = tmpErr
  535. return
  536. }
  537. if len(v.Highlight["EdbCode"]) > 0 {
  538. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  539. }
  540. if len(v.Highlight["EdbName"]) > 0 {
  541. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  542. }
  543. list = append(list, edbInfoItem)
  544. searchMap[v.Id] = v.Id
  545. }
  546. }
  547. }
  548. //for _, v := range result {
  549. // fmt.Println(v)
  550. //}
  551. return
  552. }
  553. // SearchAddPredictEdbInfoData 查询允许添加预测指标的数据
  554. func SearchAddPredictEdbInfoData(indexName, keywordStr string, noPermissionEdbInfoIdList []int, from, size int) (total int64, list []*models.EdbInfoList, err error) {
  555. list = make([]*models.EdbInfoList, 0)
  556. defer func() {
  557. if err != nil {
  558. fmt.Println("EsAddOrEditData Err:", err.Error())
  559. }
  560. }()
  561. highlight := elastic.NewHighlight()
  562. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  563. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  564. mustMap := make([]interface{}, 0)
  565. mustNotMap := make([]interface{}, 0)
  566. mustNotMap = []interface{}{
  567. //map[string]interface{}{
  568. // "terms": map[string]interface{}{
  569. // "Frequency.keyword": []string{"日度", "周度", "月度"},
  570. // },
  571. // //"match": map[string]interface{}{
  572. // // "Frequency": []string{"日度", "周度", "月度"},
  573. // // //"Frequency.keyword": []string{"日度", "周度", "月度"},
  574. // //},
  575. //},
  576. }
  577. // 指标类型:普通指标、预算指标
  578. mustMap = append(mustMap, map[string]interface{}{
  579. "term": map[string]interface{}{
  580. "EdbInfoType": 0,
  581. },
  582. })
  583. mustMap = append(mustMap, map[string]interface{}{
  584. "terms": map[string]interface{}{
  585. "Frequency.keyword": []string{"日度", "周度", "月度"},
  586. },
  587. })
  588. //关键字匹配
  589. shouldMap := map[string]interface{}{
  590. "should": []interface{}{
  591. map[string]interface{}{
  592. "match": map[string]interface{}{
  593. "EdbCode": keywordStr,
  594. //"Frequency.keyword": "月度",
  595. },
  596. },
  597. map[string]interface{}{
  598. "match": map[string]interface{}{
  599. "EdbName": keywordStr,
  600. //"Frequency.keyword": "月度",
  601. },
  602. },
  603. },
  604. }
  605. // noPermissionEdbInfoIdList 无权限指标id
  606. if len(noPermissionEdbInfoIdList) > 0 {
  607. mustNotMap = append(mustNotMap, map[string]interface{}{
  608. "terms": map[string]interface{}{
  609. "EdbInfoId": noPermissionEdbInfoIdList,
  610. //"Frequency.keyword": "月度",
  611. },
  612. })
  613. }
  614. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  615. }
  616. // searchEdbInfoData 查询es中的指标数据
  617. func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMap map[string]interface{}, from, size int) (total int64, list []*models.EdbInfoList, err error) {
  618. list = make([]*models.EdbInfoList, 0)
  619. defer func() {
  620. if err != nil {
  621. fmt.Println("EsAddOrEditData Err:", err.Error())
  622. }
  623. }()
  624. client := utils.EsClient
  625. //queryString := elastic.NewQueryStringQuery(keywordStr)
  626. //boolQueryJson, err := json.Marshal(queryString)
  627. //if err != nil {
  628. // fmt.Println("boolQueryJson err:", err)
  629. //} else {
  630. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  631. //}
  632. highlight := elastic.NewHighlight()
  633. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  634. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  635. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  636. //关键字匹配
  637. mustMap = append(mustMap, map[string]interface{}{
  638. "bool": shouldMap,
  639. })
  640. queryMap := map[string]interface{}{
  641. "query": map[string]interface{}{
  642. "bool": map[string]interface{}{
  643. "must": mustMap,
  644. "must_not": mustNotMap,
  645. //"should": shouldMap,
  646. },
  647. },
  648. }
  649. //根据条件数量统计
  650. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  651. total, err = requestTotalHits.Do(context.Background())
  652. if err != nil {
  653. return
  654. }
  655. queryMap["from"] = from
  656. queryMap["size"] = size
  657. jsonBytes, _ := json.Marshal(queryMap)
  658. fmt.Println(string(jsonBytes))
  659. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  660. //switch filterSource {
  661. //case 2:
  662. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  663. //case 3:
  664. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  665. //case 4:
  666. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  667. //}
  668. //queryString := elastic.RawStringQuery(queryStr)
  669. //fmt.Println("queryString:", queryString)
  670. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  671. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  672. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  673. //requestJson, err := json.Marshal(request)
  674. //if err != nil {
  675. // fmt.Println("requestJson err:", err)
  676. //}
  677. //fmt.Println("requestJson ", string(requestJson))
  678. searchMap := make(map[string]string)
  679. searchResp, err := request.Do(context.Background())
  680. if err != nil {
  681. return
  682. }
  683. fmt.Println(searchResp)
  684. fmt.Println(searchResp.Status)
  685. if searchResp.Status != 0 {
  686. return
  687. }
  688. //total = searchResp.TotalHits()
  689. if searchResp.Hits != nil {
  690. for _, v := range searchResp.Hits.Hits {
  691. if _, ok := searchMap[v.Id]; !ok {
  692. itemJson, tmpErr := v.Source.MarshalJSON()
  693. if tmpErr != nil {
  694. err = tmpErr
  695. fmt.Println("movieJson err:", err)
  696. return
  697. }
  698. edbInfoItem := new(models.EdbInfoList)
  699. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  700. if tmpErr != nil {
  701. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  702. err = tmpErr
  703. return
  704. }
  705. if len(v.Highlight["EdbCode"]) > 0 {
  706. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  707. }
  708. if len(v.Highlight["EdbName"]) > 0 {
  709. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  710. }
  711. list = append(list, edbInfoItem)
  712. searchMap[v.Id] = v.Id
  713. }
  714. }
  715. }
  716. return
  717. }
  718. // EsDeleteEdbInfoData 删除es中的指标数据
  719. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  720. defer func() {
  721. if err != nil {
  722. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  723. }
  724. }()
  725. client := utils.EsClient
  726. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  727. fmt.Println(resp)
  728. if err != nil {
  729. return
  730. }
  731. if resp.Status == 0 {
  732. fmt.Println("删除成功")
  733. } else {
  734. fmt.Println("AddData", resp.Status, resp.Result)
  735. }
  736. return
  737. }
  738. // AnalyzeResp 分词接口返回结构体
  739. type AnalyzeResp struct {
  740. Tokens []struct {
  741. EndOffset int64 `json:"end_offset"`
  742. Position int64 `json:"position"`
  743. StartOffset int64 `json:"start_offset"`
  744. Token string `json:"token"`
  745. Type string `json:"type"`
  746. } `json:"tokens"`
  747. }
  748. // Analyze 根据输入的文字获取分词后的文字
  749. func Analyze(content string) (contentList []string, err error) {
  750. defer func() {
  751. if err != nil {
  752. fmt.Println("Analyze Err:", err.Error())
  753. }
  754. }()
  755. client := utils.EsClient
  756. queryMap := map[string]string{
  757. "text": content,
  758. "analyzer": "ik_max_word",
  759. }
  760. res, err := client.PerformRequest(
  761. context.Background(),
  762. elastic.PerformRequestOptions{
  763. Method: "GET",
  764. Path: "/_analyze",
  765. Body: queryMap,
  766. Stream: false,
  767. },
  768. )
  769. if res.StatusCode == 200 {
  770. var analyzeResp AnalyzeResp
  771. tmpErr := json.Unmarshal(res.Body, &analyzeResp)
  772. if tmpErr != nil {
  773. err = errors.New("返回数据转结构体失败:" + tmpErr.Error())
  774. return
  775. }
  776. for _, v := range analyzeResp.Tokens {
  777. contentList = append(contentList, v.Token)
  778. }
  779. } else {
  780. err = errors.New("分词失败,返回code异常:" + strconv.Itoa(res.StatusCode))
  781. }
  782. return
  783. }
  784. // EsAddOrEditChartInfoData 新增/修改es中的图表数据
  785. func EsAddOrEditChartInfoData(indexName, docId string, item *models.ChartInfo) (err error) {
  786. defer func() {
  787. if err != nil {
  788. fmt.Println("EsAddOrEditData Err:", err.Error())
  789. }
  790. }()
  791. client := utils.EsClient
  792. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  793. if err != nil {
  794. fmt.Println("新增失败:", err.Error())
  795. return err
  796. }
  797. fmt.Println(resp)
  798. if resp.Status == 0 {
  799. fmt.Println("新增成功", resp.Result)
  800. err = nil
  801. } else {
  802. fmt.Println("AddData", resp.Status, resp.Result)
  803. }
  804. return
  805. }
  806. // EsDeleteDataV2 删除es中的数据
  807. func EsDeleteDataV2(indexName, docId string) (err error) {
  808. defer func() {
  809. if err != nil {
  810. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  811. }
  812. }()
  813. client := utils.EsClient
  814. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  815. fmt.Println(resp)
  816. if err != nil {
  817. return
  818. }
  819. if resp.Status == 0 {
  820. fmt.Println("删除成功")
  821. } else {
  822. fmt.Println("AddData", resp.Status, resp.Result)
  823. }
  824. return
  825. }
  826. // SearchChartInfoData 查询es中的图表数据
  827. func SearchChartInfoData(indexName, keywordStr string, showSysId []int, sourceList []int, from, size int) (list []*models.ChartInfo, total int64, err error) {
  828. list = make([]*models.ChartInfo, 0)
  829. defer func() {
  830. if err != nil {
  831. fmt.Println("EsAddOrEditData Err:", err.Error())
  832. }
  833. }()
  834. client := utils.EsClient
  835. //queryString := elastic.NewQueryStringQuery(keywordStr)
  836. //boolQueryJson, err := json.Marshal(queryString)
  837. //if err != nil {
  838. // fmt.Println("boolQueryJson err:", err)
  839. //} else {
  840. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  841. //}
  842. highlight := elastic.NewHighlight()
  843. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  844. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  845. mustMap := make([]interface{}, 0)
  846. mustNotMap := make([]interface{}, 0)
  847. //指标来源
  848. if len(showSysId) > 0 {
  849. mustMap = append(mustMap, map[string]interface{}{
  850. "terms": map[string]interface{}{
  851. "SysUserId": showSysId,
  852. //"Frequency.keyword": "月度",
  853. },
  854. })
  855. }
  856. mustMap = append(mustMap, map[string]interface{}{
  857. "terms": map[string]interface{}{
  858. "Source": sourceList,
  859. },
  860. })
  861. //关键字匹配
  862. shouldMap := map[string]interface{}{
  863. "should": []interface{}{
  864. map[string]interface{}{
  865. "match": map[string]interface{}{
  866. "ChartName": keywordStr,
  867. //"Frequency.keyword": "月度",
  868. },
  869. },
  870. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  871. map[string]interface{}{
  872. "match": map[string]interface{}{
  873. "ChartName": map[string]interface{}{
  874. "query": keywordStr,
  875. "operator": "and",
  876. },
  877. //"Frequency.keyword": "月度",
  878. },
  879. },
  880. map[string]interface{}{
  881. "match": map[string]interface{}{
  882. "ChartNameEn": keywordStr,
  883. //"Frequency.keyword": "月度",
  884. },
  885. },
  886. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  887. map[string]interface{}{
  888. "match": map[string]interface{}{
  889. "ChartNameEn": map[string]interface{}{
  890. "query": keywordStr,
  891. "operator": "and",
  892. },
  893. //"Frequency.keyword": "月度",
  894. },
  895. },
  896. },
  897. }
  898. mustMap = append(mustMap, map[string]interface{}{
  899. "bool": shouldMap,
  900. })
  901. queryMap := map[string]interface{}{
  902. "query": map[string]interface{}{
  903. "bool": map[string]interface{}{
  904. "must": mustMap,
  905. "must_not": mustNotMap,
  906. //"should": shouldMap,
  907. },
  908. },
  909. }
  910. //根据条件数量统计
  911. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  912. total, err = requestTotalHits.Do(context.Background())
  913. if err != nil {
  914. return
  915. }
  916. // 分页查询
  917. queryMap["from"] = from
  918. queryMap["size"] = size
  919. jsonBytes, _ := json.Marshal(queryMap)
  920. fmt.Println(string(jsonBytes))
  921. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  922. //requestJson, err := json.Marshal(request)
  923. //if err != nil {
  924. // fmt.Println("requestJson err:", err)
  925. //}
  926. //fmt.Println("requestJson ", string(requestJson))
  927. searchMap := make(map[string]string)
  928. searchResp, err := request.Do(context.Background())
  929. if err != nil {
  930. return
  931. }
  932. fmt.Println(searchResp)
  933. fmt.Println(searchResp.Status)
  934. if searchResp.Status != 0 {
  935. return
  936. }
  937. if searchResp.Hits != nil {
  938. for _, v := range searchResp.Hits.Hits {
  939. if _, ok := searchMap[v.Id]; !ok {
  940. itemJson, tmpErr := v.Source.MarshalJSON()
  941. if tmpErr != nil {
  942. err = tmpErr
  943. fmt.Println("movieJson err:", err)
  944. return
  945. }
  946. chartInfoItem := new(models.ChartInfo)
  947. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  948. if err != nil {
  949. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  950. err = tmpErr
  951. return
  952. }
  953. if len(v.Highlight["ChartName"]) > 0 {
  954. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  955. }
  956. list = append(list, chartInfoItem)
  957. searchMap[v.Id] = v.Id
  958. }
  959. }
  960. }
  961. //for _, v := range result {
  962. // fmt.Println(v)
  963. //}
  964. return
  965. }
  966. // EsAddOrEditDataInterface 新增/修改es中的数据
  967. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  968. defer func() {
  969. if err != nil {
  970. fmt.Println("EsAddOrEditData Err:", err.Error())
  971. }
  972. }()
  973. client := utils.EsClient
  974. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  975. if err != nil {
  976. fmt.Println("新增失败:", err.Error())
  977. return err
  978. }
  979. fmt.Println(resp)
  980. if resp.Status == 0 {
  981. fmt.Println("新增成功", resp.Result)
  982. err = nil
  983. } else {
  984. fmt.Println("AddData", resp.Status, resp.Result)
  985. }
  986. return
  987. }
  988. // SearchEdbInfoDataByAdminId 查询es中的指标数据
  989. func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType uint8, frequency string, adminId int) (total int64, list []*models.EdbInfoList, err error) {
  990. list = make([]*models.EdbInfoList, 0)
  991. defer func() {
  992. if err != nil {
  993. fmt.Println("EsAddOrEditData Err:", err.Error())
  994. }
  995. }()
  996. highlight := elastic.NewHighlight()
  997. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  998. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  999. //var source map[string]interface{}
  1000. //source := map[string]interface{}{
  1001. // "query": map[string]interface{}{
  1002. // "match_all": map[string]interface{}{},
  1003. // },
  1004. //}
  1005. mustMap := make([]interface{}, 0)
  1006. mustNotMap := make([]interface{}, 0)
  1007. //source := map[string]interface{}{
  1008. // "query": map[string]interface{}{
  1009. // "bool": map[string]interface{}{
  1010. // "must": map[string]interface{}{
  1011. // "query_string": map[string]interface{}{
  1012. // "query": keywordStr,
  1013. // "fields": []string{"EdbCode", "EdbName"},
  1014. // },
  1015. // },
  1016. // },
  1017. // },
  1018. //}
  1019. switch filterSource {
  1020. case 2:
  1021. //source = map[string]interface{}{
  1022. // "query": map[string]interface{}{
  1023. // "bool": map[string]interface{}{
  1024. // "must": map[string]interface{}{
  1025. // "query_string": map[string]interface{}{
  1026. // "query": keywordStr,
  1027. // },
  1028. // },
  1029. // "filter": []interface{}{
  1030. // map[string]interface{}{
  1031. // "term": map[string]interface{}{
  1032. // "Frequency.keyword": "月度",
  1033. // },
  1034. // }},
  1035. // },
  1036. // },
  1037. //}
  1038. mustMap = []interface{}{
  1039. map[string]interface{}{
  1040. "term": map[string]interface{}{
  1041. "Frequency.keyword": "月度",
  1042. //"Frequency.keyword": "月度",
  1043. },
  1044. },
  1045. }
  1046. case 3:
  1047. //source = map[string]interface{}{
  1048. // "query": map[string]interface{}{
  1049. // "bool": map[string]interface{}{
  1050. // "must": map[string]interface{}{
  1051. // "query_string": map[string]interface{}{
  1052. // "query": keywordStr,
  1053. // },
  1054. // },
  1055. // "must_not": []interface{}{
  1056. // map[string]interface{}{
  1057. // "match": map[string]interface{}{
  1058. // "Frequency.keyword": "日度",
  1059. // },
  1060. // }},
  1061. // },
  1062. // },
  1063. //}
  1064. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  1065. //mustNotMap = []interface{}{
  1066. // map[string]interface{}{
  1067. // "match": map[string]interface{}{
  1068. // "Frequency.keyword": "日度",
  1069. // //"Frequency.keyword": "月度",
  1070. // },
  1071. // },
  1072. //}
  1073. case 4:
  1074. //source = map[string]interface{}{
  1075. // "query": map[string]interface{}{
  1076. // "bool": map[string]interface{}{
  1077. // "must": map[string]interface{}{
  1078. // "query_string": map[string]interface{}{
  1079. // "query": keywordStr,
  1080. // },
  1081. // },
  1082. // "filter": []interface{}{
  1083. // map[string]interface{}{
  1084. // "term": map[string]interface{}{
  1085. // "EdbType": 1,
  1086. // },
  1087. // }},
  1088. // },
  1089. // },
  1090. //}
  1091. mustMap = []interface{}{
  1092. map[string]interface{}{
  1093. "term": map[string]interface{}{
  1094. "EdbType": 1,
  1095. },
  1096. },
  1097. }
  1098. case 5:
  1099. mustMap = []interface{}{
  1100. map[string]interface{}{
  1101. "term": map[string]interface{}{
  1102. "Source": 6,
  1103. },
  1104. },
  1105. }
  1106. case 6:
  1107. mustNotMap = []interface{}{
  1108. map[string]interface{}{
  1109. "match": map[string]interface{}{
  1110. "Frequency.keyword": "年度",
  1111. },
  1112. },
  1113. }
  1114. }
  1115. //指标来源
  1116. if source > 0 {
  1117. mustMap = append(mustMap, map[string]interface{}{
  1118. "term": map[string]interface{}{
  1119. "Source": source,
  1120. //"Frequency.keyword": "月度",
  1121. },
  1122. })
  1123. }
  1124. if frequency != "" {
  1125. mustMap = append(mustMap, map[string]interface{}{
  1126. "term": map[string]interface{}{
  1127. "Frequency.keyword": frequency,
  1128. //"Frequency.keyword": "月度",
  1129. },
  1130. })
  1131. }
  1132. // 指标类型:普通指标、预算指标
  1133. mustMap = append(mustMap, map[string]interface{}{
  1134. "term": map[string]interface{}{
  1135. "EdbInfoType": edbInfoType,
  1136. },
  1137. })
  1138. //普通指标
  1139. //mustMap = append(mustMap, map[string]interface{}{
  1140. // "term": map[string]interface{}{
  1141. // "EdbInfoType": 0,
  1142. // //"Frequency.keyword": "月度",
  1143. // },
  1144. //})
  1145. //关键字匹配
  1146. shouldMap := map[string]interface{}{
  1147. "should": []interface{}{
  1148. map[string]interface{}{
  1149. "match": map[string]interface{}{
  1150. "EdbCode": keywordStr,
  1151. //"Frequency.keyword": "月度",
  1152. },
  1153. },
  1154. map[string]interface{}{
  1155. "match": map[string]interface{}{
  1156. "EdbName": keywordStr,
  1157. //"Frequency.keyword": "月度",
  1158. },
  1159. },
  1160. },
  1161. }
  1162. mustMap = append(mustMap, map[string]interface{}{
  1163. "bool": shouldMap,
  1164. })
  1165. //创建人
  1166. if adminId > 0 {
  1167. mustMap = append(mustMap, map[string]interface{}{
  1168. "term": map[string]interface{}{
  1169. "SysUserId": adminId,
  1170. },
  1171. })
  1172. }
  1173. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  1174. }