elastic.go 40 KB

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