findanswerlogic.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. )
  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. //组装es query
  28. query := util.DSL4SmartResponse(in.Question, in.TenantId, int(in.Type))
  29. fmt.Println("query:", query)
  30. res := elastic.Get(C.Es.Index, C.Es.Type, query)
  31. if res != nil && len(*res) > 0 {
  32. data := (*res)[0]
  33. question.XId = cm.ObjToString(data["_id"])
  34. question.Answer = cm.ObjToString(data["answer"])
  35. question.Question = cm.ObjToString(data["question"])
  36. }
  37. fmt.Println("根据问题匹配答案:", question)
  38. return &knowledgeclient.FindAnswerResp{
  39. ErrorCode: 0,
  40. ErrorMsg: "请求成功",
  41. Data: question,
  42. }, nil
  43. }