docService.go 5.6 KB

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