elastic.go 44 KB

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