elastic.go 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/models/data_manage/excel"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  10. "github.com/olivere/elastic/v7"
  11. "strconv"
  12. )
  13. // indexName:索引名称
  14. // mappingJson:表结构
  15. func EsCreateIndex(indexName, mappingJson string) (err error) {
  16. client := utils.EsClient
  17. //定义表结构
  18. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  19. if err != nil {
  20. return
  21. }
  22. if !exists {
  23. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  24. //BodyJson(bodyJson).Do(context.Background())
  25. if err != nil {
  26. fmt.Println("CreateIndex Err:" + err.Error())
  27. return err
  28. }
  29. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  30. } else {
  31. fmt.Println(indexName + " 已存在")
  32. }
  33. return
  34. }
  35. // 新增和修改数据
  36. //func EsAddOrEditData(item *cygx.CygxArticle) (err error) {
  37. // defer func() {
  38. // if err != nil {
  39. // fmt.Println("EsAddOrEditData Err:", err.Error())
  40. // }
  41. // }()
  42. // indexName := utils.IndexName
  43. // content := html.UnescapeString(item.Body)
  44. // doc, errDoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  45. // if errDoc != nil {
  46. // return
  47. // }
  48. // doc.Find("a").Each(func(i int, a *goquery.Selection) {
  49. // a.Remove()
  50. // })
  51. // bodyText := doc.Text()
  52. // client, err := NewClient()
  53. // if err != nil {
  54. // fmt.Println(err, "err1")
  55. // return
  56. // }
  57. // docId := strconv.Itoa(item.ArticleId)
  58. // searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  59. // if err != nil && !strings.Contains(err.Error(), "404") {
  60. // fmt.Println("Get Err" + err.Error())
  61. // return
  62. // }
  63. // if err != nil && strings.Contains(err.Error(), "404") {
  64. // err = nil
  65. // }
  66. // if searchById != nil && searchById.Found {
  67. // resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  68. // "BodyText": bodyText,
  69. // "Title": item.Title,
  70. // "PublishDate": item.PublishDate.Format(utils.FormatDateTime),
  71. // "CategoryId": item.CategoryId,
  72. // "ExpertBackground": item.ExpertBackground,
  73. // }).Do(context.Background())
  74. // if err != nil {
  75. // fmt.Println(err, "err")
  76. // return err
  77. // }
  78. // if resp.Status == 0 {
  79. // fmt.Println("修改成功")
  80. // } else {
  81. // fmt.Println("EditData", resp.Status, resp.Result)
  82. // }
  83. // } else {
  84. // itemEs := new(cygx.ElasticTestArticleDetail)
  85. // itemEs.ArticleId = item.ArticleId
  86. // itemEs.Title = item.Title
  87. // itemEs.PublishDate = item.PublishDate.Format(utils.FormatDateTime)
  88. // itemEs.BodyText = bodyText
  89. // itemEs.CategoryId = strconv.Itoa(item.CategoryId)
  90. // itemEs.ExpertBackground = item.ExpertBackground
  91. // resp, err := client.Index().Index(indexName).Id(docId).BodyJson(itemEs).Do(context.Background())
  92. // if err != nil {
  93. // fmt.Println("新增失败:", err.Error())
  94. // return err
  95. // }
  96. // if resp.Status == 0 && resp.Result == "created" {
  97. // fmt.Println("新增成功")
  98. // err = nil
  99. // } else {
  100. // fmt.Println("AddData", resp.Status, resp.Result)
  101. // }
  102. // }
  103. // return
  104. //}
  105. // 删除数据
  106. func EsDeleteData(indexName, docId string) (err error) {
  107. client := utils.EsClient
  108. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  109. fmt.Println(resp)
  110. if err != nil {
  111. return
  112. }
  113. if resp.Status == 0 {
  114. fmt.Println("删除成功")
  115. } else {
  116. fmt.Println("AddData", resp.Status, resp.Result)
  117. }
  118. return
  119. }
  120. func MappingModify(indexName, mappingJson string) {
  121. client := utils.EsClient
  122. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  123. fmt.Println(err)
  124. fmt.Println(result)
  125. return
  126. }
  127. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  128. func EsAddOrEditEdbInfoData(indexName, docId string, item *data_manage.EdbInfoList) (err error) {
  129. defer func() {
  130. if err != nil {
  131. fmt.Println("EsAddOrEditData Err:", err.Error())
  132. }
  133. }()
  134. client := utils.EsClient
  135. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  136. if err != nil {
  137. fmt.Println("新增失败:", err.Error())
  138. return err
  139. }
  140. fmt.Println(resp)
  141. if resp.Status == 0 {
  142. fmt.Println("新增成功", resp.Result)
  143. err = nil
  144. } else {
  145. fmt.Println("AddData", resp.Status, resp.Result)
  146. }
  147. return
  148. }
  149. // SearchEdbInfoData 查询es中的指标数据
  150. func SearchEdbInfoData(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType int8, frequency string, noPermissionEdbInfoIdList []int) (total int64, list []*data_manage.EdbInfoList, err error) {
  151. list = make([]*data_manage.EdbInfoList, 0)
  152. defer func() {
  153. if err != nil {
  154. fmt.Println("EsAddOrEditData Err:", err.Error())
  155. }
  156. }()
  157. highlight := elastic.NewHighlight()
  158. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  159. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  160. //var source map[string]interface{}
  161. //source := map[string]interface{}{
  162. // "query": map[string]interface{}{
  163. // "match_all": map[string]interface{}{},
  164. // },
  165. //}
  166. mustMap := make([]interface{}, 0)
  167. mustNotMap := make([]interface{}, 0)
  168. //source := map[string]interface{}{
  169. // "query": map[string]interface{}{
  170. // "bool": map[string]interface{}{
  171. // "must": map[string]interface{}{
  172. // "query_string": map[string]interface{}{
  173. // "query": keywordStr,
  174. // "fields": []string{"EdbCode", "EdbName"},
  175. // },
  176. // },
  177. // },
  178. // },
  179. //}
  180. switch filterSource {
  181. case 2:
  182. //source = map[string]interface{}{
  183. // "query": map[string]interface{}{
  184. // "bool": map[string]interface{}{
  185. // "must": map[string]interface{}{
  186. // "query_string": map[string]interface{}{
  187. // "query": keywordStr,
  188. // },
  189. // },
  190. // "filter": []interface{}{
  191. // map[string]interface{}{
  192. // "term": map[string]interface{}{
  193. // "Frequency.keyword": "月度",
  194. // },
  195. // }},
  196. // },
  197. // },
  198. //}
  199. mustMap = []interface{}{
  200. map[string]interface{}{
  201. "term": map[string]interface{}{
  202. "Frequency.keyword": "月度",
  203. //"Frequency.keyword": "月度",
  204. },
  205. },
  206. }
  207. case 3:
  208. //source = map[string]interface{}{
  209. // "query": map[string]interface{}{
  210. // "bool": map[string]interface{}{
  211. // "must": map[string]interface{}{
  212. // "query_string": map[string]interface{}{
  213. // "query": keywordStr,
  214. // },
  215. // },
  216. // "must_not": []interface{}{
  217. // map[string]interface{}{
  218. // "match": map[string]interface{}{
  219. // "Frequency.keyword": "日度",
  220. // },
  221. // }},
  222. // },
  223. // },
  224. //}
  225. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  226. //mustNotMap = []interface{}{
  227. // map[string]interface{}{
  228. // "match": map[string]interface{}{
  229. // "Frequency.keyword": "日度",
  230. // //"Frequency.keyword": "月度",
  231. // },
  232. // },
  233. //}
  234. case 4:
  235. //source = map[string]interface{}{
  236. // "query": map[string]interface{}{
  237. // "bool": map[string]interface{}{
  238. // "must": map[string]interface{}{
  239. // "query_string": map[string]interface{}{
  240. // "query": keywordStr,
  241. // },
  242. // },
  243. // "filter": []interface{}{
  244. // map[string]interface{}{
  245. // "term": map[string]interface{}{
  246. // "EdbType": 1,
  247. // },
  248. // }},
  249. // },
  250. // },
  251. //}
  252. mustMap = []interface{}{
  253. map[string]interface{}{
  254. "term": map[string]interface{}{
  255. "EdbType": 1,
  256. },
  257. },
  258. }
  259. case 5:
  260. mustMap = []interface{}{
  261. map[string]interface{}{
  262. "term": map[string]interface{}{
  263. "Source": 6,
  264. },
  265. },
  266. }
  267. case 6:
  268. mustNotMap = []interface{}{
  269. map[string]interface{}{
  270. "match": map[string]interface{}{
  271. "Frequency.keyword": "年度",
  272. },
  273. },
  274. }
  275. }
  276. //指标来源
  277. if source > 0 {
  278. mustMap = append(mustMap, map[string]interface{}{
  279. "term": map[string]interface{}{
  280. "Source": source,
  281. //"Frequency.keyword": "月度",
  282. },
  283. })
  284. }
  285. if frequency != "" {
  286. mustMap = append(mustMap, map[string]interface{}{
  287. "term": map[string]interface{}{
  288. "Frequency.keyword": frequency,
  289. //"Frequency.keyword": "月度",
  290. },
  291. })
  292. }
  293. // noPermissionEdbInfoIdList 无权限指标id
  294. if len(noPermissionEdbInfoIdList) > 0 {
  295. mustNotMap = append(mustNotMap, map[string]interface{}{
  296. "terms": map[string]interface{}{
  297. "EdbInfoId": noPermissionEdbInfoIdList,
  298. //"Frequency.keyword": "月度",
  299. },
  300. })
  301. }
  302. // 指标类型:普通指标、预测指标(小于0 代表不区分指标是普通还是预测)
  303. if edbInfoType >= 0 {
  304. mustMap = append(mustMap, map[string]interface{}{
  305. "term": map[string]interface{}{
  306. "EdbInfoType": edbInfoType,
  307. },
  308. })
  309. }
  310. //普通指标
  311. //mustMap = append(mustMap, map[string]interface{}{
  312. // "term": map[string]interface{}{
  313. // "EdbInfoType": 0,
  314. // //"Frequency.keyword": "月度",
  315. // },
  316. //})
  317. //关键字匹配
  318. //shouldMap := map[string]interface{}{
  319. // "should": []interface{}{
  320. // map[string]interface{}{
  321. // "match": map[string]interface{}{
  322. // "EdbCode": keywordStr,
  323. // //"Frequency.keyword": "月度",
  324. // },
  325. // },
  326. // map[string]interface{}{
  327. // "match": map[string]interface{}{
  328. // "EdbName": keywordStr,
  329. // //"Frequency.keyword": "月度",
  330. // },
  331. // },
  332. // map[string]interface{}{
  333. // "match": map[string]interface{}{
  334. // "EdbNameEn": keywordStr,
  335. // //"Frequency.keyword": "月度",
  336. // },
  337. // },
  338. // },
  339. //}
  340. // 默认使用中文名字字段去匹配
  341. keywordNameKey := `EdbName`
  342. // 如果没有中文,则使用英文名称字段去匹配
  343. if !utils.ContainsChinese(keywordStr) {
  344. keywordNameKey = `EdbNameEn`
  345. }
  346. shouldMap := map[string]interface{}{
  347. "should": []interface{}{
  348. map[string]interface{}{
  349. "match": map[string]interface{}{
  350. "EdbCode": keywordStr,
  351. //"Frequency.keyword": "月度",
  352. },
  353. },
  354. map[string]interface{}{
  355. "match": map[string]interface{}{
  356. keywordNameKey: keywordStr,
  357. //"Frequency.keyword": "月度",
  358. },
  359. },
  360. },
  361. }
  362. //mustMap = append(mustMap, map[string]interface{}{
  363. // "bool": shouldMap,
  364. //})
  365. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  366. }
  367. func SearchEdbInfoDataBak(indexName, keywordStr string, from, size, filterSource, source int, frequency string) (total int64, list []*data_manage.EdbInfoList, err error) {
  368. list = make([]*data_manage.EdbInfoList, 0)
  369. defer func() {
  370. if err != nil {
  371. fmt.Println("EsAddOrEditData Err:", err.Error())
  372. }
  373. }()
  374. client := utils.EsClient
  375. //queryString := elastic.NewQueryStringQuery(keywordStr)
  376. //boolQueryJson, err := json.Marshal(queryString)
  377. //if err != nil {
  378. // fmt.Println("boolQueryJson err:", err)
  379. //} else {
  380. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  381. //}
  382. highlight := elastic.NewHighlight()
  383. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  384. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  385. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  386. //var source map[string]interface{}
  387. //source := map[string]interface{}{
  388. // "query": map[string]interface{}{
  389. // "match_all": map[string]interface{}{},
  390. // },
  391. //}
  392. mustMap := make([]interface{}, 0)
  393. mustNotMap := make([]interface{}, 0)
  394. //source := map[string]interface{}{
  395. // "query": map[string]interface{}{
  396. // "bool": map[string]interface{}{
  397. // "must": map[string]interface{}{
  398. // "query_string": map[string]interface{}{
  399. // "query": keywordStr,
  400. // "fields": []string{"EdbCode", "EdbName"},
  401. // },
  402. // },
  403. // },
  404. // },
  405. //}
  406. switch filterSource {
  407. case 2:
  408. //source = map[string]interface{}{
  409. // "query": map[string]interface{}{
  410. // "bool": map[string]interface{}{
  411. // "must": map[string]interface{}{
  412. // "query_string": map[string]interface{}{
  413. // "query": keywordStr,
  414. // },
  415. // },
  416. // "filter": []interface{}{
  417. // map[string]interface{}{
  418. // "term": map[string]interface{}{
  419. // "Frequency.keyword": "月度",
  420. // },
  421. // }},
  422. // },
  423. // },
  424. //}
  425. mustMap = []interface{}{
  426. map[string]interface{}{
  427. "term": map[string]interface{}{
  428. "Frequency.keyword": "月度",
  429. //"Frequency.keyword": "月度",
  430. },
  431. },
  432. }
  433. case 3:
  434. //source = map[string]interface{}{
  435. // "query": map[string]interface{}{
  436. // "bool": map[string]interface{}{
  437. // "must": map[string]interface{}{
  438. // "query_string": map[string]interface{}{
  439. // "query": keywordStr,
  440. // },
  441. // },
  442. // "must_not": []interface{}{
  443. // map[string]interface{}{
  444. // "match": map[string]interface{}{
  445. // "Frequency.keyword": "日度",
  446. // },
  447. // }},
  448. // },
  449. // },
  450. //}
  451. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  452. //mustNotMap = []interface{}{
  453. // map[string]interface{}{
  454. // "match": map[string]interface{}{
  455. // "Frequency.keyword": "日度",
  456. // //"Frequency.keyword": "月度",
  457. // },
  458. // },
  459. //}
  460. case 4:
  461. //source = map[string]interface{}{
  462. // "query": map[string]interface{}{
  463. // "bool": map[string]interface{}{
  464. // "must": map[string]interface{}{
  465. // "query_string": map[string]interface{}{
  466. // "query": keywordStr,
  467. // },
  468. // },
  469. // "filter": []interface{}{
  470. // map[string]interface{}{
  471. // "term": map[string]interface{}{
  472. // "EdbType": 1,
  473. // },
  474. // }},
  475. // },
  476. // },
  477. //}
  478. mustMap = []interface{}{
  479. map[string]interface{}{
  480. "term": map[string]interface{}{
  481. "EdbType": 1,
  482. },
  483. },
  484. }
  485. case 5:
  486. mustMap = []interface{}{
  487. map[string]interface{}{
  488. "term": map[string]interface{}{
  489. "Source": 6,
  490. },
  491. },
  492. }
  493. }
  494. //指标来源
  495. if source > 0 {
  496. mustMap = append(mustMap, map[string]interface{}{
  497. "term": map[string]interface{}{
  498. "Source": source,
  499. //"Frequency.keyword": "月度",
  500. },
  501. })
  502. }
  503. if frequency != "" {
  504. mustMap = append(mustMap, map[string]interface{}{
  505. "term": map[string]interface{}{
  506. "Frequency.keyword": frequency,
  507. //"Frequency.keyword": "月度",
  508. },
  509. })
  510. }
  511. //普通指标
  512. //mustMap = append(mustMap, map[string]interface{}{
  513. // "term": map[string]interface{}{
  514. // "EdbInfoType": 0,
  515. // //"Frequency.keyword": "月度",
  516. // },
  517. //})
  518. //关键字匹配
  519. shouldMap := map[string]interface{}{
  520. "should": []interface{}{
  521. map[string]interface{}{
  522. "match": map[string]interface{}{
  523. "EdbCode": keywordStr,
  524. //"Frequency.keyword": "月度",
  525. },
  526. },
  527. map[string]interface{}{
  528. "match": map[string]interface{}{
  529. "EdbName": keywordStr,
  530. //"Frequency.keyword": "月度",
  531. },
  532. },
  533. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  534. map[string]interface{}{
  535. "match": map[string]interface{}{
  536. "EdbCode": map[string]interface{}{
  537. "query": keywordStr,
  538. "operator": "and",
  539. },
  540. //"Frequency.keyword": "月度",
  541. },
  542. },
  543. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  544. map[string]interface{}{
  545. "match": map[string]interface{}{
  546. "EdbName": map[string]interface{}{
  547. "query": keywordStr,
  548. "operator": "and",
  549. },
  550. //"Frequency.keyword": "月度",
  551. },
  552. },
  553. },
  554. }
  555. mustMap = append(mustMap, map[string]interface{}{
  556. "bool": shouldMap,
  557. })
  558. queryMap := map[string]interface{}{
  559. "query": map[string]interface{}{
  560. "bool": map[string]interface{}{
  561. "must": mustMap,
  562. "must_not": mustNotMap,
  563. //"should": shouldMap,
  564. },
  565. },
  566. }
  567. //根据条件数量统计
  568. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  569. total, err = requestTotalHits.Do(context.Background())
  570. if err != nil {
  571. return
  572. }
  573. queryMap["from"] = from
  574. queryMap["size"] = size
  575. jsonBytes, _ := json.Marshal(queryMap)
  576. fmt.Println(string(jsonBytes))
  577. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  578. //switch filterSource {
  579. //case 2:
  580. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  581. //case 3:
  582. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  583. //case 4:
  584. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  585. //}
  586. //queryString := elastic.RawStringQuery(queryStr)
  587. //fmt.Println("queryString:", queryString)
  588. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  589. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  590. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  591. //requestJson, err := json.Marshal(request)
  592. //if err != nil {
  593. // fmt.Println("requestJson err:", err)
  594. //}
  595. //fmt.Println("requestJson ", string(requestJson))
  596. searchMap := make(map[string]string)
  597. searchResp, err := request.Do(context.Background())
  598. if err != nil {
  599. return
  600. }
  601. fmt.Println(searchResp)
  602. fmt.Println(searchResp.Status)
  603. if searchResp.Status != 0 {
  604. return
  605. }
  606. if searchResp.Hits != nil {
  607. for _, v := range searchResp.Hits.Hits {
  608. if _, ok := searchMap[v.Id]; !ok {
  609. itemJson, tmpErr := v.Source.MarshalJSON()
  610. if tmpErr != nil {
  611. err = tmpErr
  612. fmt.Println("movieJson err:", err)
  613. return
  614. }
  615. edbInfoItem := new(data_manage.EdbInfoList)
  616. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  617. if err != nil {
  618. fmt.Println("json.Unmarshal movieJson err:", err)
  619. err = tmpErr
  620. return
  621. }
  622. if len(v.Highlight["EdbCode"]) > 0 {
  623. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  624. }
  625. if len(v.Highlight["EdbName"]) > 0 {
  626. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  627. }
  628. list = append(list, edbInfoItem)
  629. searchMap[v.Id] = v.Id
  630. }
  631. }
  632. }
  633. //for _, v := range result {
  634. // fmt.Println(v)
  635. //}
  636. return
  637. }
  638. // SearchAddPredictEdbInfoData 查询允许添加预测指标的数据
  639. func SearchAddPredictEdbInfoData(indexName, keywordStr string, noPermissionEdbInfoIdList []int, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  640. list = make([]*data_manage.EdbInfoList, 0)
  641. defer func() {
  642. if err != nil {
  643. fmt.Println("EsAddOrEditData Err:", err.Error())
  644. }
  645. }()
  646. highlight := elastic.NewHighlight()
  647. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  648. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  649. mustMap := make([]interface{}, 0)
  650. mustNotMap := make([]interface{}, 0)
  651. mustNotMap = []interface{}{
  652. //map[string]interface{}{
  653. // "terms": map[string]interface{}{
  654. // "Frequency.keyword": []string{"日度", "周度", "月度"},
  655. // },
  656. // //"match": map[string]interface{}{
  657. // // "Frequency": []string{"日度", "周度", "月度"},
  658. // // //"Frequency.keyword": []string{"日度", "周度", "月度"},
  659. // //},
  660. //},
  661. }
  662. // 指标类型:普通指标、预算指标
  663. mustMap = append(mustMap, map[string]interface{}{
  664. "term": map[string]interface{}{
  665. "EdbInfoType": 0,
  666. },
  667. })
  668. mustMap = append(mustMap, map[string]interface{}{
  669. "terms": map[string]interface{}{
  670. "Frequency.keyword": []string{"日度", "周度", "月度"},
  671. },
  672. })
  673. //关键字匹配
  674. //shouldMap := map[string]interface{}{
  675. // "should": []interface{}{
  676. // map[string]interface{}{
  677. // "match": map[string]interface{}{
  678. // "EdbCode": keywordStr,
  679. // //"Frequency.keyword": "月度",
  680. // },
  681. // },
  682. // map[string]interface{}{
  683. // "match": map[string]interface{}{
  684. // "EdbName": keywordStr,
  685. // //"Frequency.keyword": "月度",
  686. // },
  687. // },
  688. // map[string]interface{}{
  689. // "match": map[string]interface{}{
  690. // "EdbNameEn": keywordStr,
  691. // //"Frequency.keyword": "月度",
  692. // },
  693. // },
  694. // },
  695. //}
  696. // 默认使用中文名字字段去匹配
  697. keywordNameKey := `EdbName`
  698. // 如果没有中文,则使用英文名称字段去匹配
  699. if !utils.ContainsChinese(keywordStr) {
  700. keywordNameKey = `EdbNameEn`
  701. }
  702. shouldMap := map[string]interface{}{
  703. "should": []interface{}{
  704. map[string]interface{}{
  705. "match": map[string]interface{}{
  706. "EdbCode": keywordStr,
  707. //"Frequency.keyword": "月度",
  708. },
  709. },
  710. map[string]interface{}{
  711. "match": map[string]interface{}{
  712. keywordNameKey: keywordStr,
  713. //"Frequency.keyword": "月度",
  714. },
  715. },
  716. },
  717. }
  718. // noPermissionEdbInfoIdList 无权限指标id
  719. if len(noPermissionEdbInfoIdList) > 0 {
  720. mustNotMap = append(mustNotMap, map[string]interface{}{
  721. "terms": map[string]interface{}{
  722. "EdbInfoId": noPermissionEdbInfoIdList,
  723. //"Frequency.keyword": "月度",
  724. },
  725. })
  726. }
  727. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  728. }
  729. // searchEdbInfoData 查询es中的指标数据
  730. func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMap map[string]interface{}, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  731. list = make([]*data_manage.EdbInfoList, 0)
  732. defer func() {
  733. if err != nil {
  734. fmt.Println("EsAddOrEditData Err:", err.Error())
  735. }
  736. }()
  737. client := utils.EsClient
  738. //queryString := elastic.NewQueryStringQuery(keywordStr)
  739. //boolQueryJson, err := json.Marshal(queryString)
  740. //if err != nil {
  741. // fmt.Println("boolQueryJson err:", err)
  742. //} else {
  743. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  744. //}
  745. highlight := elastic.NewHighlight()
  746. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  747. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  748. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  749. //关键字匹配
  750. mustMap = append(mustMap, map[string]interface{}{
  751. "bool": shouldMap,
  752. })
  753. queryMap := map[string]interface{}{
  754. "query": map[string]interface{}{
  755. "bool": map[string]interface{}{
  756. "must": mustMap,
  757. "must_not": mustNotMap,
  758. //"should": shouldMap,
  759. },
  760. },
  761. }
  762. //根据条件数量统计
  763. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  764. total, err = requestTotalHits.Do(context.Background())
  765. if err != nil {
  766. return
  767. }
  768. queryMap["from"] = from
  769. queryMap["size"] = size
  770. queryMap["highlight"] = map[string]interface{}{
  771. "fields": map[string]interface{}{
  772. "EdbName": map[string]interface{}{},
  773. },
  774. "pre_tags": "<span style=\"color:#0052D9\">",
  775. "post_tags": "</span>",
  776. }
  777. jsonBytes, _ := json.Marshal(queryMap)
  778. fmt.Println(string(jsonBytes))
  779. //queryStr := fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}}}}}`, keywordStr)
  780. //switch filterSource {
  781. //case 2:
  782. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"filter":{"term":{"Frequency.keyword":"%s"}}}}}`, keywordStr, "月度")
  783. //case 3:
  784. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"Frequency.keyword":"%s"}}]}}}`, keywordStr, "日度")
  785. //case 4:
  786. // queryStr = fmt.Sprintf(`{"query":{"bool":{"must":{"query_string":{"query":"%s","fields":["EdbCode","EdbName"]}},"must_not":[{"match":{"EdbType":1}}]}}}`, keywordStr)
  787. //}
  788. //queryString := elastic.RawStringQuery(queryStr)
  789. //fmt.Println("queryString:", queryString)
  790. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  791. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  792. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  793. //requestJson, err := json.Marshal(request)
  794. //if err != nil {
  795. // fmt.Println("requestJson err:", err)
  796. //}
  797. //fmt.Println("requestJson ", string(requestJson))
  798. searchMap := make(map[string]string)
  799. searchResp, err := request.Do(context.Background())
  800. if err != nil {
  801. return
  802. }
  803. fmt.Println(searchResp)
  804. fmt.Println(searchResp.Status)
  805. if searchResp.Status != 0 {
  806. return
  807. }
  808. //total = searchResp.TotalHits()
  809. if searchResp.Hits != nil {
  810. for _, v := range searchResp.Hits.Hits {
  811. if _, ok := searchMap[v.Id]; !ok {
  812. itemJson, tmpErr := v.Source.MarshalJSON()
  813. if tmpErr != nil {
  814. err = tmpErr
  815. fmt.Println("movieJson err:", err)
  816. return
  817. }
  818. edbInfoItem := new(data_manage.EdbInfoList)
  819. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  820. if err != nil {
  821. fmt.Println("json.Unmarshal movieJson err:", err)
  822. err = tmpErr
  823. return
  824. }
  825. if len(v.Highlight["EdbCode"]) > 0 {
  826. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  827. }
  828. if len(v.Highlight["EdbName"]) > 0 {
  829. // 搜索结果高亮用新字段,原EdbName直接高亮展示上会有点影响
  830. edbInfoItem.SearchText = v.Highlight["EdbName"][0]
  831. //edbInfoItem.EdbName = v.Highlight["EdbName"][0]
  832. } else {
  833. edbInfoItem.SearchText = edbInfoItem.EdbName
  834. }
  835. list = append(list, edbInfoItem)
  836. searchMap[v.Id] = v.Id
  837. }
  838. }
  839. }
  840. return
  841. }
  842. // EsDeleteEdbInfoData 删除es中的指标数据
  843. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  844. defer func() {
  845. if err != nil {
  846. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  847. }
  848. }()
  849. client := utils.EsClient
  850. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  851. fmt.Println(resp)
  852. if err != nil {
  853. return
  854. }
  855. if resp.Status == 0 {
  856. fmt.Println("删除成功")
  857. } else {
  858. fmt.Println("AddData", resp.Status, resp.Result)
  859. }
  860. return
  861. }
  862. // EsAddOrEditReport 新增编辑es报告
  863. //func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  864. // defer func() {
  865. // if err != nil {
  866. // fmt.Println("EsAddOrEditReport Err:", err.Error())
  867. // }
  868. // }()
  869. // client, err := NewClient()
  870. // if err != nil {
  871. // return
  872. // }
  873. // // docId为报告ID+章节ID
  874. // searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  875. // if err != nil && !strings.Contains(err.Error(), "404") {
  876. // fmt.Println("Get Err" + err.Error())
  877. // return
  878. // }
  879. // if searchById != nil && searchById.Found {
  880. // resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  881. // "ReportId": item.ReportId,
  882. // "ReportChapterId": item.ReportChapterId,
  883. // "Title": item.Title,
  884. // "Abstract": item.Abstract,
  885. // "BodyContent": item.BodyContent,
  886. // "PublishTime": item.PublishTime,
  887. // "PublishState": item.PublishState,
  888. // "Author": item.Author,
  889. // "ClassifyIdFirst": item.ClassifyIdFirst,
  890. // "ClassifyNameFirst": item.ClassifyNameFirst,
  891. // "ClassifyIdSecond": item.ClassifyIdSecond,
  892. // "ClassifyNameSecond": item.ClassifyNameSecond,
  893. // "Categories": item.Categories,
  894. // "StageStr": item.StageStr,
  895. // }).Do(context.Background())
  896. // if err != nil {
  897. // return err
  898. // }
  899. // //fmt.Println(resp.Status, resp.Result)
  900. // if resp.Status == 0 {
  901. // fmt.Println("修改成功" + docId)
  902. // err = nil
  903. // } else {
  904. // fmt.Println("EditData", resp.Status, resp.Result)
  905. // }
  906. // } else {
  907. // resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  908. // if err != nil {
  909. // fmt.Println("新增失败:", err.Error())
  910. // return err
  911. // }
  912. // if resp.Status == 0 && resp.Result == "created" {
  913. // fmt.Println("新增成功" + docId)
  914. // return nil
  915. // } else {
  916. // fmt.Println("AddData", resp.Status, resp.Result)
  917. // }
  918. // }
  919. // return
  920. //}
  921. // AnalyzeResp 分词接口返回结构体
  922. type AnalyzeResp struct {
  923. Tokens []struct {
  924. EndOffset int64 `json:"end_offset"`
  925. Position int64 `json:"position"`
  926. StartOffset int64 `json:"start_offset"`
  927. Token string `json:"token"`
  928. Type string `json:"type"`
  929. } `json:"tokens"`
  930. }
  931. // Analyze 根据输入的文字获取分词后的文字
  932. func Analyze(content string) (contentList []string, err error) {
  933. defer func() {
  934. if err != nil {
  935. fmt.Println("Analyze Err:", err.Error())
  936. }
  937. }()
  938. client := utils.EsClient
  939. queryMap := map[string]string{
  940. "text": content,
  941. "analyzer": "ik_max_word",
  942. }
  943. res, err := client.PerformRequest(
  944. context.Background(),
  945. elastic.PerformRequestOptions{
  946. Method: "GET",
  947. Path: "/_analyze",
  948. Body: queryMap,
  949. Stream: false,
  950. },
  951. )
  952. if res.StatusCode == 200 {
  953. var analyzeResp AnalyzeResp
  954. tmpErr := json.Unmarshal(res.Body, &analyzeResp)
  955. if tmpErr != nil {
  956. err = errors.New("返回数据转结构体失败:" + tmpErr.Error())
  957. return
  958. }
  959. for _, v := range analyzeResp.Tokens {
  960. contentList = append(contentList, v.Token)
  961. }
  962. } else {
  963. err = errors.New("分词失败,返回code异常:" + strconv.Itoa(res.StatusCode))
  964. }
  965. return
  966. }
  967. // SearcCygxArticleHistoryData 查询查研观向的文章阅读记录
  968. //func SearcCygxArticleHistoryData(indexName, keyword, startDate, endDate, companyIds string, from, size int) (total int64, list []*cygx.EsUserInteraction, err error) {
  969. // list = make([]*cygx.EsUserInteraction, 0)
  970. // defer func() {
  971. // if err != nil {
  972. // fmt.Println("EsUserInteraction Err:", err.Error())
  973. // }
  974. // }()
  975. // client, err := NewClient()
  976. // if err != nil {
  977. // return
  978. // }
  979. //
  980. // mustMap := make([]interface{}, 0)
  981. // seliceid := []int{}
  982. // //管理员权限||对应销售
  983. // if companyIds != "" {
  984. // silceCompanyId := strings.Split(companyIds, ",")
  985. // for _, v := range silceCompanyId {
  986. // companyId, _ := strconv.Atoi(v)
  987. // seliceid = append(seliceid, companyId)
  988. // }
  989. // mustMap = append(mustMap, map[string]interface{}{
  990. // "terms": map[string]interface{}{
  991. // "CompanyId": seliceid,
  992. // },
  993. // })
  994. // }
  995. //
  996. // //时间
  997. // if startDate != "" && endDate != "" {
  998. // mustMap = append(mustMap, map[string]interface{}{
  999. // "range": map[string]interface{}{
  1000. // "CreateTime": map[string]interface{}{
  1001. // "gte": startDate,
  1002. // "lte": endDate,
  1003. // },
  1004. // },
  1005. // })
  1006. // }
  1007. // shouldMap := make(map[string]interface{}, 0)
  1008. // //关键字匹配
  1009. // if keyword != "" {
  1010. // shouldMap = map[string]interface{}{
  1011. // "should": []interface{}{
  1012. // map[string]interface{}{
  1013. // "wildcard": map[string]interface{}{
  1014. // "RealName": "*" + keyword + "*",
  1015. // },
  1016. // },
  1017. // map[string]interface{}{
  1018. // "wildcard": map[string]interface{}{
  1019. // "Email": "*" + keyword + "*",
  1020. // },
  1021. // },
  1022. // map[string]interface{}{
  1023. // "wildcard": map[string]interface{}{
  1024. // "Mobile": "*" + keyword + "*",
  1025. // },
  1026. // },
  1027. // map[string]interface{}{
  1028. // "wildcard": map[string]interface{}{
  1029. // "CompanyName": "*" + keyword + "*",
  1030. // },
  1031. // },
  1032. // },
  1033. // }
  1034. // }
  1035. // mustMap = append(mustMap, map[string]interface{}{
  1036. // "bool": shouldMap,
  1037. // })
  1038. //
  1039. // queryMap := map[string]interface{}{
  1040. // "query": map[string]interface{}{
  1041. // "bool": map[string]interface{}{
  1042. // "must": mustMap,
  1043. // },
  1044. // },
  1045. // }
  1046. // //排序
  1047. // //机构阅读数量
  1048. // sortMap := make([]interface{}, 0)
  1049. // //如果是一家公司就不按照这个字段排序
  1050. // if len(seliceid) > 1 {
  1051. // sortMap = append(sortMap, map[string]interface{}{
  1052. // "CompanyArticleHistoryNum": map[string]interface{}{
  1053. // "order": "desc",
  1054. // },
  1055. // })
  1056. // }
  1057. //
  1058. // //用户阅读数量
  1059. // sortMap = append(sortMap, map[string]interface{}{
  1060. // "UserArticleHistoryNum": map[string]interface{}{
  1061. // "order": "desc",
  1062. // },
  1063. // })
  1064. // //时间
  1065. // sortMap = append(sortMap, map[string]interface{}{
  1066. // "CreateTime": map[string]interface{}{
  1067. // "order": "desc",
  1068. // },
  1069. // })
  1070. // //根据条件数量统计
  1071. // requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1072. // total, err = requestTotalHits.Do(context.Background())
  1073. // if err != nil {
  1074. // return
  1075. // }
  1076. //
  1077. // queryMap["sort"] = sortMap
  1078. // queryMap["from"] = from
  1079. // queryMap["size"] = size
  1080. // jsonBytes, _ := json.Marshal(queryMap)
  1081. // fmt.Println(string(jsonBytes))
  1082. //
  1083. // request := client.Search(indexName).Source(queryMap) // sets the JSON request
  1084. // searchMap := make(map[string]string)
  1085. //
  1086. // searchResp, err := request.Do(context.Background())
  1087. // if err != nil {
  1088. // return
  1089. // }
  1090. // //fmt.Println(searchResp)
  1091. // //fmt.Println(searchResp.Status)
  1092. // if searchResp.Status != 0 {
  1093. // return
  1094. // }
  1095. // if searchResp.Hits != nil {
  1096. // for _, v := range searchResp.Hits.Hits {
  1097. // if _, ok := searchMap[v.Id]; !ok {
  1098. // itemJson, tmpErr := v.Source.MarshalJSON()
  1099. // if tmpErr != nil {
  1100. // err = tmpErr
  1101. // fmt.Println("movieJson err:", err)
  1102. // return
  1103. // }
  1104. // edbInfoItem := new(cygx.EsUserInteraction)
  1105. // tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  1106. // if tmpErr != nil {
  1107. // fmt.Println("json.Unmarshal movieJson err:", err)
  1108. // err = tmpErr
  1109. // return
  1110. // }
  1111. // list = append(list, edbInfoItem)
  1112. // searchMap[v.Id] = v.Id
  1113. // }
  1114. // }
  1115. // }
  1116. // return
  1117. //}
  1118. // EsAddOrEditChartInfoData 新增/修改es中的图表数据
  1119. func EsAddOrEditChartInfoData(indexName, docId string, item *data_manage.ChartInfo) (err error) {
  1120. defer func() {
  1121. if err != nil {
  1122. fmt.Println("EsAddOrEditData Err:", err.Error())
  1123. }
  1124. }()
  1125. client := utils.EsClient
  1126. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  1127. if err != nil {
  1128. fmt.Println("新增失败:", err.Error())
  1129. return err
  1130. }
  1131. fmt.Println(resp)
  1132. if resp.Status == 0 {
  1133. fmt.Println("新增成功", resp.Result)
  1134. err = nil
  1135. } else {
  1136. fmt.Println("AddData", resp.Status, resp.Result)
  1137. }
  1138. return
  1139. }
  1140. // EsDeleteDataV2 删除es中的数据
  1141. func EsDeleteDataV2(indexName, docId string) (err error) {
  1142. defer func() {
  1143. if err != nil {
  1144. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  1145. }
  1146. }()
  1147. client := utils.EsClient
  1148. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  1149. fmt.Println(resp)
  1150. if err != nil {
  1151. return
  1152. }
  1153. if resp.Status == 0 {
  1154. fmt.Println("删除成功")
  1155. } else {
  1156. fmt.Println("AddData", resp.Status, resp.Result)
  1157. }
  1158. return
  1159. }
  1160. // SearchChartInfoData 查询es中的图表数据
  1161. func SearchChartInfoData(indexName, keywordStr string, showSysId int, sourceList []int, noPermissionChartIdList []int, from, size int) (list []*data_manage.ChartInfoMore, total int64, err error) {
  1162. list = make([]*data_manage.ChartInfoMore, 0)
  1163. defer func() {
  1164. if err != nil {
  1165. fmt.Println("EsAddOrEditData Err:", err.Error())
  1166. }
  1167. }()
  1168. client := utils.EsClient
  1169. //queryString := elastic.NewQueryStringQuery(keywordStr)
  1170. //boolQueryJson, err := json.Marshal(queryString)
  1171. //if err != nil {
  1172. // fmt.Println("boolQueryJson err:", err)
  1173. //} else {
  1174. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  1175. //}
  1176. highlight := elastic.NewHighlight()
  1177. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  1178. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1179. mustMap := make([]interface{}, 0)
  1180. mustNotMap := make([]interface{}, 0)
  1181. //指标来源
  1182. if showSysId > 0 {
  1183. mustMap = append(mustMap, map[string]interface{}{
  1184. "term": map[string]interface{}{
  1185. "SysUserId": showSysId,
  1186. //"Frequency.keyword": "月度",
  1187. },
  1188. })
  1189. }
  1190. mustMap = append(mustMap, map[string]interface{}{
  1191. "terms": map[string]interface{}{
  1192. "Source": sourceList,
  1193. },
  1194. })
  1195. //关键字匹配
  1196. //shouldMap := map[string]interface{}{
  1197. // "should": []interface{}{
  1198. // map[string]interface{}{
  1199. // "match": map[string]interface{}{
  1200. // "ChartName": keywordStr,
  1201. // //"Frequency.keyword": "月度",
  1202. // },
  1203. // },
  1204. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1205. // map[string]interface{}{
  1206. // "match": map[string]interface{}{
  1207. // "ChartName": map[string]interface{}{
  1208. // "query": keywordStr,
  1209. // "operator": "and",
  1210. // },
  1211. // //"Frequency.keyword": "月度",
  1212. // },
  1213. // },
  1214. // map[string]interface{}{
  1215. // "match": map[string]interface{}{
  1216. // "ChartNameEn": keywordStr,
  1217. // //"Frequency.keyword": "月度",
  1218. // },
  1219. // },
  1220. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1221. // map[string]interface{}{
  1222. // "match": map[string]interface{}{
  1223. // "ChartNameEn": map[string]interface{}{
  1224. // "query": keywordStr,
  1225. // "operator": "and",
  1226. // },
  1227. // //"Frequency.keyword": "月度",
  1228. // },
  1229. // },
  1230. // },
  1231. //}
  1232. // 默认使用中文名字字段去匹配
  1233. keywordNameKey := `ChartName`
  1234. // 如果没有中文,则使用英文名称字段去匹配
  1235. if !utils.ContainsChinese(keywordStr) {
  1236. keywordNameKey = `ChartNameEn`
  1237. }
  1238. shouldMap := map[string]interface{}{
  1239. "should": []interface{}{
  1240. map[string]interface{}{
  1241. "match": map[string]interface{}{
  1242. keywordNameKey: keywordStr,
  1243. //"Frequency.keyword": "月度",
  1244. },
  1245. },
  1246. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1247. map[string]interface{}{
  1248. "match": map[string]interface{}{
  1249. keywordNameKey: map[string]interface{}{
  1250. "query": keywordStr,
  1251. "operator": "and",
  1252. },
  1253. //"Frequency.keyword": "月度",
  1254. },
  1255. },
  1256. },
  1257. }
  1258. mustMap = append(mustMap, map[string]interface{}{
  1259. "bool": shouldMap,
  1260. })
  1261. // noPermissionEdbInfoIdList 无权限指标id
  1262. if len(noPermissionChartIdList) > 0 {
  1263. mustNotMap = append(mustNotMap, map[string]interface{}{
  1264. "terms": map[string]interface{}{
  1265. "ChartInfoId": noPermissionChartIdList,
  1266. //"Frequency.keyword": "月度",
  1267. },
  1268. })
  1269. }
  1270. queryMap := map[string]interface{}{
  1271. "query": map[string]interface{}{
  1272. "bool": map[string]interface{}{
  1273. "must": mustMap,
  1274. "must_not": mustNotMap,
  1275. //"should": shouldMap,
  1276. },
  1277. },
  1278. }
  1279. //根据条件数量统计
  1280. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1281. total, err = requestTotalHits.Do(context.Background())
  1282. if err != nil {
  1283. return
  1284. }
  1285. // 分页查询
  1286. queryMap["from"] = from
  1287. queryMap["size"] = size
  1288. queryMap["highlight"] = map[string]interface{}{
  1289. "fields": map[string]interface{}{
  1290. keywordNameKey: map[string]interface{}{},
  1291. },
  1292. "pre_tags": "<span style=\"color:#0052D9\">",
  1293. "post_tags": "</span>",
  1294. }
  1295. jsonBytes, _ := json.Marshal(queryMap)
  1296. fmt.Println(string(jsonBytes))
  1297. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  1298. //requestJson, err := json.Marshal(request)
  1299. //if err != nil {
  1300. // fmt.Println("requestJson err:", err)
  1301. //}
  1302. //fmt.Println("requestJson ", string(requestJson))
  1303. searchMap := make(map[string]string)
  1304. searchResp, err := request.Do(context.Background())
  1305. if err != nil {
  1306. return
  1307. }
  1308. fmt.Println(searchResp)
  1309. fmt.Println(searchResp.Status)
  1310. if searchResp.Status != 0 {
  1311. return
  1312. }
  1313. if searchResp.Hits != nil {
  1314. for _, v := range searchResp.Hits.Hits {
  1315. if _, ok := searchMap[v.Id]; !ok {
  1316. itemJson, tmpErr := v.Source.MarshalJSON()
  1317. if tmpErr != nil {
  1318. err = tmpErr
  1319. fmt.Println("movieJson err:", err)
  1320. return
  1321. }
  1322. chartInfoItem := new(data_manage.ChartInfoMore)
  1323. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  1324. if err != nil {
  1325. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  1326. err = tmpErr
  1327. return
  1328. }
  1329. if len(v.Highlight[keywordNameKey]) > 0 {
  1330. //chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  1331. chartInfoItem.SearchText = v.Highlight[keywordNameKey][0]
  1332. } else {
  1333. chartInfoItem.SearchText = chartInfoItem.ChartName
  1334. }
  1335. list = append(list, chartInfoItem)
  1336. searchMap[v.Id] = v.Id
  1337. }
  1338. }
  1339. }
  1340. //for _, v := range result {
  1341. // fmt.Println(v)
  1342. //}
  1343. return
  1344. }
  1345. // EsAddOrEditDataInterface 新增/修改es中的数据
  1346. func EsAddOrEditDataInterface(indexName, docId string, item interface{}) (err error) {
  1347. defer func() {
  1348. if err != nil {
  1349. fmt.Println("EsAddOrEditData Err:", err.Error())
  1350. }
  1351. }()
  1352. client := utils.EsClient
  1353. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  1354. if err != nil {
  1355. fmt.Println("新增失败:", err.Error())
  1356. return err
  1357. }
  1358. fmt.Println(resp)
  1359. if resp.Status == 0 {
  1360. fmt.Println("新增成功", resp.Result)
  1361. err = nil
  1362. } else {
  1363. fmt.Println("AddData", resp.Status, resp.Result)
  1364. }
  1365. return
  1366. }
  1367. // SearchMyChartInfoData 查询es中的我的图表数据
  1368. func SearchMyChartInfoData(indexName, keywordStr string, adminId int, noPermissionChartIdList []int, from, size int) (list []*data_manage.MyChartList, total int64, err error) {
  1369. list = make([]*data_manage.MyChartList, 0)
  1370. defer func() {
  1371. if err != nil {
  1372. fmt.Println("EsAddOrEditData Err:", err.Error())
  1373. }
  1374. }()
  1375. client := utils.EsClient
  1376. //queryString := elastic.NewQueryStringQuery(keywordStr)
  1377. //boolQueryJson, err := json.Marshal(queryString)
  1378. //if err != nil {
  1379. // fmt.Println("boolQueryJson err:", err)
  1380. //} else {
  1381. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  1382. //}
  1383. highlight := elastic.NewHighlight()
  1384. highlight = highlight.Fields(elastic.NewHighlighterField("ChartName"))
  1385. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1386. mustMap := make([]interface{}, 0)
  1387. mustNotMap := make([]interface{}, 0)
  1388. //指标来源
  1389. if adminId > 0 {
  1390. mustMap = append(mustMap, map[string]interface{}{
  1391. "term": map[string]interface{}{
  1392. "AdminId": adminId,
  1393. //"Frequency.keyword": "月度",
  1394. },
  1395. })
  1396. }
  1397. //关键字匹配
  1398. //shouldMap := map[string]interface{}{
  1399. // "should": []interface{}{
  1400. // map[string]interface{}{
  1401. // "match": map[string]interface{}{
  1402. // "ChartName": keywordStr,
  1403. // //"Frequency.keyword": "月度",
  1404. // },
  1405. // },
  1406. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1407. // map[string]interface{}{
  1408. // "match": map[string]interface{}{
  1409. // "ChartName": map[string]interface{}{
  1410. // "query": keywordStr,
  1411. // "operator": "and",
  1412. // },
  1413. // //"Frequency.keyword": "月度",
  1414. // },
  1415. // },
  1416. // map[string]interface{}{
  1417. // "match": map[string]interface{}{
  1418. // "ChartNameEn": keywordStr,
  1419. // //"Frequency.keyword": "月度",
  1420. // },
  1421. // },
  1422. // // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1423. // map[string]interface{}{
  1424. // "match": map[string]interface{}{
  1425. // "ChartNameEn": map[string]interface{}{
  1426. // "query": keywordStr,
  1427. // "operator": "and",
  1428. // },
  1429. // //"Frequency.keyword": "月度",
  1430. // },
  1431. // },
  1432. // },
  1433. //}
  1434. // 默认使用中文名字字段去匹配
  1435. keywordNameKey := `ChartName`
  1436. // 如果没有中文,则使用英文名称字段去匹配
  1437. if !utils.ContainsChinese(keywordStr) {
  1438. keywordNameKey = `ChartNameEn`
  1439. }
  1440. shouldMap := map[string]interface{}{
  1441. "should": []interface{}{
  1442. map[string]interface{}{
  1443. "match": map[string]interface{}{
  1444. keywordNameKey: keywordStr,
  1445. //"Frequency.keyword": "月度",
  1446. },
  1447. },
  1448. // 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
  1449. map[string]interface{}{
  1450. "match": map[string]interface{}{
  1451. keywordNameKey: map[string]interface{}{
  1452. "query": keywordStr,
  1453. "operator": "and",
  1454. },
  1455. //"Frequency.keyword": "月度",
  1456. },
  1457. },
  1458. },
  1459. }
  1460. mustMap = append(mustMap, map[string]interface{}{
  1461. "bool": shouldMap,
  1462. })
  1463. // noPermissionEdbInfoIdList 无权限指标id
  1464. if len(noPermissionChartIdList) > 0 {
  1465. mustNotMap = append(mustNotMap, map[string]interface{}{
  1466. "terms": map[string]interface{}{
  1467. "ChartInfoId": noPermissionChartIdList,
  1468. //"Frequency.keyword": "月度",
  1469. },
  1470. })
  1471. }
  1472. queryMap := map[string]interface{}{
  1473. "query": map[string]interface{}{
  1474. "bool": map[string]interface{}{
  1475. "must": mustMap,
  1476. "must_not": mustNotMap,
  1477. //"should": shouldMap,
  1478. },
  1479. },
  1480. }
  1481. //根据条件数量统计
  1482. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1483. total, err = requestTotalHits.Do(context.Background())
  1484. if err != nil {
  1485. return
  1486. }
  1487. // 分页查询
  1488. queryMap["from"] = from
  1489. queryMap["size"] = size
  1490. queryMap["highlight"] = map[string]interface{}{
  1491. "fields": map[string]interface{}{
  1492. keywordNameKey: map[string]interface{}{},
  1493. },
  1494. "pre_tags": "<span style=\"color:#0052D9\">",
  1495. "post_tags": "</span>",
  1496. }
  1497. jsonBytes, _ := json.Marshal(queryMap)
  1498. fmt.Println(string(jsonBytes))
  1499. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  1500. //requestJson, err := json.Marshal(request)
  1501. //if err != nil {
  1502. // fmt.Println("requestJson err:", err)
  1503. //}
  1504. //fmt.Println("requestJson ", string(requestJson))
  1505. searchMap := make(map[string]string)
  1506. searchResp, err := request.Do(context.Background())
  1507. if err != nil {
  1508. return
  1509. }
  1510. fmt.Println(searchResp)
  1511. fmt.Println(searchResp.Status)
  1512. if searchResp.Status != 0 {
  1513. return
  1514. }
  1515. if searchResp.Hits != nil {
  1516. for _, v := range searchResp.Hits.Hits {
  1517. if _, ok := searchMap[v.Id]; !ok {
  1518. itemJson, tmpErr := v.Source.MarshalJSON()
  1519. if tmpErr != nil {
  1520. err = tmpErr
  1521. fmt.Println("movieJson err:", err)
  1522. return
  1523. }
  1524. chartInfoItem := new(data_manage.MyChartList)
  1525. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  1526. if err != nil {
  1527. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  1528. err = tmpErr
  1529. return
  1530. }
  1531. if len(v.Highlight[keywordNameKey]) > 0 {
  1532. //chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  1533. chartInfoItem.SearchText = v.Highlight[keywordNameKey][0]
  1534. } else {
  1535. chartInfoItem.SearchText = chartInfoItem.ChartName
  1536. }
  1537. list = append(list, chartInfoItem)
  1538. searchMap[v.Id] = v.Id
  1539. }
  1540. }
  1541. }
  1542. //for _, v := range result {
  1543. // fmt.Println(v)
  1544. //}
  1545. return
  1546. }
  1547. // SearchEdbInfoDataByAdminId 查询es中的指标数据
  1548. func SearchEdbInfoDataByAdminId(indexName, keywordStr string, from, size, filterSource, source int, edbInfoType uint8, frequency string, adminId int) (total int64, list []*data_manage.EdbInfoList, err error) {
  1549. list = make([]*data_manage.EdbInfoList, 0)
  1550. defer func() {
  1551. if err != nil {
  1552. fmt.Println("EsAddOrEditData Err:", err.Error())
  1553. }
  1554. }()
  1555. highlight := elastic.NewHighlight()
  1556. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  1557. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  1558. //var source map[string]interface{}
  1559. //source := map[string]interface{}{
  1560. // "query": map[string]interface{}{
  1561. // "match_all": map[string]interface{}{},
  1562. // },
  1563. //}
  1564. mustMap := make([]interface{}, 0)
  1565. mustNotMap := make([]interface{}, 0)
  1566. //source := map[string]interface{}{
  1567. // "query": map[string]interface{}{
  1568. // "bool": map[string]interface{}{
  1569. // "must": map[string]interface{}{
  1570. // "query_string": map[string]interface{}{
  1571. // "query": keywordStr,
  1572. // "fields": []string{"EdbCode", "EdbName"},
  1573. // },
  1574. // },
  1575. // },
  1576. // },
  1577. //}
  1578. switch filterSource {
  1579. case 2:
  1580. //source = map[string]interface{}{
  1581. // "query": map[string]interface{}{
  1582. // "bool": map[string]interface{}{
  1583. // "must": map[string]interface{}{
  1584. // "query_string": map[string]interface{}{
  1585. // "query": keywordStr,
  1586. // },
  1587. // },
  1588. // "filter": []interface{}{
  1589. // map[string]interface{}{
  1590. // "term": map[string]interface{}{
  1591. // "Frequency.keyword": "月度",
  1592. // },
  1593. // }},
  1594. // },
  1595. // },
  1596. //}
  1597. mustMap = []interface{}{
  1598. map[string]interface{}{
  1599. "term": map[string]interface{}{
  1600. "Frequency.keyword": "月度",
  1601. //"Frequency.keyword": "月度",
  1602. },
  1603. },
  1604. }
  1605. case 3:
  1606. //source = map[string]interface{}{
  1607. // "query": map[string]interface{}{
  1608. // "bool": map[string]interface{}{
  1609. // "must": map[string]interface{}{
  1610. // "query_string": map[string]interface{}{
  1611. // "query": keywordStr,
  1612. // },
  1613. // },
  1614. // "must_not": []interface{}{
  1615. // map[string]interface{}{
  1616. // "match": map[string]interface{}{
  1617. // "Frequency.keyword": "日度",
  1618. // },
  1619. // }},
  1620. // },
  1621. // },
  1622. //}
  1623. ////注释掉,所有频度都可以变频 2022-08-31 14:31:28
  1624. //mustNotMap = []interface{}{
  1625. // map[string]interface{}{
  1626. // "match": map[string]interface{}{
  1627. // "Frequency.keyword": "日度",
  1628. // //"Frequency.keyword": "月度",
  1629. // },
  1630. // },
  1631. //}
  1632. case 4:
  1633. //source = map[string]interface{}{
  1634. // "query": map[string]interface{}{
  1635. // "bool": map[string]interface{}{
  1636. // "must": map[string]interface{}{
  1637. // "query_string": map[string]interface{}{
  1638. // "query": keywordStr,
  1639. // },
  1640. // },
  1641. // "filter": []interface{}{
  1642. // map[string]interface{}{
  1643. // "term": map[string]interface{}{
  1644. // "EdbType": 1,
  1645. // },
  1646. // }},
  1647. // },
  1648. // },
  1649. //}
  1650. mustMap = []interface{}{
  1651. map[string]interface{}{
  1652. "term": map[string]interface{}{
  1653. "EdbType": 1,
  1654. },
  1655. },
  1656. }
  1657. case 5:
  1658. mustMap = []interface{}{
  1659. map[string]interface{}{
  1660. "term": map[string]interface{}{
  1661. "Source": 6,
  1662. },
  1663. },
  1664. }
  1665. case 6:
  1666. mustNotMap = []interface{}{
  1667. map[string]interface{}{
  1668. "match": map[string]interface{}{
  1669. "Frequency.keyword": "年度",
  1670. },
  1671. },
  1672. }
  1673. }
  1674. //指标来源
  1675. if source > 0 {
  1676. mustMap = append(mustMap, map[string]interface{}{
  1677. "term": map[string]interface{}{
  1678. "Source": source,
  1679. //"Frequency.keyword": "月度",
  1680. },
  1681. })
  1682. }
  1683. if frequency != "" {
  1684. mustMap = append(mustMap, map[string]interface{}{
  1685. "term": map[string]interface{}{
  1686. "Frequency.keyword": frequency,
  1687. //"Frequency.keyword": "月度",
  1688. },
  1689. })
  1690. }
  1691. // 指标类型:普通指标、预算指标
  1692. mustMap = append(mustMap, map[string]interface{}{
  1693. "term": map[string]interface{}{
  1694. "EdbInfoType": edbInfoType,
  1695. },
  1696. })
  1697. //普通指标
  1698. //mustMap = append(mustMap, map[string]interface{}{
  1699. // "term": map[string]interface{}{
  1700. // "EdbInfoType": 0,
  1701. // //"Frequency.keyword": "月度",
  1702. // },
  1703. //})
  1704. //关键字匹配
  1705. //shouldMap := map[string]interface{}{
  1706. // "should": []interface{}{
  1707. // map[string]interface{}{
  1708. // "match": map[string]interface{}{
  1709. // "EdbCode": keywordStr,
  1710. // //"Frequency.keyword": "月度",
  1711. // },
  1712. // },
  1713. // map[string]interface{}{
  1714. // "match": map[string]interface{}{
  1715. // "EdbName": keywordStr,
  1716. // //"Frequency.keyword": "月度",
  1717. // },
  1718. // },
  1719. // },
  1720. //}
  1721. // 默认使用中文名字字段去匹配
  1722. keywordNameKey := `EdbName`
  1723. // 如果没有中文,则使用英文名称字段去匹配
  1724. if !utils.ContainsChinese(keywordStr) {
  1725. keywordNameKey = `EdbNameEn`
  1726. }
  1727. shouldMap := map[string]interface{}{
  1728. "should": []interface{}{
  1729. map[string]interface{}{
  1730. "match": map[string]interface{}{
  1731. "EdbCode": keywordStr,
  1732. //"Frequency.keyword": "月度",
  1733. },
  1734. },
  1735. map[string]interface{}{
  1736. "match": map[string]interface{}{
  1737. keywordNameKey: keywordStr,
  1738. //"Frequency.keyword": "月度",
  1739. },
  1740. },
  1741. },
  1742. }
  1743. mustMap = append(mustMap, map[string]interface{}{
  1744. "bool": shouldMap,
  1745. })
  1746. //创建人
  1747. if adminId > 0 {
  1748. mustMap = append(mustMap, map[string]interface{}{
  1749. "term": map[string]interface{}{
  1750. "SysUserId": adminId,
  1751. },
  1752. })
  1753. }
  1754. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMap, from, size)
  1755. }
  1756. // SearchExcelInfoData 查询es中的表格
  1757. func SearchExcelInfoData(indexName, keyword string, source, adminId int, queryIds, exceptIds []int, from, size int) (total int64, list []*excel.SearchExcelInfo, err error) {
  1758. list = make([]*excel.SearchExcelInfo, 0)
  1759. defer func() {
  1760. if err != nil {
  1761. tips := fmt.Sprintf("SearchExcelInfoData err: %v", err)
  1762. utils.FileLog.Info(tips)
  1763. }
  1764. }()
  1765. client := utils.EsClient
  1766. mustMap := make([]interface{}, 0)
  1767. mustNotMap := make([]interface{}, 0)
  1768. mustMap = append(mustMap, map[string]interface{}{
  1769. "term": map[string]interface{}{
  1770. "IsDelete": 0,
  1771. },
  1772. })
  1773. // 表格名称
  1774. shouldMap := make(map[string]interface{}, 0)
  1775. if keyword != "" {
  1776. shouldMap["should"] = []interface{}{
  1777. map[string]interface{}{
  1778. "match": map[string]interface{}{
  1779. "ExcelName": keyword,
  1780. },
  1781. },
  1782. }
  1783. //shouldMap := map[string]interface{}{
  1784. // "should": []interface{}{
  1785. // map[string]interface{}{
  1786. // "match": map[string]interface{}{
  1787. // "ExcelName": keyword,
  1788. // },
  1789. // },
  1790. // },
  1791. //}
  1792. }
  1793. // 表格来源
  1794. if source > 0 {
  1795. mustMap = append(mustMap, map[string]interface{}{
  1796. "term": map[string]interface{}{
  1797. "Source": source,
  1798. },
  1799. })
  1800. }
  1801. // 创建人
  1802. if adminId > 0 {
  1803. mustMap = append(mustMap, map[string]interface{}{
  1804. "term": map[string]interface{}{
  1805. "SysUserId": adminId,
  1806. },
  1807. })
  1808. }
  1809. // 查询和排除的表格IDs
  1810. if len(queryIds) > 0 {
  1811. mustMap = append(mustMap, map[string]interface{}{
  1812. "terms": map[string]interface{}{
  1813. "ExcelInfoId": queryIds,
  1814. },
  1815. })
  1816. }
  1817. if len(exceptIds) > 0 {
  1818. mustNotMap = append(mustNotMap, map[string]interface{}{
  1819. "terms": map[string]interface{}{
  1820. "ExcelInfoId": exceptIds,
  1821. },
  1822. })
  1823. }
  1824. // 关键字匹配
  1825. mustMap = append(mustMap, map[string]interface{}{
  1826. "bool": shouldMap,
  1827. })
  1828. queryMap := map[string]interface{}{
  1829. "query": map[string]interface{}{
  1830. "bool": map[string]interface{}{
  1831. "must": mustMap,
  1832. "must_not": mustNotMap,
  1833. //"should": shouldMap,
  1834. },
  1835. },
  1836. }
  1837. //jsonBytes, _ := json.Marshal(queryMap)
  1838. //fmt.Println(string(jsonBytes))
  1839. // 根据条件数量统计
  1840. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  1841. t, e := requestTotalHits.Do(context.Background())
  1842. if e != nil {
  1843. err = fmt.Errorf("total hits err: %v", e)
  1844. return
  1845. }
  1846. total = t
  1847. // 表格名称高亮,分页
  1848. highlightKeyName := "ExcelName"
  1849. queryMap["highlight"] = map[string]interface{}{
  1850. "fields": map[string]interface{}{
  1851. highlightKeyName: map[string]interface{}{},
  1852. },
  1853. "pre_tags": "<span style=\"color:#0052D9\">",
  1854. "post_tags": "</span>",
  1855. }
  1856. queryMap["from"] = from
  1857. queryMap["size"] = size
  1858. //jsonBytes, _ := json.Marshal(queryMap)
  1859. //fmt.Println(string(jsonBytes))
  1860. request := client.Search(indexName).Source(queryMap)
  1861. searchResp, e := request.Do(context.Background())
  1862. if e != nil {
  1863. err = fmt.Errorf("search do err: %v", e)
  1864. return
  1865. }
  1866. //fmt.Println(searchResp)
  1867. if searchResp.Status != 0 {
  1868. return
  1869. }
  1870. if searchResp.Hits == nil {
  1871. return
  1872. }
  1873. searchMap := make(map[string]string)
  1874. for _, v := range searchResp.Hits.Hits {
  1875. if _, ok := searchMap[v.Id]; ok {
  1876. continue
  1877. }
  1878. j, e := v.Source.MarshalJSON()
  1879. if e != nil {
  1880. err = fmt.Errorf("hits json err: %v", e)
  1881. return
  1882. }
  1883. item := new(excel.SearchExcelInfo)
  1884. if e = json.Unmarshal(j, &item); e != nil {
  1885. err = fmt.Errorf("hits json unmarshal err: %v", e)
  1886. return
  1887. }
  1888. if len(v.Highlight[highlightKeyName]) > 0 {
  1889. item.SearchText = v.Highlight[highlightKeyName][0]
  1890. } else {
  1891. item.SearchText = item.ExcelName
  1892. }
  1893. list = append(list, item)
  1894. searchMap[v.Id] = v.Id
  1895. }
  1896. return
  1897. }