Browse Source

增加service层

renjiaojiao 3 years ago
parent
commit
a8f7d27545

+ 12 - 13
rpc/knowledge/internal/service/knowledgeService.go

@@ -249,13 +249,14 @@ func (k *KnowledgeService) FindAnswer(param *knowledgeclient.FindAnswerReq, addr
 	robotEntId := SE.Decode4Hex(param.RobotEntId)
 	//组装es query
 	query := util.DSL4SmartResponse(param.Question, robotEntId, int(param.Type), addr, index, segment)
-
-	res := elastic.Get(Index, Type, query)
-	log.Println("es查询:", res)
-	if res != nil && len(*res) > 0 {
-		data := (*res)[0]
-		question.Answer = cm.ObjToString(data["answer"])
-		question.Question = cm.ObjToString(data["question"])
+	logx.Info("query:", query)
+	if query != "" {
+		res := elastic.Get(Index, Type, query)
+		if res != nil && len(*res) > 0 {
+			data := (*res)[0]
+			question.Answer = cm.ObjToString(data["answer"])
+			question.Question = cm.ObjToString(data["question"])
+		}
 	}
 	return &question
 }
@@ -267,22 +268,20 @@ func (k *KnowledgeService) RecommendAnswer(param *knowledgeclient.FindAnswerReq,
 		answers     []*knowledgeclient.Question
 	)
 	//根据问题进行分词
-	keywords := ""
 	keywordsArr := util.HanlpGetNormalWords(param.Question, segment)
 	log.Println("keywordsArr", keywordsArr)
 	if len(keywordsArr) != 0 {
 		for _, val := range keywordsArr {
-			keywords += val + " "
+			keyWords += val + " "
 		}
 	}
-	log.Println("问题分词关键字1:", len(keyWords))
-	if keywords == "" {
-		keywords += param.Question
+	if keyWords == "" {
+		keyWords = param.Question
 	}
 	log.Println("问题分词关键字2:", keyWords)
 	var query = util.DSL4SearchByKwsOrid(keyWords, SE.Decode4Hex(param.RobotEntId))
 	res := elastic.GetAllByNgram(Index, Type, query, "", "", searchField, 0, 3, 0, false)
-	//log.Println("推荐3个答案:", res)
+	log.Println("推荐3个答案:", res)
 	if res != nil && len(*res) > 0 {
 		for _, val := range *res {
 			answers = append(answers, &knowledgeclient.Question{

+ 2 - 2
rpc/knowledge/test/knowledge_test.go

@@ -83,7 +83,7 @@ func Test_FindAnswer(t *testing.T) {
 	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
 	TestSystem := knowledgeclient.NewKnowledge(zrpc.MustNewClient(c.TestConf))
 	req := &knowledgeclient.FindAnswerReq{}
-	req.Question = "超级订阅为什么购买不了"
+	req.Question = ""
 	req.RobotEntId = "455b495c5c"
 	req.Type = 1
 	res, err := TestSystem.FindAnswer(ctx, req)
@@ -95,7 +95,7 @@ func Test_RecommendAnswer(t *testing.T) {
 	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
 	TestSystem := knowledgeclient.NewKnowledge(zrpc.MustNewClient(c.TestConf))
 	req := &knowledgeclient.FindAnswerReq{}
-	req.Question = "大会员"
+	req.Question = "订阅购买"
 	req.RobotEntId = "455b495c5c"
 	req.Type = 1
 	res, err := TestSystem.RecommendAnswer(ctx, req)

+ 2 - 8
rpc/knowledge/util/elasticsearch_dsl.go

@@ -3,13 +3,10 @@ package util
 import (
 	"fmt"
 	"github.com/zeromicro/go-zero/core/logx"
-	"log"
 	"strings"
 )
 
-/*
-	项目中所用到的几类查询dsl语句构建工具类
-*/
+/*项目中所用到的几类查询dsl语句构建工具类*/
 
 func DSL4SmartResponse(question string, entId string, msgType int, addr, index, segment string) string {
 	var (
@@ -19,15 +16,13 @@ func DSL4SmartResponse(question string, entId string, msgType int, addr, index,
 	)
 	var typeStr string
 	/*1.首先将问题使用hanlp分词*/
-	//hanlpCutWords := HanlpGetNormalWords(question, "http://39.106.145.77:8080/api/segment")
 	hanlpCutWords := HanlpGetNormalWords(question, segment)
-	log.Println("hanlp分词结果:", hanlpCutWords)
 	if len(hanlpCutWords) == 0 {
 		hanlpCutWords = append(hanlpCutWords, question)
 	}
 	question = strings.Join(hanlpCutWords, "")
 	lenQuestion := len([]rune(question))
-	//log.Println("len", lenQuestion)
+	logx.Info("lenQuestion", lenQuestion)
 	if lenQuestion >= 2 {
 		queryPercent := "40%"
 		if lenQuestion < 5 {
@@ -43,7 +38,6 @@ func DSL4SmartResponse(question string, entId string, msgType int, addr, index,
 			typeStr = "keywords.key_pinyin"
 		}
 		/*2使用sik分词将问题分词以获取更多查询词语*/
-		//mustque := ElasticSmartIK(question, "http://39.106.145.77:9201/smart/_analyze")
 		mustque := ElasticSmartIK(question, addr+"/"+index+"/_analyze")
 		if mustque != "" {
 			postFilter = fmt.Sprintf(postFilter, mustque)