elastic.go 45 KB

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