elastic.go 46 KB

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