findanswerlogic.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package logic
  2. import (
  3. cm "app.yhyue.com/moapp/jybase/common"
  4. elastic "app.yhyue.com/moapp/jybase/esv1"
  5. "context"
  6. "fmt"
  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. "log"
  13. )
  14. type FindAnswerLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewFindAnswerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindAnswerLogic {
  20. return &FindAnswerLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *FindAnswerLogic) FindAnswer(in *knowledgeclient.FindAnswerReq) (*knowledgeclient.FindAnswerResp, error) {
  27. var question knowledgeclient.Question
  28. //组装es query
  29. query := util.DSL4SmartResponse(in.Question, in.TenantId, int(in.Type))
  30. fmt.Println("query:", query)
  31. res := elastic.Get(C.Es.Index, C.Es.Type, query)
  32. log.Println("结果:", res)
  33. if res != nil && len(*res) > 0 {
  34. data := (*res)[0]
  35. log.Println(data["answer"])
  36. question.Answer = cm.ObjToString(data["answer"])
  37. question.Question = cm.ObjToString(data["question"])
  38. }
  39. return &knowledgeclient.FindAnswerResp{
  40. ErrorCode: 0,
  41. ErrorMsg: "请求成功",
  42. Data: &question,
  43. }, nil
  44. }