elastic.go 39 KB

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