elastic.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_forum_hub/models"
  7. "eta/eta_forum_hub/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, noPermissionChartIdList []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 showSysId > 0 {
  849. mustMap = append(mustMap, map[string]interface{}{
  850. "term": 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. // noPermissionEdbInfoIdList 无权限指标id
  902. if len(noPermissionChartIdList) > 0 {
  903. mustNotMap = append(mustNotMap, map[string]interface{}{
  904. "terms": map[string]interface{}{
  905. "ChartInfoId": noPermissionChartIdList,
  906. //"Frequency.keyword": "月度",
  907. },
  908. })
  909. }
  910. queryMap := map[string]interface{}{
  911. "query": map[string]interface{}{
  912. "bool": map[string]interface{}{
  913. "must": mustMap,
  914. "must_not": mustNotMap,
  915. //"should": shouldMap,
  916. },
  917. },
  918. }
  919. //根据条件数量统计
  920. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  921. total, err = requestTotalHits.Do(context.Background())
  922. if err != nil {
  923. return
  924. }
  925. // 分页查询
  926. queryMap["from"] = from
  927. queryMap["size"] = size
  928. jsonBytes, _ := json.Marshal(queryMap)
  929. fmt.Println(string(jsonBytes))
  930. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  931. //requestJson, err := json.Marshal(request)
  932. //if err != nil {
  933. // fmt.Println("requestJson err:", err)
  934. //}
  935. //fmt.Println("requestJson ", string(requestJson))
  936. searchMap := make(map[string]string)
  937. searchResp, err := request.Do(context.Background())
  938. if err != nil {
  939. return
  940. }
  941. fmt.Println(searchResp)
  942. fmt.Println(searchResp.Status)
  943. if searchResp.Status != 0 {
  944. return
  945. }
  946. if searchResp.Hits != nil {
  947. for _, v := range searchResp.Hits.Hits {
  948. if _, ok := searchMap[v.Id]; !ok {
  949. itemJson, tmpErr := v.Source.MarshalJSON()
  950. if tmpErr != nil {
  951. err = tmpErr
  952. fmt.Println("movieJson err:", err)
  953. return
  954. }
  955. chartInfoItem := new(models.ChartInfo)
  956. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  957. if err != nil {
  958. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  959. err = tmpErr
  960. return
  961. }
  962. if len(v.Highlight["ChartName"]) > 0 {
  963. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  964. }
  965. list = append(list, chartInfoItem)
  966. searchMap[v.Id] = v.Id
  967. }
  968. }
  969. }
  970. //for _, v := range result {
  971. // fmt.Println(v)
  972. //}
  973. return
  974. }
  975. // EsAddOrEditDataInterface 新增/修改es中的数据
  976. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  977. defer func() {
  978. if err != nil {
  979. fmt.Println("EsAddOrEditData Err:", err.Error())
  980. }
  981. }()
  982. client := utils.EsClient
  983. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  984. if err != nil {
  985. fmt.Println("新增失败:", err.Error())
  986. return err
  987. }
  988. fmt.Println(resp)
  989. if resp.Status == 0 {
  990. fmt.Println("新增成功", resp.Result)
  991. err = nil
  992. } else {
  993. fmt.Println("AddData", resp.Status, resp.Result)
  994. }
  995. return
  996. }
  997. // SearchEdbInfoDataByAdminId 查询es中的指标数据
  998. func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType uint8, frequency string, adminId int) (total int64, list []*models.EdbInfoList, err error) {
  999. list = make([]*models.EdbInfoList, 0)
  1000. defer func() {
  1001. if err != nil {
  1002. fmt.Println("EsAddOrEditData Err:", err.Error())
  1003. }
  1004. }()
  1005. highlight := elastic.NewHighlight()
  1006. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  1007. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1008. //var source map[string]interface{}
  1009. //source := map[string]interface{}{
  1010. // "query": map[string]interface{}{
  1011. // "match_all": map[string]interface{}{},
  1012. // },
  1013. //}
  1014. mustMap := make([]interface{}, 0)
  1015. mustNotMap := make([]interface{}, 0)
  1016. //source := map[string]interface{}{
  1017. // "query": map[string]interface{}{
  1018. // "bool": map[string]interface{}{
  1019. // "must": map[string]interface{}{
  1020. // "query_string": map[string]interface{}{
  1021. // "query": keywordStr,
  1022. // "fields": []string{"EdbCode", "EdbName"},
  1023. // },
  1024. // },
  1025. // },
  1026. // },
  1027. //}
  1028. switch filterSource {
  1029. case 2:
  1030. //source = map[string]interface{}{
  1031. // "query": map[string]interface{}{
  1032. // "bool": map[string]interface{}{
  1033. // "must": map[string]interface{}{
  1034. // "query_string": map[string]interface{}{
  1035. // "query": keywordStr,
  1036. // },
  1037. // },
  1038. // "filter": []interface{}{
  1039. // map[string]interface{}{
  1040. // "term": map[string]interface{}{
  1041. // "Frequency.keyword": "月度",
  1042. // },
  1043. // }},
  1044. // },
  1045. // },
  1046. //}
  1047. mustMap = []interface{}{
  1048. map[string]interface{}{
  1049. "term": map[string]interface{}{
  1050. "Frequency.keyword": "月度",
  1051. //"Frequency.keyword": "月度",
  1052. },
  1053. },
  1054. }
  1055. case 3:
  1056. //source = map[string]interface{}{
  1057. // "query": map[string]interface{}{
  1058. // "bool": map[string]interface{}{
  1059. // "must": map[string]interface{}{
  1060. // "query_string": map[string]interface{}{
  1061. // "query": keywordStr,
  1062. // },
  1063. // },
  1064. // "must_not": []interface{}{
  1065. // map[string]interface{}{
  1066. // "match": map[string]interface{}{
  1067. // "Frequency.keyword": "日度",
  1068. // },
  1069. // }},
  1070. // },
  1071. // },
  1072. //}
  1073. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  1074. //mustNotMap = []interface{}{
  1075. // map[string]interface{}{
  1076. // "match": map[string]interface{}{
  1077. // "Frequency.keyword": "日度",
  1078. // //"Frequency.keyword": "月度",
  1079. // },
  1080. // },
  1081. //}
  1082. case 4:
  1083. //source = map[string]interface{}{
  1084. // "query": map[string]interface{}{
  1085. // "bool": map[string]interface{}{
  1086. // "must": map[string]interface{}{
  1087. // "query_string": map[string]interface{}{
  1088. // "query": keywordStr,
  1089. // },
  1090. // },
  1091. // "filter": []interface{}{
  1092. // map[string]interface{}{
  1093. // "term": map[string]interface{}{
  1094. // "EdbType": 1,
  1095. // },
  1096. // }},
  1097. // },
  1098. // },
  1099. //}
  1100. mustMap = []interface{}{
  1101. map[string]interface{}{
  1102. "term": map[string]interface{}{
  1103. "EdbType": 1,
  1104. },
  1105. },
  1106. }
  1107. case 5:
  1108. mustMap = []interface{}{
  1109. map[string]interface{}{
  1110. "term": map[string]interface{}{
  1111. "Source": 6,
  1112. },
  1113. },
  1114. }
  1115. case 6:
  1116. mustNotMap = []interface{}{
  1117. map[string]interface{}{
  1118. "match": map[string]interface{}{
  1119. "Frequency.keyword": "年度",
  1120. },
  1121. },
  1122. }
  1123. }
  1124. //指标来源
  1125. if source > 0 {
  1126. mustMap = append(mustMap, map[string]interface{}{
  1127. "term": map[string]interface{}{
  1128. "Source": source,
  1129. //"Frequency.keyword": "月度",
  1130. },
  1131. })
  1132. }
  1133. if frequency != "" {
  1134. mustMap = append(mustMap, map[string]interface{}{
  1135. "term": map[string]interface{}{
  1136. "Frequency.keyword": frequency,
  1137. //"Frequency.keyword": "月度",
  1138. },
  1139. })
  1140. }
  1141. // 指标类型:普通指标、预算指标
  1142. mustMap = append(mustMap, map[string]interface{}{
  1143. "term": map[string]interface{}{
  1144. "EdbInfoType": edbInfoType,
  1145. },
  1146. })
  1147. //普通指标
  1148. //mustMap = append(mustMap, map[string]interface{}{
  1149. // "term": map[string]interface{}{
  1150. // "EdbInfoType": 0,
  1151. // //"Frequency.keyword": "月度",
  1152. // },
  1153. //})
  1154. //关键字匹配
  1155. shouldMap := map[string]interface{}{
  1156. "should": []interface{}{
  1157. map[string]interface{}{
  1158. "match": map[string]interface{}{
  1159. "EdbCode": keywordStr,
  1160. //"Frequency.keyword": "月度",
  1161. },
  1162. },
  1163. map[string]interface{}{
  1164. "match": map[string]interface{}{
  1165. "EdbName": keywordStr,
  1166. //"Frequency.keyword": "月度",
  1167. },
  1168. },
  1169. },
  1170. }
  1171. mustMap = append(mustMap, map[string]interface{}{
  1172. "bool": shouldMap,
  1173. })
  1174. //创建人
  1175. if adminId > 0 {
  1176. mustMap = append(mustMap, map[string]interface{}{
  1177. "term": map[string]interface{}{
  1178. "SysUserId": adminId,
  1179. },
  1180. })
  1181. }
  1182. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  1183. }