elastic.go 45 KB

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