|
@@ -5,6 +5,7 @@ import (
|
|
|
. "app.yhyue.com/moapp/jybase/encrypt"
|
|
|
elastic "app.yhyue.com/moapp/jybase/esv1"
|
|
|
. "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/entity"
|
|
|
+ "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/init"
|
|
|
"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/knowledgeclient"
|
|
|
"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/util"
|
|
|
"database/sql"
|
|
@@ -77,6 +78,22 @@ func (k *KnowledgeService) KnowledgeAdd(param *knowledgeclient.AddRequest, segme
|
|
|
"entId": param.EntId,
|
|
|
}
|
|
|
b := elastic.Save(Index, Type, knowledge)
|
|
|
+ if !b {
|
|
|
+ return false, "es 保存失败"
|
|
|
+ }
|
|
|
+ // 存入向量库
|
|
|
+ //插入es
|
|
|
+ knowledgeV := map[string]interface{}{
|
|
|
+ "mod_time": time.Now().Unix(),
|
|
|
+ "answer": param.Answer,
|
|
|
+ "question": param.Question,
|
|
|
+ "id": answerId,
|
|
|
+ "entId": param.EntId,
|
|
|
+ "questionVector": util.EncodeVector(param.Question),
|
|
|
+ }
|
|
|
+ if !ESV7.Save(ESV7Index, ESV7Type, knowledgeV) {
|
|
|
+ logx.Error("知识库添加向量失败:", knowledgeV)
|
|
|
+ }
|
|
|
return b, ""
|
|
|
}
|
|
|
return fool, "插入mysql出错"
|
|
@@ -143,6 +160,42 @@ func (k *KnowledgeService) KnowledgeEdit(param *knowledgeclient.KnowledgeEditReq
|
|
|
}
|
|
|
ok1 := elastic.Del(Index, Type, query)
|
|
|
ok2 := elastic.Save(Index, Type, newKnowledge)
|
|
|
+ // 查询出来
|
|
|
+ queryByAid := fmt.Sprintf(`{
|
|
|
+ "query": {
|
|
|
+ "bool": {
|
|
|
+ "must": [
|
|
|
+ {
|
|
|
+ "term": {
|
|
|
+ "id": %v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "size": 1
|
|
|
+}`, param.AnswerId)
|
|
|
+ rs := ESV7.Get(ESV7Index, ESV7Type, queryByAid)
|
|
|
+ _id := ""
|
|
|
+ if rs != nil && len(*rs) > 0 {
|
|
|
+ _id = cm.InterfaceToStr((*rs)[0]["_id"])
|
|
|
+ }
|
|
|
+ // 存入向量库
|
|
|
+ //插入es
|
|
|
+ knowledgeV := map[string]interface{}{
|
|
|
+ "mod_time": time.Now().Unix(),
|
|
|
+ "answer": param.Answer,
|
|
|
+ "question": param.Question,
|
|
|
+ "id": param.AnswerId,
|
|
|
+ "entId": param.EntId,
|
|
|
+ "questionVector": util.EncodeVector(param.Question),
|
|
|
+ }
|
|
|
+ if _id != "" {
|
|
|
+ knowledgeV["_id"] = _id
|
|
|
+ }
|
|
|
+ if !ESV7.Save(ESV7Index, ESV7Type, knowledgeV) {
|
|
|
+ logx.Error("知识库添加向量失败:", knowledgeV)
|
|
|
+ }
|
|
|
return ok1 && ok2
|
|
|
}
|
|
|
return ok
|
|
@@ -204,7 +257,29 @@ func (k *KnowledgeService) KnowledgeDel(answerId int64) (ok bool) {
|
|
|
//删除es数据
|
|
|
query := `{"query":{"bool":{"must":[{"term":{"answerId":"` + strconv.Itoa(int(answerId)) + `"}}],"must_not":[],"should":[]}},"from":0,"size":1,"sort":[],"facets":{}}`
|
|
|
ok = elastic.Del(Index, Type, query)
|
|
|
+ queryByAid := fmt.Sprintf(`{
|
|
|
+ "query": {
|
|
|
+ "bool": {
|
|
|
+ "must": [
|
|
|
+ {
|
|
|
+ "term": {
|
|
|
+ "id": %v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "size": 1
|
|
|
+}`, answerId)
|
|
|
+ rs := ESV7.Get(ESV7Index, ESV7Type, queryByAid)
|
|
|
+ if rs != nil && len(*rs) > 0 {
|
|
|
+ _id := cm.InterfaceToStr((*rs)[0]["_id"])
|
|
|
+ if !ESV7.DelById(ESV7Index, ESV7Type, _id) {
|
|
|
+ logx.Error("删除向量库失败:", _id, answerId)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
return ok
|
|
|
}
|
|
|
|
|
@@ -213,10 +288,11 @@ func (k *KnowledgeService) FindAnswer(param *knowledgeclient.FindAnswerReq, addr
|
|
|
var question knowledgeclient.Question
|
|
|
robotEntId := SE.Decode4Hex(param.RobotEntId)
|
|
|
//组装es query
|
|
|
- query := util.DSL4SmartResponse(param.Question, robotEntId, int(param.Type), addr, index, segment)
|
|
|
+ //query := util.DSL4SmartResponse(param.Question, robotEntId, int(param.Type), addr, index, segment)
|
|
|
+ query := util.GetAnswerQueryStr(param.Question, robotEntId, 1, init.C.MinScore)
|
|
|
logx.Info("query:", query)
|
|
|
if query != "" {
|
|
|
- res := elastic.Get(Index, Type, query)
|
|
|
+ res := ESV7.Get(ESV7Index, ESV7Type, query)
|
|
|
if res != nil && len(*res) > 0 {
|
|
|
data := (*res)[0]
|
|
|
question.Answer = cm.ObjToString(data["answer"])
|