elastic.go 45 KB

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