elastic.go 49 KB

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