elastic.go 43 KB

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