elastic.go 40 KB

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