findanswerlogic.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package logic
  2. import (
  3. cm "app.yhyue.com/moapp/jybase/common"
  4. . "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/esv1"
  6. "context"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. . "knowledgeBase/rpc/knowledge/init"
  9. "knowledgeBase/rpc/knowledge/internal/svc"
  10. "knowledgeBase/rpc/knowledge/knowledgeclient"
  11. "knowledgeBase/rpc/knowledge/util"
  12. )
  13. type FindAnswerLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewFindAnswerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindAnswerLogic {
  19. return &FindAnswerLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *FindAnswerLogic) FindAnswer(in *knowledgeclient.FindAnswerReq) (*knowledgeclient.FindAnswerResp, error) {
  26. var question knowledgeclient.Question
  27. robotEntId := SE.Decode4Hex(in.RobotEntId)
  28. //组装es query
  29. query := util.DSL4SmartResponse(in.Question, robotEntId, int(in.Type))
  30. res := elastic.Get(C.Es.Index, C.Es.Type, query)
  31. if res != nil && len(*res) > 0 {
  32. data := (*res)[0]
  33. question.Answer = cm.ObjToString(data["answer"])
  34. question.Question = cm.ObjToString(data["question"])
  35. }
  36. return &knowledgeclient.FindAnswerResp{
  37. ErrorCode: 0,
  38. ErrorMsg: "请求成功",
  39. Data: &question,
  40. }, nil
  41. }