elastic.go 45 KB

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