123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package stdlib
- import (
- "fmt"
- "log"
- "regexp"
- "strings"
- "app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
- "app.yhyue.com/moapp/jy_docs/services/model"
- jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
- "app.yhyue.com/moapp/jybase/common"
- elastic "app.yhyue.com/moapp/jybase/esv7"
- )
- const (
- Es_Query_All = `{"query":{"match_all":{}}%s}`
- Es_Query_Boosting = `{"query":{"boosting":{"positive":{"bool":{"must":[%s]}},"negative":{"bool":{"must":[%s]}},"negative_boost":2}}%s}`
- Es_Query_Bool = `{"query":{"bool":{"filter":[%s]}}%s}`
- Es_Query_Append = `,"_source":["id","docName","price","downTimes","viewTimes","docSummary","uploadDate","docFileSize","docPageSize","docFileType","previewImgId"],"from":%d,"size":%d`
- Es_Query_Highlight = `,"highlight":{"fields":{"docName":{},"docSummary":{"fragment_size":300,"number_of_fragments":1}}}`
- Es_Query_Sort = `,"sort":{%s}`
- Multi_Match = `{"multi_match":{"query":"%s","fields":["docName","docSummary^2"]}}`
- Multi_Match_Phrase = `{"multi_match":{"query":"%s","fields":["docName.docName_c","docSummary.docSummary_c"],"type":"phrase"}}`
- Terms = `{"terms":{"%s":[%s]}}`
- )
- var (
- Reg = regexp.MustCompile(`\s+`)
- )
- func FindDocumentById(id int) {
- log.Println(jyDocsRpcUtil.GetJyDocsDB().Exec("select * from ").Error)
- }
- func DocQuery(in *stdlib.DocQueryRequest, searchSource []string) *stdlib.DocQueryResponse {
- defer common.Catch()
- in.KeyWord = strings.TrimSpace(in.KeyWord)
- musts := []string{}
- negative_musts := []string{}
- sorts := []string{}
- //搜索词
- if in.KeyWord != "" {
- sorts = append(sorts, `"_score":"desc"`)
- for _, v := range strings.Split(in.KeyWord, " ") {
- v = strings.ReplaceAll(v, `"`, `\"`)
- musts = append(musts, fmt.Sprintf(Multi_Match, v))
- negative_musts = append(negative_musts, fmt.Sprintf(Multi_Match_Phrase, v))
- }
- }
- for _, v := range in.Sort {
- if strings.HasPrefix(v, "-") {
- sorts = append(sorts, fmt.Sprintf(`"%s":"desc"`, strings.TrimLeft(v, "-")))
- } else {
- sorts = append(sorts, fmt.Sprintf(`"%s":"asc"`, v))
- }
- }
- //分类
- if len(in.DocClass) > 0 {
- musts = append(musts, fmt.Sprintf(Terms, "docClass", `"`+strings.Join(in.DocClass, `","`)+`"`))
- }
- //标签
- if len(in.DocTag) > 0 {
- musts = append(musts, fmt.Sprintf(Terms, "docTags", `"`+strings.Join(in.DocTag, `","`)+`"`))
- }
- query := ""
- query_sort := ""
- if len(sorts) > 0 {
- query_sort = fmt.Sprintf(Es_Query_Sort, strings.Join(sorts, ","))
- }
- query_append := fmt.Sprintf(Es_Query_Append, (in.PageNum-1)*in.PageSize, in.PageSize)
- if searchSource != nil && len(searchSource) > 0 {
- musts = append(musts, fmt.Sprintf(Terms, "source", strings.Join(searchSource, `,`)))
- }
- if len(musts) == 0 {
- query = fmt.Sprintf(Es_Query_All, fmt.Sprint(query_append, query_sort))
- } else if in.KeyWord != "" {
- query = fmt.Sprintf(Es_Query_Boosting, strings.Join(musts, ","), strings.Join(negative_musts, ","), fmt.Sprint(query_append, query_sort, Es_Query_Highlight))
- } else {
- query = fmt.Sprintf(Es_Query_Bool, strings.Join(musts, ","), fmt.Sprint(query_append, query_sort))
- }
- log.Println("query:", query)
- total, list := elastic.GetBySearchType(model.Es_JyDoc, "dfs_query_then_fetch", query)
- docs := []*stdlib.Doc{}
- //获取我购买的文档
- if list != nil {
- myDocs := map[string]int64{}
- if in.UserId != "" {
- docIds, whs := []interface{}{}, []string{}
- for _, v := range *list {
- whs = append(whs, "?")
- docIds = append(docIds, common.ObjToString(v["id"]))
- }
- args := []interface{}{in.UserId, model.UserDocStatus_Normal, in.AppId}
- args = append(args, docIds...)
- userDocs := []*model.UserDoc{}
- 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)
- for _, v := range userDocs {
- myDocs[v.DocId] = 1
- }
- }
- for _, v := range *list {
- doc := &stdlib.Doc{
- DocId: common.ObjToString(v["id"]),
- DocName: common.ObjToString(v["docName"]),
- Price: common.Int64All(v["price"]),
- DocPageSize: common.Int64All(v["docPageSize"]),
- DocFileSize: common.Int64All(v["docFileSize"]),
- DownTimes: common.Int64All(v["downTimes"]),
- ViewTimes: common.Int64All(v["viewTimes"]),
- UploadDate: common.ObjToString(v["uploadDate"]),
- DocSummary: common.ObjToString(v["docSummary"]),
- DocFileType: model.DocFileType[common.IntAll(v["docFileType"])],
- PreviewImgId: common.ObjToString(v["previewImgId"]),
- }
- highlight, _ := v["highlight"].(map[string][]string)
- if len(highlight["docName"]) > 0 {
- doc.DocName = highlight["docName"][0]
- }
- if len(highlight["docSummary"]) > 0 {
- doc.DocSummary = highlight["docSummary"][0]
- }
- doc.IsDownload = myDocs[doc.DocId]
- docs = append(docs, doc)
- }
- }
- return &stdlib.DocQueryResponse{
- Total: total,
- Docs: docs,
- Code: 1,
- }
- }
|