docService.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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","source","productType","docTags"],"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. Term = `{"term":{"%s":%s}}`
  24. )
  25. var (
  26. Reg = regexp.MustCompile(`\s+`)
  27. )
  28. func FindDocumentById(id int) {
  29. log.Println(jyDocsRpcUtil.GetJyDocsDB().Exec("select * from ").Error)
  30. }
  31. func DocQuery(in *stdlib.DocQueryRequest) *stdlib.DocQueryResponse {
  32. defer common.Catch()
  33. in.KeyWord = strings.TrimSpace(in.KeyWord)
  34. musts := []string{}
  35. negative_musts := []string{}
  36. sorts := []string{}
  37. //搜索词
  38. if in.KeyWord != "" {
  39. sorts = append(sorts, `"_score":"desc"`)
  40. for _, v := range strings.Split(in.KeyWord, " ") {
  41. v = strings.ReplaceAll(v, `"`, `\"`)
  42. musts = append(musts, fmt.Sprintf(Multi_Match, v))
  43. negative_musts = append(negative_musts, fmt.Sprintf(Multi_Match_Phrase, v))
  44. }
  45. }
  46. for _, v := range in.Sort {
  47. if strings.HasPrefix(v, "-") {
  48. sorts = append(sorts, fmt.Sprintf(`"%s":"desc"`, strings.TrimLeft(v, "-")))
  49. } else {
  50. sorts = append(sorts, fmt.Sprintf(`"%s":"asc"`, v))
  51. }
  52. }
  53. //分类
  54. if len(in.DocClass) > 0 {
  55. musts = append(musts, fmt.Sprintf(Terms, "docClass", `"`+strings.Join(in.DocClass, `","`)+`"`))
  56. }
  57. //标签
  58. if len(in.DocTag) > 0 {
  59. musts = append(musts, fmt.Sprintf(Terms, "docTags", `"`+strings.Join(in.DocTag, `","`)+`"`))
  60. }
  61. // 文件类型
  62. if in.DocFileType > 0 {
  63. musts = append(musts, fmt.Sprintf(Term, "docFileType", `"`+fmt.Sprintf("%d", in.DocFileType)+`"`))
  64. }
  65. // 商品类型
  66. if in.ProductType > 0 {
  67. musts = append(musts, fmt.Sprintf(Term, "productType", `"`+fmt.Sprintf("%d", in.ProductType)+`"`))
  68. }
  69. query := ""
  70. query_sort := ""
  71. if len(sorts) > 0 {
  72. query_sort = fmt.Sprintf(Es_Query_Sort, strings.Join(sorts, ","))
  73. }
  74. query_append := fmt.Sprintf(Es_Query_Append, (in.PageNum-1)*in.PageSize, in.PageSize)
  75. if len(musts) == 0 {
  76. query = fmt.Sprintf(Es_Query_All, fmt.Sprint(query_append, query_sort))
  77. } else if in.KeyWord != "" {
  78. query = fmt.Sprintf(Es_Query_Boosting, strings.Join(musts, ","), strings.Join(negative_musts, ","), fmt.Sprint(query_append, query_sort, Es_Query_Highlight))
  79. } else {
  80. query = fmt.Sprintf(Es_Query_Bool, strings.Join(musts, ","), fmt.Sprint(query_append, query_sort))
  81. }
  82. log.Println("query:", query)
  83. total, list := elastic.GetBySearchType(model.Es_JyDoc, "dfs_query_then_fetch", query)
  84. docs := []*stdlib.Doc{}
  85. //获取我购买的文档
  86. if list != nil {
  87. myDocs := map[string]int64{}
  88. if in.UserId != "" {
  89. docIds, whs := []interface{}{}, []string{}
  90. for _, v := range *list {
  91. whs = append(whs, "?")
  92. docIds = append(docIds, common.ObjToString(v["id"]))
  93. }
  94. args := []interface{}{in.UserId, model.UserDocStatus_Normal, in.AppId}
  95. args = append(args, docIds...)
  96. userDocs := []*model.UserDoc{}
  97. 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)
  98. for _, v := range userDocs {
  99. myDocs[v.DocId] = 1
  100. }
  101. }
  102. for _, v := range *list {
  103. doc := &stdlib.Doc{
  104. DocId: common.ObjToString(v["id"]),
  105. DocName: common.ObjToString(v["docName"]),
  106. DocPageSize: common.Int64All(v["docPageSize"]),
  107. DocFileSize: common.Int64All(v["docFileSize"]),
  108. DownTimes: common.Int64All(v["downTimes"]),
  109. ViewTimes: common.Int64All(v["viewTimes"]),
  110. UploadDate: common.ObjToString(v["uploadDate"]),
  111. DocSummary: common.ObjToString(v["docSummary"]),
  112. DocFileType: model.DocFileType[common.IntAll(v["docFileType"])],
  113. PreviewImgId: common.ObjToString(v["previewImgId"]),
  114. ProductType: common.Int64All(v["productType"]),
  115. Source: common.Int64All(v["source"]),
  116. DocTags: common.ObjToString(v["docTags"]),
  117. }
  118. highlight, _ := v["highlight"].(map[string][]string)
  119. if len(highlight["docName"]) > 0 {
  120. doc.DocName = highlight["docName"][0]
  121. }
  122. if len(highlight["docSummary"]) > 0 {
  123. doc.DocSummary = highlight["docSummary"][0]
  124. }
  125. doc.IsDownload = myDocs[doc.DocId]
  126. docs = append(docs, doc)
  127. }
  128. }
  129. return &stdlib.DocQueryResponse{
  130. Total: total,
  131. Docs: docs,
  132. Code: 1,
  133. }
  134. }