docService.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package stdlib
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. "app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
  8. "app.yhyue.com/moapp/jy_docs/services/model"
  9. jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
  10. "app.yhyue.com/moapp/jybase/common"
  11. elastic "app.yhyue.com/moapp/jybase/esv7"
  12. )
  13. const (
  14. Es_Query_All = `{"query":{"match_all":{}}%s}`
  15. Es_Query_Boosting = `{"query":{"boosting":{"positive":{"bool":{"must":[%s]}},"negative":{"bool":{"must":[%s]}},"negative_boost":2}}%s}`
  16. Es_Query_Bool = `{"query":{"bool":{"filter":[%s]}}%s}`
  17. Es_Query_Append = `,"_source":["id","docName","price","downTimes","viewTimes","docSummary","uploadDate","docFileSize","docPageSize","docFileType","previewImgId"],"from":%d,"size":%d`
  18. Es_Query_Highlight = `,"highlight":{"fields":{"docName":{},"docSummary":{"fragment_size":100,"number_of_fragments":1}}}`
  19. Es_Query_Sort = `,"sort":{%s}`
  20. Multi_Match = `{"multi_match":{"query":"%s","fields":["docName","docSummary^2"]}}`
  21. Multi_Match_Phrase = `{"multi_match":{"query":"%s","fields":["docName.docName_c","docSummary.docSummary_c"],"type":"phrase"}}`
  22. Terms = `{"terms":{"%s":[%s]}}`
  23. )
  24. var (
  25. Reg = regexp.MustCompile(`\s+`)
  26. )
  27. func FindDocumentById(id int) {
  28. log.Println(jyDocsRpcUtil.GetJyDocsDB().Exec("select * from ").Error)
  29. }
  30. func DocQuery(in *stdlib.DocQueryRequest) *stdlib.DocQueryResponse {
  31. defer common.Catch()
  32. in.KeyWord = strings.TrimSpace(in.KeyWord)
  33. musts := []string{}
  34. negative_musts := []string{}
  35. sorts := []string{}
  36. //搜索词
  37. if in.KeyWord != "" {
  38. sorts = append(sorts, `"_score":"desc"`)
  39. for _, v := range strings.Split(in.KeyWord, " ") {
  40. v = strings.ReplaceAll(v, `"`, `\"`)
  41. musts = append(musts, fmt.Sprintf(Multi_Match, v))
  42. negative_musts = append(negative_musts, fmt.Sprintf(Multi_Match_Phrase, v))
  43. }
  44. }
  45. for _, v := range in.Sort {
  46. if strings.HasPrefix(v, "-") {
  47. sorts = append(sorts, fmt.Sprintf(`"%s":"desc"`, strings.TrimLeft(v, "-")))
  48. } else {
  49. sorts = append(sorts, fmt.Sprintf(`"%s":"asc"`, v))
  50. }
  51. }
  52. //分类
  53. if len(in.DocClass) > 0 {
  54. musts = append(musts, fmt.Sprintf(Terms, "docClass", `"`+strings.Join(in.DocClass, `","`)+`"`))
  55. }
  56. //标签
  57. if len(in.DocTag) > 0 {
  58. musts = append(musts, fmt.Sprintf(Terms, "docTags", `"`+strings.Join(in.DocTag, `","`)+`"`))
  59. }
  60. query := ""
  61. query_sort := ""
  62. if len(sorts) > 0 {
  63. query_sort = fmt.Sprintf(Es_Query_Sort, strings.Join(sorts, ","))
  64. }
  65. query_append := fmt.Sprintf(Es_Query_Append, (in.PageNum-1)*in.PageSize, in.PageSize)
  66. if len(musts) == 0 {
  67. query = fmt.Sprintf(Es_Query_All, fmt.Sprint(query_append, query_sort))
  68. } else if in.KeyWord != "" {
  69. query = fmt.Sprintf(Es_Query_Boosting, strings.Join(musts, ","), strings.Join(negative_musts, ","), fmt.Sprint(query_append, query_sort, Es_Query_Highlight))
  70. } else {
  71. query = fmt.Sprintf(Es_Query_Bool, strings.Join(musts, ","), fmt.Sprint(query_append, query_sort))
  72. }
  73. log.Println("query:", query)
  74. total, list := elastic.GetBySearchType(model.Es_JyDoc, "dfs_query_then_fetch", query)
  75. docs := []*stdlib.Doc{}
  76. //获取我购买的文档
  77. if list != nil {
  78. myDocs := map[string]int64{}
  79. if in.UserId != "" {
  80. docIds, whs := []interface{}{}, []string{}
  81. for _, v := range *list {
  82. whs = append(whs, "?")
  83. docIds = append(docIds, common.ObjToString(v["id"]))
  84. }
  85. args := []interface{}{in.UserId, model.UserDocStatus_Normal, in.AppId}
  86. args = append(args, docIds...)
  87. userDocs := []*model.UserDoc{}
  88. jyDocsRpcUtil.GetJyDocsDB().Exec(`select docId from user_doc where userId=? and isDelete=? and appId=? and isDownload=1 and docId in (`+strings.Join(whs, ",")+`)`, args...).Find(&userDocs)
  89. for _, v := range userDocs {
  90. myDocs[v.DocId] = 1
  91. }
  92. }
  93. for _, v := range *list {
  94. doc := &stdlib.Doc{
  95. DocId: common.ObjToString(v["id"]),
  96. DocName: common.ObjToString(v["docName"]),
  97. Price: common.Int64All(v["price"]),
  98. DocPageSize: common.Int64All(v["docPageSize"]),
  99. DocFileSize: common.Int64All(v["docFileSize"]),
  100. DownTimes: common.Int64All(v["downTimes"]),
  101. ViewTimes: common.Int64All(v["viewTimes"]),
  102. UploadDate: common.ObjToString(v["uploadDate"]),
  103. DocSummary: common.ObjToString(v["docSummary"]),
  104. DocFileType: model.DocFileType[common.IntAll(v["docFileType"])],
  105. PreviewImgId: common.ObjToString(v["previewImgId"]),
  106. }
  107. highlight, _ := v["highlight"].(map[string][]string)
  108. if len(highlight["docName"]) > 0 {
  109. doc.DocName = highlight["docName"][0]
  110. }
  111. if len(highlight["docSummary"]) > 0 {
  112. doc.DocSummary = highlight["docSummary"][0]
  113. }
  114. doc.IsDownload = myDocs[doc.DocId]
  115. docs = append(docs, doc)
  116. }
  117. }
  118. return &stdlib.DocQueryResponse{
  119. Total: total,
  120. Docs: docs,
  121. Code: 1,
  122. }
  123. }