findanswerlogic.go 1.4 KB

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