elastic.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/data_manage/excel"
  9. dataSourceModel "eta/eta_api/models/data_source"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/olivere/elastic/v7"
  13. "strconv"
  14. "strings"
  15. )
  16. // indexName:索引名称
  17. // mappingJson:表结构
  18. func EsCreateIndex(indexName, mappingJson string) (err error) {
  19. client := utils.EsClient
  20. //定义表结构
  21. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  22. if err != nil {
  23. return
  24. }
  25. if !exists {
  26. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  27. //BodyJson(bodyJson).Do(context.Background())
  28. if err != nil {
  29. fmt.Println("CreateIndex Err:" + err.Error())
  30. return err
  31. }
  32. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  33. } else {
  34. fmt.Println(indexName + " 已存在")
  35. }
  36. return
  37. }
  38. // 删除数据
  39. func EsDeleteData(indexName, docId string) (err error) {
  40. client := utils.EsClient
  41. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  42. fmt.Println(resp)
  43. if err != nil {
  44. return
  45. }
  46. if resp.Status == 0 {
  47. fmt.Println("删除成功")
  48. } else {
  49. fmt.Println("AddData", resp.Status, resp.Result)
  50. }
  51. return
  52. }
  53. func MappingModify(indexName, mappingJson string) {
  54. client := utils.EsClient
  55. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  56. fmt.Println(err)
  57. fmt.Println(result)
  58. return
  59. }
  60. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  61. func EsAddOrEditEdbInfoData(indexName, docId string, item *data_manage.EdbInfoList) (err error) {
  62. defer func() {
  63. if err != nil {
  64. fmt.Println("EsAddOrEditData Err:", err.Error())
  65. }
  66. }()
  67. client := utils.EsClient
  68. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  69. if err != nil {
  70. fmt.Println("新增失败:", err.Error())
  71. return err
  72. }
  73. fmt.Println(resp)
  74. if resp.Status == 0 {
  75. fmt.Println("新增成功", resp.Result)
  76. err = nil
  77. } else {
  78. fmt.Println("AddData", resp.Status, resp.Result)
  79. }
  80. return
  81. }
  82. // SearchEdbInfoData 查询es中的指标数据
  83. func SearchEdbInfoData(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType int8, frequency string, noPermissionEdbInfoIdList []int) (total int64, list []*data_manage.EdbInfoList, err error) {
  84. list = make([]*data_manage.EdbInfoList, 0)
  85. defer func() {
  86. if err != nil {
  87. fmt.Println("EsAddOrEditData Err:", err.Error())
  88. }
  89. }()
  90. highlight := elastic.NewHighlight()
  91. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  92. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  93. //var source map[string]interface{}
  94. //source := map[string]interface{}{
  95. // "query": map[string]interface{}{
  96. // "match_all": map[string]interface{}{},
  97. // },
  98. //}
  99. mustMap := make([]interface{}, 0)
  100. mustNotMap := make([]interface{}, 0)
  101. //source := map[string]interface{}{
  102. // "query": map[string]interface{}{
  103. // "bool": map[string]interface{}{
  104. // "must": map[string]interface{}{
  105. // "query_string": map[string]interface{}{
  106. // "query": keywordStr,
  107. // "fields": []string{"EdbCode", "EdbName"},
  108. // },
  109. // },
  110. // },
  111. // },
  112. //}
  113. switch filterSource {
  114. case 2:
  115. //source = map[string]interface{}{
  116. // "query": map[string]interface{}{
  117. // "bool": map[string]interface{}{
  118. // "must": map[string]interface{}{
  119. // "query_string": map[string]interface{}{
  120. // "query": keywordStr,
  121. // },
  122. // },
  123. // "filter": []interface{}{
  124. // map[string]interface{}{
  125. // "term": map[string]interface{}{
  126. // "Frequency.keyword": "月度",
  127. // },
  128. // }},
  129. // },
  130. // },
  131. //}
  132. mustMap = []interface{}{
  133. map[string]interface{}{
  134. "term": map[string]interface{}{
  135. "Frequency.keyword": "月度",
  136. //"Frequency.keyword": "月度",
  137. },
  138. },
  139. }
  140. case 3:
  141. //source = map[string]interface{}{
  142. // "query": map[string]interface{}{
  143. // "bool": map[string]interface{}{
  144. // "must": map[string]interface{}{
  145. // "query_string": map[string]interface{}{
  146. // "query": keywordStr,
  147. // },
  148. // },
  149. // "must_not": []interface{}{
  150. // map[string]interface{}{
  151. // "match": map[string]interface{}{
  152. // "Frequency.keyword": "日度",
  153. // },
  154. // }},
  155. // },
  156. // },
  157. //}
  158. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  159. //mustNotMap = []interface{}{
  160. // map[string]interface{}{
  161. // "match": map[string]interface{}{
  162. // "Frequency.keyword": "日度",
  163. // //"Frequency.keyword": "月度",
  164. // },
  165. // },
  166. //}
  167. case 4:
  168. //source = map[string]interface{}{
  169. // "query": map[string]interface{}{
  170. // "bool": map[string]interface{}{
  171. // "must": map[string]interface{}{
  172. // "query_string": map[string]interface{}{
  173. // "query": keywordStr,
  174. // },
  175. // },
  176. // "filter": []interface{}{
  177. // map[string]interface{}{
  178. // "term": map[string]interface{}{
  179. // "EdbType": 1,
  180. // },
  181. // }},
  182. // },
  183. // },
  184. //}
  185. mustMap = []interface{}{
  186. map[string]interface{}{
  187. "term": map[string]interface{}{
  188. "EdbType": 1,
  189. },
  190. },
  191. }
  192. case 5:
  193. mustMap = []interface{}{
  194. map[string]interface{}{
  195. "term": map[string]interface{}{
  196. "Source": 6,
  197. },
  198. },
  199. }
  200. case 6:
  201. mustNotMap = []interface{}{
  202. map[string]interface{}{
  203. "match": map[string]interface{}{
  204. "Frequency.keyword": "年度",
  205. },
  206. },
  207. }
  208. }
  209. //指标来源
  210. if source > 0 {
  211. mustMap = append(mustMap, map[string]interface{}{
  212. "term": map[string]interface{}{
  213. "Source": source,
  214. //"Frequency.keyword": "月度",
  215. },
  216. })
  217. }
  218. if frequency != "" {
  219. mustMap = append(mustMap, map[string]interface{}{
  220. "term": map[string]interface{}{
  221. "Frequency.keyword": frequency,
  222. //"Frequency.keyword": "月度",
  223. },
  224. })
  225. }
  226. // noPermissionEdbInfoIdList 无权限指标id
  227. if len(noPermissionEdbInfoIdList) > 0 {
  228. mustNotMap = append(mustNotMap, map[string]interface{}{
  229. "terms": map[string]interface{}{
  230. "EdbInfoId": noPermissionEdbInfoIdList,
  231. //"Frequency.keyword": "月度",
  232. },
  233. })
  234. }
  235. // 指标类型:普通指标、预测指标(小于0 代表不区分指标是普通还是预测)
  236. if edbInfoType >= 0 {
  237. mustMap = append(mustMap, map[string]interface{}{
  238. "term": map[string]interface{}{
  239. "EdbInfoType": edbInfoType,
  240. },
  241. })
  242. }
  243. //普通指标
  244. //mustMap = append(mustMap, map[string]interface{}{
  245. // "term": map[string]interface{}{
  246. // "EdbInfoType": 0,
  247. // //"Frequency.keyword": "月度",
  248. // },
  249. //})
  250. //关键字匹配
  251. //shouldMap := map[string]interface{}{
  252. // "should": []interface{}{
  253. // map[string]interface{}{
  254. // "match": map[string]interface{}{
  255. // "EdbCode": keywordStr,
  256. // //"Frequency.keyword": "月度",
  257. // },
  258. // },
  259. // map[string]interface{}{
  260. // "match": map[string]interface{}{
  261. // "EdbName": keywordStr,
  262. // //"Frequency.keyword": "月度",
  263. // },
  264. // },
  265. // map[string]interface{}{
  266. // "match": map[string]interface{}{
  267. // "EdbNameEn": keywordStr,
  268. // //"Frequency.keyword": "月度",
  269. // },
  270. // },
  271. // },
  272. //}
  273. // 默认使用中文名字字段去匹配
  274. keywordNameKey := `EdbName`
  275. // 如果没有中文,则使用英文名称字段去匹配
  276. if !utils.ContainsChinese(keywordStr) {
  277. keywordNameKey = `EdbNameEn`
  278. }
  279. shouldMap := map[string]interface{}{
  280. "should": []interface{}{
  281. map[string]interface{}{
  282. "match": map[string]interface{}{
  283. "EdbCode": keywordStr,
  284. //"Frequency.keyword": "月度",
  285. },
  286. },
  287. map[string]interface{}{
  288. "match": map[string]interface{}{
  289. keywordNameKey: keywordStr,
  290. //"Frequency.keyword": "月度",
  291. },
  292. },
  293. },
  294. }
  295. //mustMap = append(mustMap, map[string]interface{}{
  296. // "bool": shouldMap,
  297. //})
  298. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  299. }
  300. // SearchEdbInfoData 查询es中的指标数据
  301. func SearchEdbInfoDataByfrequency(indexName, keywordStr string, from, size int, edbInfoType int8, frequency []string, noPermissionEdbInfoIdList []int) (total int64, list []*data_manage.EdbInfoList, err error) {
  302. list = make([]*data_manage.EdbInfoList, 0)
  303. defer func() {
  304. if err != nil {
  305. fmt.Println("EsAddOrEditData Err:", err.Error())
  306. }
  307. }()
  308. mustMap := make([]interface{}, 0)
  309. mustNotMap := make([]interface{}, 0)
  310. if len(frequency) != 0 {
  311. mustMap = append(mustMap, map[string]interface{}{
  312. "terms": map[string]interface{}{
  313. "Frequency.keyword": frequency,
  314. //"Frequency.keyword": "月度",
  315. },
  316. })
  317. }
  318. // noPermissionEdbInfoIdList 无权限指标id
  319. if len(noPermissionEdbInfoIdList) > 0 {
  320. mustNotMap = append(mustNotMap, map[string]interface{}{
  321. "terms": map[string]interface{}{
  322. "EdbInfoId": noPermissionEdbInfoIdList,
  323. //"Frequency.keyword": "月度",
  324. },
  325. })
  326. }
  327. // 指标类型:普通指标、预测指标(小于0 代表不区分指标是普通还是预测)
  328. if edbInfoType >= 0 {
  329. mustMap = append(mustMap, map[string]interface{}{
  330. "term": map[string]interface{}{
  331. "EdbInfoType": edbInfoType,
  332. },
  333. })
  334. }
  335. // 默认使用中文名字字段去匹配
  336. keywordNameKey := `EdbName`
  337. // 如果没有中文,则使用英文名称字段去匹配
  338. if !utils.ContainsChinese(keywordStr) {
  339. keywordNameKey = `EdbNameEn`
  340. }
  341. shouldMap := map[string]interface{}{
  342. "should": []interface{}{
  343. map[string]interface{}{
  344. "match": map[string]interface{}{
  345. "EdbCode": keywordStr,
  346. //"Frequency.keyword": "月度",
  347. },
  348. },
  349. map[string]interface{}{
  350. "match": map[string]interface{}{
  351. keywordNameKey: keywordStr,
  352. //"Frequency.keyword": "月度",
  353. },
  354. },
  355. },
  356. }
  357. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  358. }
  359. func SearchEdbInfoDataBak(indexName, keywordStr string, from, size, filterSource, source int, frequency string) (total int64, list []*data_manage.EdbInfoList, err error) {
  360. list = make([]*data_manage.EdbInfoList, 0)
  361. defer func() {
  362. if err != nil {
  363. fmt.Println("EsAddOrEditData Err:", err.Error())
  364. }
  365. }()
  366. client := utils.EsClient
  367. //queryString := elastic.NewQueryStringQuery(keywordStr)
  368. //boolQueryJson, err := json.Marshal(queryString)
  369. //if err != nil {
  370. // fmt.Println("boolQueryJson err:", err)
  371. //} else {
  372. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  373. //}
  374. highlight := elastic.NewHighlight()
  375. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  376. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  377. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  378. //var source map[string]interface{}
  379. //source := map[string]interface{}{
  380. // "query": map[string]interface{}{
  381. // "match_all": map[string]interface{}{},
  382. // },
  383. //}
  384. mustMap := make([]interface{}, 0)
  385. mustNotMap := make([]interface{}, 0)
  386. //source := map[string]interface{}{
  387. // "query": map[string]interface{}{
  388. // "bool": map[string]interface{}{
  389. // "must": map[string]interface{}{
  390. // "query_string": map[string]interface{}{
  391. // "query": keywordStr,
  392. // "fields": []string{"EdbCode", "EdbName"},
  393. // },
  394. // },
  395. // },
  396. // },
  397. //}
  398. switch filterSource {
  399. case 2:
  400. //source = map[string]interface{}{
  401. // "query": map[string]interface{}{
  402. // "bool": map[string]interface{}{
  403. // "must": map[string]interface{}{
  404. // "query_string": map[string]interface{}{
  405. // "query": keywordStr,
  406. // },
  407. // },
  408. // "filter": []interface{}{
  409. // map[string]interface{}{
  410. // "term": map[string]interface{}{
  411. // "Frequency.keyword": "月度",
  412. // },
  413. // }},
  414. // },
  415. // },
  416. //}
  417. mustMap = []interface{}{
  418. map[string]interface{}{
  419. "term": map[string]interface{}{
  420. "Frequency.keyword": "月度",
  421. //"Frequency.keyword": "月度",
  422. },
  423. },
  424. }
  425. case 3:
  426. //source = map[string]interface{}{
  427. // "query": map[string]interface{}{
  428. // "bool": map[string]interface{}{
  429. // "must": map[string]interface{}{
  430. // "query_string": map[string]interface{}{
  431. // "query": keywordStr,
  432. // },
  433. // },
  434. // "must_not": []interface{}{
  435. // map[string]interface{}{
  436. // "match": map[string]interface{}{
  437. // "Frequency.keyword": "日度",
  438. // },
  439. // }},
  440. // },
  441. // },
  442. //}
  443. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  444. //mustNotMap = []interface{}{
  445. // map[string]interface{}{
  446. // "match": map[string]interface{}{
  447. // "Frequency.keyword": "日度",
  448. // //"Frequency.keyword": "月度",
  449. // },
  450. // },
  451. //}
  452. case 4:
  453. //source = map[string]interface{}{
  454. // "query": map[string]interface{}{
  455. // "bool": map[string]interface{}{
  456. // "must": map[string]interface{}{
  457. // "query_string": map[string]interface{}{
  458. // "query": keywordStr,
  459. // },
  460. // },
  461. // "filter": []interface{}{
  462. // map[string]interface{}{
  463. // "term": map[string]interface{}{
  464. // "EdbType": 1,
  465. // },
  466. // }},
  467. // },
  468. // },
  469. //}
  470. mustMap = []interface{}{
  471. map[string]interface{}{
  472. "term": map[string]interface{}{
  473. "EdbType": 1,
  474. },
  475. },
  476. }
  477. case 5:
  478. mustMap = []interface{}{
  479. map[string]interface{}{
  480. "term": map[string]interface{}{
  481. "Source": 6,
  482. },
  483. },
  484. }
  485. case 6:
  486. mustNotMap = []interface{}{
  487. map[string]interface{}{
  488. "match": map[string]interface{}{
  489. "Frequency.keyword": "年度",
  490. },
  491. },
  492. }
  493. }
  494. //指标来源
  495. if source > 0 {
  496. mustMap = append(mustMap, map[string]interface{}{
  497. "term": map[string]interface{}{
  498. "Source": source,
  499. //"Frequency.keyword": "月度",
  500. },
  501. })
  502. }
  503. if frequency != "" {
  504. mustMap = append(mustMap, map[string]interface{}{
  505. "term": map[string]interface{}{
  506. "Frequency.keyword": frequency,
  507. //"Frequency.keyword": "月度",
  508. },
  509. })
  510. }
  511. //普通指标
  512. //mustMap = append(mustMap, map[string]interface{}{
  513. // "term": map[string]interface{}{
  514. // "EdbInfoType": 0,
  515. // //"Frequency.keyword": "月度",
  516. // },
  517. //})
  518. //关键字匹配
  519. shouldMap := map[string]interface{}{
  520. "should": []interface{}{
  521. map[string]interface{}{
  522. "match": map[string]interface{}{
  523. "EdbCode": keywordStr,
  524. //"Frequency.keyword": "月度",
  525. },
  526. },
  527. map[string]interface{}{
  528. "match": map[string]interface{}{
  529. "EdbName": keywordStr,
  530. //"Frequency.keyword": "月度",
  531. },
  532. },
  533. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  534. map[string]interface{}{
  535. "match": map[string]interface{}{
  536. "EdbCode": map[string]interface{}{
  537. "query": keywordStr,
  538. "operator": "and",
  539. },
  540. //"Frequency.keyword": "月度",
  541. },
  542. },
  543. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  544. map[string]interface{}{
  545. "match": map[string]interface{}{
  546. "EdbName": map[string]interface{}{
  547. "query": keywordStr,
  548. "operator": "and",
  549. },
  550. //"Frequency.keyword": "月度",
  551. },
  552. },
  553. },
  554. }
  555. mustMap = append(mustMap, map[string]interface{}{
  556. "bool": shouldMap,
  557. })
  558. queryMap := map[string]interface{}{
  559. "query": map[string]interface{}{
  560. "bool": map[string]interface{}{
  561. "must": mustMap,
  562. "must_not": mustNotMap,
  563. //"should": shouldMap,
  564. },
  565. },
  566. }
  567. //根据条件数量统计
  568. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  569. total, err = requestTotalHits.Do(context.Background())
  570. if err != nil {
  571. return
  572. }
  573. queryMap["from"] = from
  574. queryMap["size"] = size
  575. jsonBytes, _ := json.Marshal(queryMap)
  576. fmt.Println(string(jsonBytes))
  577. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  578. //switch filterSource {
  579. //case 2:
  580. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  581. //case 3:
  582. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  583. //case 4:
  584. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  585. //}
  586. //queryString := elastic.RawStringQuery(queryStr)
  587. //fmt.Println("queryString:", queryString)
  588. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  589. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  590. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  591. //requestJson, err := json.Marshal(request)
  592. //if err != nil {
  593. // fmt.Println("requestJson err:", err)
  594. //}
  595. //fmt.Println("requestJson ", string(requestJson))
  596. searchMap := make(map[string]string)
  597. searchResp, err := request.Do(context.Background())
  598. if err != nil {
  599. return
  600. }
  601. fmt.Println(searchResp)
  602. fmt.Println(searchResp.Status)
  603. if searchResp.Status != 0 {
  604. return
  605. }
  606. if searchResp.Hits != nil {
  607. for _, v := range searchResp.Hits.Hits {
  608. if _, ok := searchMap[v.Id]; !ok {
  609. itemJson, tmpErr := v.Source.MarshalJSON()
  610. if tmpErr != nil {
  611. err = tmpErr
  612. fmt.Println("movieJson err:", err)
  613. return
  614. }
  615. edbInfoItem := new(data_manage.EdbInfoList)
  616. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  617. if tmpErr != nil {
  618. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  619. err = tmpErr
  620. return
  621. }
  622. if len(v.Highlight["EdbCode"]) > 0 {
  623. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  624. }
  625. if len(v.Highlight["EdbName"]) > 0 {
  626. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  627. }
  628. list = append(list, edbInfoItem)
  629. searchMap[v.Id] = v.Id
  630. }
  631. }
  632. }
  633. //for _, v := range result {
  634. // fmt.Println(v)
  635. //}
  636. return
  637. }
  638. // SearchAddPredictEdbInfoData 查询允许添加预测指标的数据
  639. func SearchAddPredictEdbInfoData(indexName, keywordStr string, noPermissionEdbInfoIdList []int, from, size, sysUserId int) (total int64, list []*data_manage.EdbInfoList, err error) {
  640. list = make([]*data_manage.EdbInfoList, 0)
  641. defer func() {
  642. if err != nil {
  643. fmt.Println("EsAddOrEditData Err:", err.Error())
  644. }
  645. }()
  646. highlight := elastic.NewHighlight()
  647. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  648. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  649. mustMap := make([]interface{}, 0)
  650. mustNotMap := make([]interface{}, 0)
  651. mustNotMap = []interface{}{
  652. //map[string]interface{}{
  653. // "terms": map[string]interface{}{
  654. // "Frequency.keyword": []string{"日度", "周度", "月度"},
  655. // },
  656. // //"match": map[string]interface{}{
  657. // // "Frequency": []string{"日度", "周度", "月度"},
  658. // // //"Frequency.keyword": []string{"日度", "周度", "月度"},
  659. // //},
  660. //},
  661. }
  662. // 指标类型:普通指标、预算指标
  663. mustMap = append(mustMap, map[string]interface{}{
  664. "term": map[string]interface{}{
  665. "EdbInfoType": 0,
  666. },
  667. })
  668. mustMap = append(mustMap, map[string]interface{}{
  669. "terms": map[string]interface{}{
  670. "Frequency.keyword": []string{"日度", "周度", "月度"},
  671. },
  672. })
  673. // 只看我的
  674. if sysUserId > 0 {
  675. mustMap = append(mustMap, map[string]interface{}{
  676. "terms": map[string]interface{}{
  677. "SysUserId": sysUserId,
  678. },
  679. })
  680. }
  681. //关键字匹配
  682. //shouldMap := map[string]interface{}{
  683. // "should": []interface{}{
  684. // map[string]interface{}{
  685. // "match": map[string]interface{}{
  686. // "EdbCode": keywordStr,
  687. // //"Frequency.keyword": "月度",
  688. // },
  689. // },
  690. // map[string]interface{}{
  691. // "match": map[string]interface{}{
  692. // "EdbName": keywordStr,
  693. // //"Frequency.keyword": "月度",
  694. // },
  695. // },
  696. // map[string]interface{}{
  697. // "match": map[string]interface{}{
  698. // "EdbNameEn": keywordStr,
  699. // //"Frequency.keyword": "月度",
  700. // },
  701. // },
  702. // },
  703. //}
  704. // 默认使用中文名字字段去匹配
  705. keywordNameKey := `EdbName`
  706. // 如果没有中文,则使用英文名称字段去匹配
  707. if !utils.ContainsChinese(keywordStr) {
  708. keywordNameKey = `EdbNameEn`
  709. }
  710. shouldMap := map[string]interface{}{
  711. "should": []interface{}{
  712. map[string]interface{}{
  713. "match": map[string]interface{}{
  714. "EdbCode": keywordStr,
  715. //"Frequency.keyword": "月度",
  716. },
  717. },
  718. map[string]interface{}{
  719. "match": map[string]interface{}{
  720. keywordNameKey: keywordStr,
  721. //"Frequency.keyword": "月度",
  722. },
  723. },
  724. },
  725. }
  726. // noPermissionEdbInfoIdList 无权限指标id
  727. if len(noPermissionEdbInfoIdList) > 0 {
  728. mustNotMap = append(mustNotMap, map[string]interface{}{
  729. "terms": map[string]interface{}{
  730. "EdbInfoId": noPermissionEdbInfoIdList,
  731. //"Frequency.keyword": "月度",
  732. },
  733. })
  734. }
  735. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  736. }
  737. // searchEdbInfoData 查询es中的指标数据
  738. func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMap map[string]interface{}, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  739. list = make([]*data_manage.EdbInfoList, 0)
  740. defer func() {
  741. if err != nil {
  742. fmt.Println("EsAddOrEditData Err:", err.Error())
  743. }
  744. }()
  745. client := utils.EsClient
  746. //queryString := elastic.NewQueryStringQuery(keywordStr)
  747. //boolQueryJson, err := json.Marshal(queryString)
  748. //if err != nil {
  749. // fmt.Println("boolQueryJson err:", err)
  750. //} else {
  751. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  752. //}
  753. highlight := elastic.NewHighlight()
  754. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  755. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  756. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  757. //关键字匹配
  758. mustMap = append(mustMap, map[string]interface{}{
  759. "bool": shouldMap,
  760. })
  761. queryMap := map[string]interface{}{
  762. "query": map[string]interface{}{
  763. "bool": map[string]interface{}{
  764. "must": mustMap,
  765. "must_not": mustNotMap,
  766. //"should": shouldMap,
  767. },
  768. },
  769. }
  770. //根据条件数量统计
  771. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  772. total, err = requestTotalHits.Do(context.Background())
  773. if err != nil {
  774. return
  775. }
  776. queryMap["from"] = from
  777. queryMap["size"] = size
  778. queryMap["highlight"] = map[string]interface{}{
  779. "fields": map[string]interface{}{
  780. "EdbName": map[string]interface{}{},
  781. },
  782. "pre_tags": "<span style=\"color:#0052D9\">",
  783. "post_tags": "</span>",
  784. }
  785. jsonBytes, _ := json.Marshal(queryMap)
  786. fmt.Println(string(jsonBytes))
  787. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  788. //switch filterSource {
  789. //case 2:
  790. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  791. //case 3:
  792. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  793. //case 4:
  794. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  795. //}
  796. //queryString := elastic.RawStringQuery(queryStr)
  797. //fmt.Println("queryString:", queryString)
  798. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  799. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  800. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  801. //requestJson, err := json.Marshal(request)
  802. //if err != nil {
  803. // fmt.Println("requestJson err:", err)
  804. //}
  805. //fmt.Println("requestJson ", string(requestJson))
  806. searchMap := make(map[string]string)
  807. searchResp, err := request.Do(context.Background())
  808. if err != nil {
  809. return
  810. }
  811. fmt.Println(searchResp)
  812. fmt.Println(searchResp.Status)
  813. if searchResp.Status != 0 {
  814. return
  815. }
  816. //total = searchResp.TotalHits()
  817. if searchResp.Hits != nil {
  818. for _, v := range searchResp.Hits.Hits {
  819. if _, ok := searchMap[v.Id]; !ok {
  820. itemJson, tmpErr := v.Source.MarshalJSON()
  821. if tmpErr != nil {
  822. err = tmpErr
  823. fmt.Println("movieJson err:", err)
  824. return
  825. }
  826. edbInfoItem := new(data_manage.EdbInfoList)
  827. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  828. if tmpErr != nil {
  829. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  830. err = tmpErr
  831. return
  832. }
  833. if len(v.Highlight["EdbCode"]) > 0 {
  834. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  835. }
  836. if len(v.Highlight["EdbName"]) > 0 {
  837. // 搜索结果高亮用新字段,原EdbName直接高亮展示上会有点影响
  838. edbInfoItem.SearchText = v.Highlight["EdbName"][0]
  839. //edbInfoItem.EdbName = v.Highlight["EdbName"][0]
  840. } else {
  841. edbInfoItem.SearchText = edbInfoItem.EdbName
  842. }
  843. list = append(list, edbInfoItem)
  844. searchMap[v.Id] = v.Id
  845. }
  846. }
  847. }
  848. return
  849. }
  850. // EsDeleteEdbInfoData 删除es中的指标数据
  851. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  852. defer func() {
  853. if err != nil {
  854. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  855. }
  856. }()
  857. client := utils.EsClient
  858. resp, err := client.Delete().Index(indexName).Id(docId).Refresh(`true`).Do(context.Background())
  859. fmt.Println(resp)
  860. if err != nil {
  861. return
  862. }
  863. if resp.Status == 0 {
  864. fmt.Println("删除成功")
  865. } else {
  866. fmt.Println("AddData", resp.Status, resp.Result)
  867. }
  868. return
  869. }
  870. // EsAddOrEditReport 新增编辑es报告
  871. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  872. defer func() {
  873. if err != nil {
  874. fmt.Println("EsAddOrEditReport Err:", err.Error())
  875. }
  876. }()
  877. client := utils.EsClient
  878. // docId为报告ID+章节ID
  879. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  880. if err != nil && !strings.Contains(err.Error(), "404") {
  881. fmt.Println("Get Err" + err.Error())
  882. return
  883. }
  884. if searchById != nil && searchById.Found {
  885. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  886. "ReportId": item.ReportId,
  887. "ReportChapterId": item.ReportChapterId,
  888. "Title": item.Title,
  889. "Abstract": item.Abstract,
  890. "BodyContent": item.BodyContent,
  891. "PublishTime": item.PublishTime,
  892. "PublishState": item.PublishState,
  893. "Author": item.Author,
  894. "ClassifyIdFirst": item.ClassifyIdFirst,
  895. "ClassifyNameFirst": item.ClassifyNameFirst,
  896. "ClassifyIdSecond": item.ClassifyIdSecond,
  897. "ClassifyNameSecond": item.ClassifyNameSecond,
  898. "Categories": item.Categories,
  899. "StageStr": item.StageStr,
  900. }).Do(context.Background())
  901. if err != nil {
  902. return err
  903. }
  904. //fmt.Println(resp.Status, resp.Result)
  905. if resp.Status == 0 {
  906. fmt.Println("修改成功" + docId)
  907. err = nil
  908. } else {
  909. fmt.Println("EditData", resp.Status, resp.Result)
  910. }
  911. } else {
  912. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  913. if err != nil {
  914. fmt.Println("新增失败:", err.Error())
  915. return err
  916. }
  917. if resp.Status == 0 && resp.Result == "created" {
  918. fmt.Println("新增成功" + docId)
  919. return nil
  920. } else {
  921. fmt.Println("AddData", resp.Status, resp.Result)
  922. }
  923. }
  924. return
  925. }
  926. // AnalyzeResp 分词接口返回结构体
  927. type AnalyzeResp struct {
  928. Tokens []struct {
  929. EndOffset int64 `json:"end_offset"`
  930. Position int64 `json:"position"`
  931. StartOffset int64 `json:"start_offset"`
  932. Token string `json:"token"`
  933. Type string `json:"type"`
  934. } `json:"tokens"`
  935. }
  936. // Analyze 根据输入的文字获取分词后的文字
  937. func Analyze(content string) (contentList []string, err error) {
  938. defer func() {
  939. if err != nil {
  940. fmt.Println("Analyze Err:", err.Error())
  941. }
  942. }()
  943. client := utils.EsClient
  944. queryMap := map[string]string{
  945. "text": content,
  946. "analyzer": "ik_max_word",
  947. }
  948. res, err := client.PerformRequest(
  949. context.Background(),
  950. elastic.PerformRequestOptions{
  951. Method: "GET",
  952. Path: "/_analyze",
  953. Body: queryMap,
  954. Stream: false,
  955. },
  956. )
  957. if res.StatusCode == 200 {
  958. var analyzeResp AnalyzeResp
  959. tmpErr := json.Unmarshal(res.Body, &analyzeResp)
  960. if tmpErr != nil {
  961. err = errors.New("返回数据转结构体失败:" + tmpErr.Error())
  962. return
  963. }
  964. for _, v := range analyzeResp.Tokens {
  965. contentList = append(contentList, v.Token)
  966. }
  967. } else {
  968. err = errors.New("分词失败,返回code异常:" + strconv.Itoa(res.StatusCode))
  969. }
  970. return
  971. }
  972. // EsAddOrEditChartInfoData 新增/修改es中的图表数据
  973. func EsAddOrEditChartInfoData(indexName, docId string, item *data_manage.ChartInfo) (err error) {
  974. defer func() {
  975. if err != nil {
  976. fmt.Println("EsAddOrEditData Err:", err.Error())
  977. }
  978. }()
  979. client := utils.EsClient
  980. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  981. if err != nil {
  982. fmt.Println("新增失败:", err.Error())
  983. return err
  984. }
  985. fmt.Println(resp)
  986. if resp.Status == 0 {
  987. fmt.Println("新增成功", resp.Result)
  988. err = nil
  989. } else {
  990. fmt.Println("AddData", resp.Status, resp.Result)
  991. }
  992. return
  993. }
  994. // EsDeleteDataV2 删除es中的数据
  995. func EsDeleteDataV2(indexName, docId string) (err error) {
  996. defer func() {
  997. if err != nil {
  998. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  999. }
  1000. }()
  1001. client := utils.EsClient
  1002. resp, err := client.Delete().Index(indexName).Id(docId).Refresh("true").Do(context.Background())
  1003. fmt.Println(resp)
  1004. if err != nil {
  1005. return
  1006. }
  1007. if resp.Status == 0 {
  1008. fmt.Println("删除成功")
  1009. } else {
  1010. fmt.Println("AddData", resp.Status, resp.Result)
  1011. }
  1012. return
  1013. }
  1014. // SearchChartInfoData 查询es中的图表数据
  1015. func SearchChartInfoData(indexName, keywordStr string, showSysId int, sourceList []int, noPermissionChartIdList []int, from, size int, chartClassifyIds []int) (list []*data_manage.ChartInfoMore, total int64, err error) {
  1016. list = make([]*data_manage.ChartInfoMore, 0)
  1017. defer func() {
  1018. if err != nil {
  1019. fmt.Println("EsAddOrEditData Err:", err.Error())
  1020. }
  1021. }()
  1022. client := utils.EsClient
  1023. //queryString := elastic.NewQueryStringQuery(keywordStr)
  1024. //boolQueryJson, err := json.Marshal(queryString)
  1025. //if err != nil {
  1026. // fmt.Println("boolQueryJson err:", err)
  1027. //} else {
  1028. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  1029. //}
  1030. highlight := elastic.NewHighlight()
  1031. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  1032. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1033. mustMap := make([]interface{}, 0)
  1034. mustNotMap := make([]interface{}, 0)
  1035. //指标来源
  1036. if showSysId > 0 {
  1037. mustMap = append(mustMap, map[string]interface{}{
  1038. "term": map[string]interface{}{
  1039. "SysUserId": showSysId,
  1040. //"Frequency.keyword": "月度",
  1041. },
  1042. })
  1043. }
  1044. mustMap = append(mustMap, map[string]interface{}{
  1045. "terms": map[string]interface{}{
  1046. "Source": sourceList,
  1047. },
  1048. })
  1049. //关键字匹配
  1050. //shouldMap := map[string]interface{}{
  1051. // "should": []interface{}{
  1052. // map[string]interface{}{
  1053. // "match": map[string]interface{}{
  1054. // "ChartName": keywordStr,
  1055. // //"Frequency.keyword": "月度",
  1056. // },
  1057. // },
  1058. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1059. // map[string]interface{}{
  1060. // "match": map[string]interface{}{
  1061. // "ChartName": map[string]interface{}{
  1062. // "query": keywordStr,
  1063. // "operator": "and",
  1064. // },
  1065. // //"Frequency.keyword": "月度",
  1066. // },
  1067. // },
  1068. // map[string]interface{}{
  1069. // "match": map[string]interface{}{
  1070. // "ChartNameEn": keywordStr,
  1071. // //"Frequency.keyword": "月度",
  1072. // },
  1073. // },
  1074. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1075. // map[string]interface{}{
  1076. // "match": map[string]interface{}{
  1077. // "ChartNameEn": map[string]interface{}{
  1078. // "query": keywordStr,
  1079. // "operator": "and",
  1080. // },
  1081. // //"Frequency.keyword": "月度",
  1082. // },
  1083. // },
  1084. // },
  1085. //}
  1086. // 默认使用中文名字字段去匹配
  1087. keywordNameKey := `ChartName`
  1088. // 如果没有中文,则使用英文名称字段去匹配
  1089. if !utils.ContainsChinese(keywordStr) {
  1090. keywordNameKey = `ChartNameEn`
  1091. }
  1092. shouldMap := map[string]interface{}{
  1093. "should": []interface{}{
  1094. map[string]interface{}{
  1095. "match": map[string]interface{}{
  1096. keywordNameKey: keywordStr,
  1097. //"Frequency.keyword": "月度",
  1098. },
  1099. },
  1100. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1101. map[string]interface{}{
  1102. "match": map[string]interface{}{
  1103. keywordNameKey: map[string]interface{}{
  1104. "query": keywordStr,
  1105. "operator": "and",
  1106. },
  1107. //"Frequency.keyword": "月度",
  1108. },
  1109. },
  1110. },
  1111. }
  1112. mustMap = append(mustMap, map[string]interface{}{
  1113. "bool": shouldMap,
  1114. })
  1115. // noPermissionEdbInfoIdList 无权限指标id
  1116. if len(noPermissionChartIdList) > 0 {
  1117. mustNotMap = append(mustNotMap, map[string]interface{}{
  1118. "terms": map[string]interface{}{
  1119. "ChartInfoId": noPermissionChartIdList,
  1120. //"Frequency.keyword": "月度",
  1121. },
  1122. })
  1123. }
  1124. if len(chartClassifyIds) > 0 {
  1125. mustMap = append(mustMap, map[string]interface{}{
  1126. "terms": map[string]interface{}{
  1127. "ChartClassifyId": chartClassifyIds,
  1128. },
  1129. })
  1130. }
  1131. queryMap := map[string]interface{}{
  1132. "query": map[string]interface{}{
  1133. "bool": map[string]interface{}{
  1134. "must": mustMap,
  1135. "must_not": mustNotMap,
  1136. //"should": shouldMap,
  1137. },
  1138. },
  1139. }
  1140. //根据条件数量统计
  1141. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1142. total, err = requestTotalHits.Do(context.Background())
  1143. if err != nil {
  1144. return
  1145. }
  1146. // 分页查询
  1147. queryMap["from"] = from
  1148. queryMap["size"] = size
  1149. queryMap["highlight"] = map[string]interface{}{
  1150. "fields": map[string]interface{}{
  1151. keywordNameKey: map[string]interface{}{},
  1152. },
  1153. "pre_tags": "<span style=\"color:#0052D9\">",
  1154. "post_tags": "</span>",
  1155. }
  1156. jsonBytes, _ := json.Marshal(queryMap)
  1157. fmt.Println(string(jsonBytes))
  1158. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  1159. //requestJson, err := json.Marshal(request)
  1160. //if err != nil {
  1161. // fmt.Println("requestJson err:", err)
  1162. //}
  1163. //fmt.Println("requestJson ", string(requestJson))
  1164. searchMap := make(map[string]string)
  1165. searchResp, err := request.Do(context.Background())
  1166. if err != nil {
  1167. return
  1168. }
  1169. fmt.Println(searchResp)
  1170. fmt.Println(searchResp.Status)
  1171. if searchResp.Status != 0 {
  1172. return
  1173. }
  1174. if searchResp.Hits != nil {
  1175. for _, v := range searchResp.Hits.Hits {
  1176. if _, ok := searchMap[v.Id]; !ok {
  1177. itemJson, tmpErr := v.Source.MarshalJSON()
  1178. if tmpErr != nil {
  1179. err = tmpErr
  1180. fmt.Println("movieJson err:", err)
  1181. return
  1182. }
  1183. chartInfoItem := new(data_manage.ChartInfoMore)
  1184. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  1185. if err != nil {
  1186. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  1187. err = tmpErr
  1188. return
  1189. }
  1190. if len(v.Highlight[keywordNameKey]) > 0 {
  1191. //chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  1192. chartInfoItem.SearchText = v.Highlight[keywordNameKey][0]
  1193. } else {
  1194. chartInfoItem.SearchText = chartInfoItem.ChartName
  1195. }
  1196. list = append(list, chartInfoItem)
  1197. searchMap[v.Id] = v.Id
  1198. }
  1199. }
  1200. }
  1201. //for _, v := range result {
  1202. // fmt.Println(v)
  1203. //}
  1204. return
  1205. }
  1206. // EsAddOrEditDataInterface 新增/修改es中的数据
  1207. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  1208. defer func() {
  1209. if err != nil {
  1210. fmt.Println("EsAddOrEditData Err:", err.Error())
  1211. }
  1212. }()
  1213. client := utils.EsClient
  1214. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  1215. if err != nil {
  1216. fmt.Println("新增失败:", err.Error())
  1217. return err
  1218. }
  1219. fmt.Println(resp)
  1220. if resp.Status == 0 {
  1221. fmt.Println("新增成功", resp.Result)
  1222. err = nil
  1223. } else {
  1224. fmt.Println("AddData", resp.Status, resp.Result)
  1225. }
  1226. return
  1227. }
  1228. // SearchMyChartInfoData 查询es中的我的图表数据
  1229. func SearchMyChartInfoData(indexName, keywordStr string, adminId int, noPermissionChartIdList []int, from, size int) (list []*data_manage.MyChartList, total int64, err error) {
  1230. list = make([]*data_manage.MyChartList, 0)
  1231. defer func() {
  1232. if err != nil {
  1233. fmt.Println("EsAddOrEditData Err:", err.Error())
  1234. }
  1235. }()
  1236. client := utils.EsClient
  1237. //queryString := elastic.NewQueryStringQuery(keywordStr)
  1238. //boolQueryJson, err := json.Marshal(queryString)
  1239. //if err != nil {
  1240. // fmt.Println("boolQueryJson err:", err)
  1241. //} else {
  1242. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  1243. //}
  1244. highlight := elastic.NewHighlight()
  1245. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  1246. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1247. mustMap := make([]interface{}, 0)
  1248. mustNotMap := make([]interface{}, 0)
  1249. //指标来源
  1250. if adminId > 0 {
  1251. mustMap = append(mustMap, map[string]interface{}{
  1252. "term": map[string]interface{}{
  1253. "AdminId": adminId,
  1254. //"Frequency.keyword": "月度",
  1255. },
  1256. })
  1257. }
  1258. //关键字匹配
  1259. //shouldMap := map[string]interface{}{
  1260. // "should": []interface{}{
  1261. // map[string]interface{}{
  1262. // "match": map[string]interface{}{
  1263. // "ChartName": keywordStr,
  1264. // //"Frequency.keyword": "月度",
  1265. // },
  1266. // },
  1267. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1268. // map[string]interface{}{
  1269. // "match": map[string]interface{}{
  1270. // "ChartName": map[string]interface{}{
  1271. // "query": keywordStr,
  1272. // "operator": "and",
  1273. // },
  1274. // //"Frequency.keyword": "月度",
  1275. // },
  1276. // },
  1277. // map[string]interface{}{
  1278. // "match": map[string]interface{}{
  1279. // "ChartNameEn": keywordStr,
  1280. // //"Frequency.keyword": "月度",
  1281. // },
  1282. // },
  1283. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1284. // map[string]interface{}{
  1285. // "match": map[string]interface{}{
  1286. // "ChartNameEn": map[string]interface{}{
  1287. // "query": keywordStr,
  1288. // "operator": "and",
  1289. // },
  1290. // //"Frequency.keyword": "月度",
  1291. // },
  1292. // },
  1293. // },
  1294. //}
  1295. // 默认使用中文名字字段去匹配
  1296. keywordNameKey := `ChartName`
  1297. // 如果没有中文,则使用英文名称字段去匹配
  1298. if !utils.ContainsChinese(keywordStr) {
  1299. keywordNameKey = `ChartNameEn`
  1300. }
  1301. shouldMap := map[string]interface{}{
  1302. "should": []interface{}{
  1303. map[string]interface{}{
  1304. "match": map[string]interface{}{
  1305. keywordNameKey: keywordStr,
  1306. //"Frequency.keyword": "月度",
  1307. },
  1308. },
  1309. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1310. map[string]interface{}{
  1311. "match": map[string]interface{}{
  1312. keywordNameKey: map[string]interface{}{
  1313. "query": keywordStr,
  1314. "operator": "and",
  1315. },
  1316. //"Frequency.keyword": "月度",
  1317. },
  1318. },
  1319. },
  1320. }
  1321. mustMap = append(mustMap, map[string]interface{}{
  1322. "bool": shouldMap,
  1323. })
  1324. // noPermissionEdbInfoIdList 无权限指标id
  1325. if len(noPermissionChartIdList) > 0 {
  1326. mustNotMap = append(mustNotMap, map[string]interface{}{
  1327. "terms": map[string]interface{}{
  1328. "ChartInfoId": noPermissionChartIdList,
  1329. //"Frequency.keyword": "月度",
  1330. },
  1331. })
  1332. }
  1333. queryMap := map[string]interface{}{
  1334. "query": map[string]interface{}{
  1335. "bool": map[string]interface{}{
  1336. "must": mustMap,
  1337. "must_not": mustNotMap,
  1338. //"should": shouldMap,
  1339. },
  1340. },
  1341. }
  1342. //根据条件数量统计
  1343. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1344. total, err = requestTotalHits.Do(context.Background())
  1345. if err != nil {
  1346. return
  1347. }
  1348. // 分页查询
  1349. queryMap["from"] = from
  1350. queryMap["size"] = size
  1351. queryMap["highlight"] = map[string]interface{}{
  1352. "fields": map[string]interface{}{
  1353. keywordNameKey: map[string]interface{}{},
  1354. },
  1355. "pre_tags": "<span style=\"color:#0052D9\">",
  1356. "post_tags": "</span>",
  1357. }
  1358. jsonBytes, _ := json.Marshal(queryMap)
  1359. fmt.Println(string(jsonBytes))
  1360. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  1361. //requestJson, err := json.Marshal(request)
  1362. //if err != nil {
  1363. // fmt.Println("requestJson err:", err)
  1364. //}
  1365. //fmt.Println("requestJson ", string(requestJson))
  1366. searchMap := make(map[string]string)
  1367. searchResp, err := request.Do(context.Background())
  1368. if err != nil {
  1369. return
  1370. }
  1371. fmt.Println(searchResp)
  1372. fmt.Println(searchResp.Status)
  1373. if searchResp.Status != 0 {
  1374. return
  1375. }
  1376. if searchResp.Hits != nil {
  1377. for _, v := range searchResp.Hits.Hits {
  1378. if _, ok := searchMap[v.Id]; !ok {
  1379. itemJson, tmpErr := v.Source.MarshalJSON()
  1380. if tmpErr != nil {
  1381. err = tmpErr
  1382. fmt.Println("movieJson err:", err)
  1383. return
  1384. }
  1385. chartInfoItem := new(data_manage.MyChartList)
  1386. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  1387. if err != nil {
  1388. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  1389. err = tmpErr
  1390. return
  1391. }
  1392. if len(v.Highlight[keywordNameKey]) > 0 {
  1393. //chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  1394. chartInfoItem.SearchText = v.Highlight[keywordNameKey][0]
  1395. } else {
  1396. chartInfoItem.SearchText = chartInfoItem.ChartName
  1397. }
  1398. list = append(list, chartInfoItem)
  1399. searchMap[v.Id] = v.Id
  1400. }
  1401. }
  1402. }
  1403. //for _, v := range result {
  1404. // fmt.Println(v)
  1405. //}
  1406. return
  1407. }
  1408. // SearchEdbInfoDataByAdminId 查询es中的指标数据
  1409. func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType uint8, frequency string, adminId int) (total int64, list []*data_manage.EdbInfoList, err error) {
  1410. list = make([]*data_manage.EdbInfoList, 0)
  1411. defer func() {
  1412. if err != nil {
  1413. fmt.Println("EsAddOrEditData Err:", err.Error())
  1414. }
  1415. }()
  1416. highlight := elastic.NewHighlight()
  1417. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  1418. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1419. //var source map[string]interface{}
  1420. //source := map[string]interface{}{
  1421. // "query": map[string]interface{}{
  1422. // "match_all": map[string]interface{}{},
  1423. // },
  1424. //}
  1425. mustMap := make([]interface{}, 0)
  1426. mustNotMap := make([]interface{}, 0)
  1427. //source := map[string]interface{}{
  1428. // "query": map[string]interface{}{
  1429. // "bool": map[string]interface{}{
  1430. // "must": map[string]interface{}{
  1431. // "query_string": map[string]interface{}{
  1432. // "query": keywordStr,
  1433. // "fields": []string{"EdbCode", "EdbName"},
  1434. // },
  1435. // },
  1436. // },
  1437. // },
  1438. //}
  1439. switch filterSource {
  1440. case 2:
  1441. //source = map[string]interface{}{
  1442. // "query": map[string]interface{}{
  1443. // "bool": map[string]interface{}{
  1444. // "must": map[string]interface{}{
  1445. // "query_string": map[string]interface{}{
  1446. // "query": keywordStr,
  1447. // },
  1448. // },
  1449. // "filter": []interface{}{
  1450. // map[string]interface{}{
  1451. // "term": map[string]interface{}{
  1452. // "Frequency.keyword": "月度",
  1453. // },
  1454. // }},
  1455. // },
  1456. // },
  1457. //}
  1458. mustMap = []interface{}{
  1459. map[string]interface{}{
  1460. "term": map[string]interface{}{
  1461. "Frequency.keyword": "月度",
  1462. //"Frequency.keyword": "月度",
  1463. },
  1464. },
  1465. }
  1466. case 3:
  1467. //source = map[string]interface{}{
  1468. // "query": map[string]interface{}{
  1469. // "bool": map[string]interface{}{
  1470. // "must": map[string]interface{}{
  1471. // "query_string": map[string]interface{}{
  1472. // "query": keywordStr,
  1473. // },
  1474. // },
  1475. // "must_not": []interface{}{
  1476. // map[string]interface{}{
  1477. // "match": map[string]interface{}{
  1478. // "Frequency.keyword": "日度",
  1479. // },
  1480. // }},
  1481. // },
  1482. // },
  1483. //}
  1484. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  1485. //mustNotMap = []interface{}{
  1486. // map[string]interface{}{
  1487. // "match": map[string]interface{}{
  1488. // "Frequency.keyword": "日度",
  1489. // //"Frequency.keyword": "月度",
  1490. // },
  1491. // },
  1492. //}
  1493. case 4:
  1494. //source = map[string]interface{}{
  1495. // "query": map[string]interface{}{
  1496. // "bool": map[string]interface{}{
  1497. // "must": map[string]interface{}{
  1498. // "query_string": map[string]interface{}{
  1499. // "query": keywordStr,
  1500. // },
  1501. // },
  1502. // "filter": []interface{}{
  1503. // map[string]interface{}{
  1504. // "term": map[string]interface{}{
  1505. // "EdbType": 1,
  1506. // },
  1507. // }},
  1508. // },
  1509. // },
  1510. //}
  1511. mustMap = []interface{}{
  1512. map[string]interface{}{
  1513. "term": map[string]interface{}{
  1514. "EdbType": 1,
  1515. },
  1516. },
  1517. }
  1518. case 5:
  1519. mustMap = []interface{}{
  1520. map[string]interface{}{
  1521. "term": map[string]interface{}{
  1522. "Source": 6,
  1523. },
  1524. },
  1525. }
  1526. case 6:
  1527. mustNotMap = []interface{}{
  1528. map[string]interface{}{
  1529. "match": map[string]interface{}{
  1530. "Frequency.keyword": "年度",
  1531. },
  1532. },
  1533. }
  1534. }
  1535. //指标来源
  1536. if source > 0 {
  1537. mustMap = append(mustMap, map[string]interface{}{
  1538. "term": map[string]interface{}{
  1539. "Source": source,
  1540. //"Frequency.keyword": "月度",
  1541. },
  1542. })
  1543. }
  1544. if frequency != "" {
  1545. mustMap = append(mustMap, map[string]interface{}{
  1546. "term": map[string]interface{}{
  1547. "Frequency.keyword": frequency,
  1548. //"Frequency.keyword": "月度",
  1549. },
  1550. })
  1551. }
  1552. // 指标类型:普通指标、预算指标
  1553. mustMap = append(mustMap, map[string]interface{}{
  1554. "term": map[string]interface{}{
  1555. "EdbInfoType": edbInfoType,
  1556. },
  1557. })
  1558. //普通指标
  1559. //mustMap = append(mustMap, map[string]interface{}{
  1560. // "term": map[string]interface{}{
  1561. // "EdbInfoType": 0,
  1562. // //"Frequency.keyword": "月度",
  1563. // },
  1564. //})
  1565. //关键字匹配
  1566. //shouldMap := map[string]interface{}{
  1567. // "should": []interface{}{
  1568. // map[string]interface{}{
  1569. // "match": map[string]interface{}{
  1570. // "EdbCode": keywordStr,
  1571. // //"Frequency.keyword": "月度",
  1572. // },
  1573. // },
  1574. // map[string]interface{}{
  1575. // "match": map[string]interface{}{
  1576. // "EdbName": keywordStr,
  1577. // //"Frequency.keyword": "月度",
  1578. // },
  1579. // },
  1580. // },
  1581. //}
  1582. // 默认使用中文名字字段去匹配
  1583. keywordNameKey := `EdbName`
  1584. // 如果没有中文,则使用英文名称字段去匹配
  1585. if !utils.ContainsChinese(keywordStr) {
  1586. keywordNameKey = `EdbNameEn`
  1587. }
  1588. shouldMap := map[string]interface{}{
  1589. "should": []interface{}{
  1590. map[string]interface{}{
  1591. "match": map[string]interface{}{
  1592. "EdbCode": keywordStr,
  1593. //"Frequency.keyword": "月度",
  1594. },
  1595. },
  1596. map[string]interface{}{
  1597. "match": map[string]interface{}{
  1598. keywordNameKey: keywordStr,
  1599. //"Frequency.keyword": "月度",
  1600. },
  1601. },
  1602. },
  1603. }
  1604. mustMap = append(mustMap, map[string]interface{}{
  1605. "bool": shouldMap,
  1606. })
  1607. //创建人
  1608. if adminId > 0 {
  1609. mustMap = append(mustMap, map[string]interface{}{
  1610. "term": map[string]interface{}{
  1611. "SysUserId": adminId,
  1612. },
  1613. })
  1614. }
  1615. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  1616. }
  1617. // EsAddOrEditExcelInfoData 新增/修改es中的表格数据
  1618. func EsAddOrEditExcelInfoData(indexName, docId string, item *excel.ExcelInfo) (err error) {
  1619. defer func() {
  1620. if err != nil {
  1621. tips := fmt.Sprintf("EsAddOrEditExcelInfoData err: %v", err)
  1622. utils.FileLog.Info(tips)
  1623. }
  1624. }()
  1625. client := utils.EsClient
  1626. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  1627. if e != nil {
  1628. err = fmt.Errorf("resp err, %v", e)
  1629. return
  1630. }
  1631. if resp.Status != 0 {
  1632. err = fmt.Errorf("result err, status: %d, result: %s", resp.Status, resp.Result)
  1633. return
  1634. }
  1635. err = nil
  1636. fmt.Println("excel write to es success", resp.Result)
  1637. return
  1638. }
  1639. // SearchExcelInfoData 查询es中的表格
  1640. func SearchExcelInfoData(indexName, keyword string, source, adminId int, queryIds, exceptIds []int, from, size int) (total int64, list []*excel.SearchExcelInfo, err error) {
  1641. list = make([]*excel.SearchExcelInfo, 0)
  1642. defer func() {
  1643. if err != nil {
  1644. tips := fmt.Sprintf("SearchExcelInfoData err: %v", err)
  1645. utils.FileLog.Info(tips)
  1646. }
  1647. }()
  1648. client := utils.EsClient
  1649. mustMap := make([]interface{}, 0)
  1650. mustNotMap := make([]interface{}, 0)
  1651. mustMap = append(mustMap, map[string]interface{}{
  1652. "term": map[string]interface{}{
  1653. "IsDelete": 0,
  1654. },
  1655. })
  1656. // 表格名称
  1657. shouldMap := make(map[string]interface{}, 0)
  1658. if keyword != "" {
  1659. shouldMap["should"] = []interface{}{
  1660. map[string]interface{}{
  1661. "match": map[string]interface{}{
  1662. "ExcelName": keyword,
  1663. },
  1664. },
  1665. }
  1666. //shouldMap := map[string]interface{}{
  1667. // "should": []interface{}{
  1668. // map[string]interface{}{
  1669. // "match": map[string]interface{}{
  1670. // "ExcelName": keyword,
  1671. // },
  1672. // },
  1673. // },
  1674. //}
  1675. }
  1676. // 表格来源
  1677. if source > 0 {
  1678. mustMap = append(mustMap, map[string]interface{}{
  1679. "term": map[string]interface{}{
  1680. "Source": source,
  1681. },
  1682. })
  1683. }
  1684. // 创建人
  1685. if adminId > 0 {
  1686. mustMap = append(mustMap, map[string]interface{}{
  1687. "term": map[string]interface{}{
  1688. "SysUserId": adminId,
  1689. },
  1690. })
  1691. }
  1692. // 查询和排除的表格IDs
  1693. if len(queryIds) > 0 {
  1694. mustMap = append(mustMap, map[string]interface{}{
  1695. "terms": map[string]interface{}{
  1696. "ExcelInfoId": queryIds,
  1697. },
  1698. })
  1699. }
  1700. if len(exceptIds) > 0 {
  1701. mustNotMap = append(mustNotMap, map[string]interface{}{
  1702. "terms": map[string]interface{}{
  1703. "ExcelInfoId": exceptIds,
  1704. },
  1705. })
  1706. }
  1707. // 关键字匹配
  1708. mustMap = append(mustMap, map[string]interface{}{
  1709. "bool": shouldMap,
  1710. })
  1711. queryMap := map[string]interface{}{
  1712. "query": map[string]interface{}{
  1713. "bool": map[string]interface{}{
  1714. "must": mustMap,
  1715. "must_not": mustNotMap,
  1716. //"should": shouldMap,
  1717. },
  1718. },
  1719. }
  1720. //jsonBytes, _ := json.Marshal(queryMap)
  1721. //fmt.Println(string(jsonBytes))
  1722. // 根据条件数量统计
  1723. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1724. t, e := requestTotalHits.Do(context.Background())
  1725. if e != nil {
  1726. err = fmt.Errorf("total hits err: %v", e)
  1727. return
  1728. }
  1729. total = t
  1730. // 表格名称高亮,分页
  1731. highlightKeyName := "ExcelName"
  1732. queryMap["highlight"] = map[string]interface{}{
  1733. "fields": map[string]interface{}{
  1734. highlightKeyName: map[string]interface{}{},
  1735. },
  1736. "pre_tags": "<span style=\"color:#0052D9\">",
  1737. "post_tags": "</span>",
  1738. }
  1739. queryMap["from"] = from
  1740. queryMap["size"] = size
  1741. //jsonBytes, _ := json.Marshal(queryMap)
  1742. //fmt.Println(string(jsonBytes))
  1743. request := client.Search(indexName).Source(queryMap)
  1744. searchResp, e := request.Do(context.Background())
  1745. if e != nil {
  1746. err = fmt.Errorf("search do err: %v", e)
  1747. return
  1748. }
  1749. //fmt.Println(searchResp)
  1750. if searchResp.Status != 0 {
  1751. return
  1752. }
  1753. if searchResp.Hits == nil {
  1754. return
  1755. }
  1756. searchMap := make(map[string]string)
  1757. for _, v := range searchResp.Hits.Hits {
  1758. if _, ok := searchMap[v.Id]; ok {
  1759. continue
  1760. }
  1761. j, e := v.Source.MarshalJSON()
  1762. if e != nil {
  1763. err = fmt.Errorf("hits json err: %v", e)
  1764. return
  1765. }
  1766. item := new(excel.SearchExcelInfo)
  1767. if e = json.Unmarshal(j, &item); e != nil {
  1768. err = fmt.Errorf("hits json unmarshal err: %v", e)
  1769. return
  1770. }
  1771. if len(v.Highlight[highlightKeyName]) > 0 {
  1772. item.SearchText = v.Highlight[highlightKeyName][0]
  1773. } else {
  1774. item.SearchText = item.ExcelName
  1775. }
  1776. list = append(list, item)
  1777. searchMap[v.Id] = v.Id
  1778. }
  1779. return
  1780. }
  1781. // EsAddOrEditDataSourceIndex 新增/修改es中的数据源指标
  1782. func EsAddOrEditDataSourceIndex(indexName, docId string, item *dataSourceModel.SearchDataSource) (err error) {
  1783. defer func() {
  1784. if err != nil {
  1785. tips := fmt.Sprintf("EsAddOrEditDataSourceIndex err: %v", err)
  1786. utils.FileLog.Info(tips)
  1787. }
  1788. }()
  1789. client := utils.EsClient
  1790. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  1791. if e != nil {
  1792. err = fmt.Errorf("resp err, %v", e)
  1793. return
  1794. }
  1795. if resp.Status != 0 {
  1796. err = fmt.Errorf("result err, status: %d, result: %s", resp.Status, resp.Result)
  1797. return
  1798. }
  1799. err = nil
  1800. fmt.Println("data source write to es success", resp.Result)
  1801. return
  1802. }
  1803. // SearchDataSourceIndex 查询es中的数据源
  1804. func SearchDataSourceIndex(indexName, keyword string, source, subSource int, classifyIds, adminIds []int, frequency []string, from, size int) (total int64, list []*dataSourceModel.SearchDataSourceItem, err error) {
  1805. list = make([]*dataSourceModel.SearchDataSourceItem, 0)
  1806. defer func() {
  1807. if err != nil {
  1808. tips := fmt.Sprintf("SearchDataSourceIndex err: %v", err)
  1809. utils.FileLog.Info(tips)
  1810. }
  1811. }()
  1812. client := utils.EsClient
  1813. mustMap := make([]interface{}, 0)
  1814. mustNotMap := make([]interface{}, 0)
  1815. mustMap = append(mustMap, map[string]interface{}{
  1816. "term": map[string]interface{}{
  1817. "IsDeleted": 0,
  1818. },
  1819. })
  1820. // 指标编码/名称
  1821. shouldMap := make(map[string]interface{}, 0)
  1822. if keyword != "" {
  1823. shouldMap["should"] = []interface{}{
  1824. map[string]interface{}{
  1825. "match": map[string]interface{}{
  1826. "IndexCode": keyword,
  1827. },
  1828. },
  1829. map[string]interface{}{
  1830. "match": map[string]interface{}{
  1831. "IndexName": keyword,
  1832. },
  1833. },
  1834. }
  1835. //queryFields := []string{"IndexName.keyword", "IndexName.ik", "IndexName.ngram", "IndexCode.keyword", "IndexCode.ngram"}
  1836. //mustMap = append(mustMap, map[string]interface{}{
  1837. // "multi_match": map[string]interface{}{
  1838. // "query": keyword,
  1839. // "fields": queryFields,
  1840. // },
  1841. //})
  1842. }
  1843. // 来源/子来源
  1844. if source > 0 {
  1845. mustMap = append(mustMap, map[string]interface{}{
  1846. "term": map[string]interface{}{
  1847. "Source": source,
  1848. },
  1849. })
  1850. }
  1851. if subSource > 0 {
  1852. mustMap = append(mustMap, map[string]interface{}{
  1853. "term": map[string]interface{}{
  1854. "SubSource": subSource,
  1855. },
  1856. })
  1857. }
  1858. // 分类
  1859. if len(classifyIds) > 0 {
  1860. mustMap = append(mustMap, map[string]interface{}{
  1861. "term": map[string]interface{}{
  1862. "ClassifyId": classifyIds,
  1863. },
  1864. })
  1865. }
  1866. // 创建人
  1867. if len(adminIds) > 0 {
  1868. mustMap = append(mustMap, map[string]interface{}{
  1869. "term": map[string]interface{}{
  1870. "SysUserId": adminIds,
  1871. },
  1872. })
  1873. }
  1874. // 频度
  1875. if len(frequency) > 0 {
  1876. mustMap = append(mustMap, map[string]interface{}{
  1877. "term": map[string]interface{}{
  1878. "Frequency": frequency,
  1879. },
  1880. })
  1881. }
  1882. // 关键字匹配
  1883. mustMap = append(mustMap, map[string]interface{}{
  1884. "bool": shouldMap,
  1885. })
  1886. queryMap := map[string]interface{}{
  1887. "query": map[string]interface{}{
  1888. "bool": map[string]interface{}{
  1889. "must": mustMap,
  1890. "must_not": mustNotMap,
  1891. //"should": shouldMap,
  1892. },
  1893. },
  1894. }
  1895. //tj, _ := json.Marshal(queryMap)
  1896. //utils.FileLog.Info(string(tj))
  1897. // 根据条件数量统计
  1898. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1899. t, e := requestTotalHits.Do(context.Background())
  1900. if e != nil {
  1901. err = fmt.Errorf("total hits err: %v", e)
  1902. return
  1903. }
  1904. total = t
  1905. // 表格名称高亮,分页
  1906. highlightKeyName := "IndexName"
  1907. //highlightIk := "IndexName.ik"
  1908. //highlightKeyword := "IndexName.keyword"
  1909. queryMap["highlight"] = map[string]interface{}{
  1910. "fields": map[string]interface{}{
  1911. highlightKeyName: map[string]interface{}{},
  1912. //highlightIk: map[string]interface{}{},
  1913. //highlightKeyword: map[string]interface{}{},
  1914. },
  1915. "pre_tags": "<span style=\"color:#0052D9\">",
  1916. "post_tags": "</span>",
  1917. }
  1918. queryMap["from"] = from
  1919. queryMap["size"] = size
  1920. qj, _ := json.Marshal(queryMap)
  1921. utils.FileLog.Info(string(qj))
  1922. request := client.Search(indexName).Source(queryMap)
  1923. searchResp, e := request.Do(context.Background())
  1924. if e != nil {
  1925. err = fmt.Errorf("search do err: %v", e)
  1926. return
  1927. }
  1928. rj, _ := json.Marshal(searchResp)
  1929. utils.FileLog.Info(string(rj))
  1930. if searchResp.Status != 0 {
  1931. return
  1932. }
  1933. if searchResp.Hits == nil {
  1934. return
  1935. }
  1936. searchMap := make(map[string]string)
  1937. for _, v := range searchResp.Hits.Hits {
  1938. if _, ok := searchMap[v.Id]; ok {
  1939. continue
  1940. }
  1941. j, e := v.Source.MarshalJSON()
  1942. if e != nil {
  1943. err = fmt.Errorf("hits json err: %v", e)
  1944. return
  1945. }
  1946. item := new(dataSourceModel.SearchDataSourceItem)
  1947. if e = json.Unmarshal(j, &item); e != nil {
  1948. err = fmt.Errorf("hits json unmarshal err: %v", e)
  1949. return
  1950. }
  1951. if len(v.Highlight[highlightKeyName]) > 0 {
  1952. item.SearchText = v.Highlight[highlightKeyName][0]
  1953. }
  1954. // 优先取keyword的高亮,其次是ik分词之后的高亮
  1955. //if len(v.Highlight) > 0 {
  1956. // if len(v.Highlight[highlightKeyword]) > 0 {
  1957. // item.SearchText = v.Highlight[highlightKeyword][0]
  1958. // }
  1959. // if len(v.Highlight[highlightKeyword]) == 0 && len(v.Highlight[highlightIk]) > 0 {
  1960. // item.SearchText = v.Highlight[highlightIk][0]
  1961. // }
  1962. //}
  1963. if item.SearchText == "" {
  1964. item.SearchText = item.IndexName
  1965. }
  1966. list = append(list, item)
  1967. searchMap[v.Id] = v.Id
  1968. }
  1969. return
  1970. }
  1971. // CreateEsIndex 创建ES索引
  1972. func CreateEsIndex(indexName, jsonMapping string) (err error) {
  1973. defer func() {
  1974. if err != nil {
  1975. tips := fmt.Sprintf("CreateEsIndex err: %v", err)
  1976. utils.FileLog.Info(tips)
  1977. }
  1978. }()
  1979. client := utils.EsClient
  1980. // 校验是否存在
  1981. exists, e := client.IndexExists(indexName).Do(context.Background())
  1982. if e != nil {
  1983. err = fmt.Errorf("check exists err: %v", e)
  1984. return
  1985. }
  1986. if exists {
  1987. fmt.Printf("索引已存在: %s, 跳过\n", indexName)
  1988. return
  1989. }
  1990. // 创建索引及映射
  1991. createIndex, e := client.CreateIndex(indexName).BodyJson(jsonMapping).Do(context.Background())
  1992. if e != nil {
  1993. err = fmt.Errorf("create index err: %v", e)
  1994. return
  1995. }
  1996. fmt.Printf("create index success: %s\n", createIndex.Index)
  1997. return
  1998. }