docService.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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":300,"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, searchSource []string) *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 searchSource != nil && len(searchSource) > 0 {
  67. musts = append(musts, fmt.Sprintf(Terms, "source", strings.Join(searchSource, `,`)))
  68. }
  69. if len(musts) == 0 {
  70. query = fmt.Sprintf(Es_Query_All, fmt.Sprint(query_append, query_sort))
  71. } else if in.KeyWord != "" {
  72. query = fmt.Sprintf(Es_Query_Boosting, strings.Join(musts, ","), strings.Join(negative_musts, ","), fmt.Sprint(query_append, query_sort, Es_Query_Highlight))
  73. } else {
  74. query = fmt.Sprintf(Es_Query_Bool, strings.Join(musts, ","), fmt.Sprint(query_append, query_sort))
  75. }
  76. log.Println("query:", query)
  77. total, list := elastic.GetBySearchType(model.Es_JyDoc, "dfs_query_then_fetch", query)
  78. docs := []*stdlib.Doc{}
  79. //获取我购买的文档
  80. if list != nil {
  81. myDocs := map[string]int64{}
  82. if in.UserId != "" {
  83. docIds, whs := []interface{}{}, []string{}
  84. for _, v := range *list {
  85. whs = append(whs, "?")
  86. docIds = append(docIds, common.ObjToString(v["id"]))
  87. }
  88. args := []interface{}{in.UserId, model.UserDocStatus_Normal, in.AppId}
  89. args = append(args, docIds...)
  90. userDocs := []*model.UserDoc{}
  91. 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)
  92. for _, v := range userDocs {
  93. myDocs[v.DocId] = 1
  94. }
  95. }
  96. for _, v := range *list {
  97. doc := &stdlib.Doc{
  98. DocId: common.ObjToString(v["id"]),
  99. DocName: common.ObjToString(v["docName"]),
  100. Price: common.Int64All(v["price"]),
  101. DocPageSize: common.Int64All(v["docPageSize"]),
  102. DocFileSize: common.Int64All(v["docFileSize"]),
  103. DownTimes: common.Int64All(v["downTimes"]),
  104. ViewTimes: common.Int64All(v["viewTimes"]),
  105. UploadDate: common.ObjToString(v["uploadDate"]),
  106. DocSummary: common.ObjToString(v["docSummary"]),
  107. DocFileType: model.DocFileType[common.IntAll(v["docFileType"])],
  108. PreviewImgId: common.ObjToString(v["previewImgId"]),
  109. }
  110. highlight, _ := v["highlight"].(map[string][]string)
  111. if len(highlight["docName"]) > 0 {
  112. doc.DocName = highlight["docName"][0]
  113. }
  114. if len(highlight["docSummary"]) > 0 {
  115. doc.DocSummary = highlight["docSummary"][0]
  116. }
  117. doc.IsDownload = myDocs[doc.DocId]
  118. docs = append(docs, doc)
  119. }
  120. }
  121. return &stdlib.DocQueryResponse{
  122. Total: total,
  123. Docs: docs,
  124. Code: 1,
  125. }
  126. }