elastic.go 41 KB

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