docService.go 6.1 KB

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