recommendanswerlogic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package logic
  2. import "C"
  3. import (
  4. cm "app.yhyue.com/moapp/jybase/common"
  5. . "app.yhyue.com/moapp/jybase/encrypt"
  6. elastic "app.yhyue.com/moapp/jybase/esv1"
  7. "context"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "knowledgeBase/rpc/knowledge/internal/svc"
  10. "knowledgeBase/rpc/knowledge/knowledgeclient"
  11. "knowledgeBase/rpc/knowledge/util"
  12. "log"
  13. )
  14. type RecommendAnswerLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewRecommendAnswerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecommendAnswerLogic {
  20. return &RecommendAnswerLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *RecommendAnswerLogic) RecommendAnswer(in *knowledgeclient.FindAnswerReq) (*knowledgeclient.RecommendAnswerResp, error) {
  27. var (
  28. keyWords = ""
  29. searchField = `"answer","question"`
  30. answers []*knowledgeclient.Question
  31. result = &knowledgeclient.RecommendAnswerResp{}
  32. )
  33. //根据问题进行分词
  34. keyWords = util.HttpDo(in.Question)
  35. log.Println("问题分词关键字:", keyWords)
  36. if keyWords != "" {
  37. var query = util.DSL4SearchByKwsOrid(keyWords, SE.Decode4Hex(in.RobotEntId))
  38. res := elastic.GetAllByNgram(C.Es.Index, C.Es.Type, query, "", "", searchField, 0, 3, 0, false)
  39. if res != nil && len(*res) > 0 {
  40. for _, val := range *res {
  41. answers = append(answers, &knowledgeclient.Question{
  42. Question: cm.ObjToString(val["question"]),
  43. Answer: cm.ObjToString(val["answer"]),
  44. })
  45. }
  46. }
  47. result.ErrorCode = 0
  48. result.ErrorMsg = "请求成功"
  49. result.Data = answers
  50. } else {
  51. result.ErrorCode = -1
  52. result.ErrorMsg = "分词出错"
  53. result.Data = answers
  54. }
  55. return result, nil
  56. }