浏览代码

fix:知识资源库写入es

zqbao 4 月之前
父节点
当前提交
0ddefebef0
共有 1 个文件被更改,包括 21 次插入24 次删除
  1. 21 24
      services/knowledge/es.go

+ 21 - 24
services/knowledge/es.go

@@ -72,29 +72,30 @@ func SearchKnowledgeResourceByEs(resourceType int, keywordStr string, showSysIds
 		})
 	}
 
-	shouldMap := map[string]interface{}{
-		"should": []interface{}{
-			map[string]interface{}{
-				"match": map[string]interface{}{
-					"Title": keywordStr,
+	if keywordStr != "" {
+		shouldMap := map[string]interface{}{
+			"should": []interface{}{
+				map[string]interface{}{
+					"match": map[string]interface{}{
+						"Title": keywordStr,
+					},
 				},
-			},
-			// 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
-			map[string]interface{}{
-				"match": map[string]interface{}{
-					"Title": map[string]interface{}{
-						"query":    keywordStr,
-						"operator": "and",
+				// 因为关键词被分了,所以需要用下面的语句来让他 整个词 查询,从而加重整词的权重
+				map[string]interface{}{
+					"match": map[string]interface{}{
+						"Title": map[string]interface{}{
+							"query":    keywordStr,
+							"operator": "and",
+						},
 					},
 				},
 			},
-		},
+		}
+		mustMap = append(mustMap, map[string]interface{}{
+			"bool": shouldMap,
+		})
 	}
 
-	mustMap = append(mustMap, map[string]interface{}{
-		"bool": shouldMap,
-	})
-
 	queryMap := map[string]interface{}{
 		"query": map[string]interface{}{
 			"bool": map[string]interface{}{
@@ -163,16 +164,12 @@ func EsAddOrEditKnowledgeResource(item *knowledge.KnowledgeResource) (err error)
 	}()
 	indexName := utils.EsKnowledgeResourceIndexName
 	client := utils.EsClient
-	itemJson, err := json.Marshal(item)
-	if err != nil {
-		fmt.Println("EsAddOrEditKnowledgeResource json.Marshal itemJson err:", err)
-		return
-	}
 
-	request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(itemJson)
+	request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(item)
 	response, err := request.Do(context.Background())
 	if err != nil {
-		fmt.Println("add json:", string(itemJson))
+		jsonBytes, _ := json.Marshal(item)
+		fmt.Println("add json:", string(jsonBytes))
 		fmt.Println("EsAddOrEditKnowledgeResource err:", err)
 		return
 	}